Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master:
Move id3v2 tag writing to a separate file.
swscale: add missing colons to x86 assembly yuv2planeX.
g722: split decoder and encoder into separate files
cosmetics: remove extra spaces before end-of-statement semi-colons
vorbisdec: check output buffer size before writing output
wavpack: calculate bpp using av_get_bytes_per_sample()
ac3enc: Set max value for mode options correctly
lavc: move get_b_cbp() from h263.h to mpeg4videoenc.c
mpeg12: move closed_gop from MpegEncContext to Mpeg1Context
mpeg12: move full_pel from MpegEncContext to Mpeg1Context
mpeg12: move Mpeg1Context from mpeg12.c to mpeg12.h
mpegvideo: remove some unused variables from MpegEncContext.

Conflicts:
libavcodec/mpeg12.c
libavformat/mp3enc.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2011/10/24 08:01:21
Showing 23 changed files
... ...
@@ -519,8 +519,8 @@ OBJS-$(CONFIG_ADPCM_EA_R1_DECODER)        += adpcm.o
519 519
 OBJS-$(CONFIG_ADPCM_EA_R2_DECODER)        += adpcm.o
520 520
 OBJS-$(CONFIG_ADPCM_EA_R3_DECODER)        += adpcm.o
521 521
 OBJS-$(CONFIG_ADPCM_EA_XAS_DECODER)       += adpcm.o
522
-OBJS-$(CONFIG_ADPCM_G722_DECODER)         += g722.o
523
-OBJS-$(CONFIG_ADPCM_G722_ENCODER)         += g722.o
522
+OBJS-$(CONFIG_ADPCM_G722_DECODER)         += g722.o g722dec.o
523
+OBJS-$(CONFIG_ADPCM_G722_ENCODER)         += g722.o g722enc.o
524 524
 OBJS-$(CONFIG_ADPCM_G726_DECODER)         += g726.o
525 525
 OBJS-$(CONFIG_ADPCM_G726_ENCODER)         += g726.o
526 526
 OBJS-$(CONFIG_ADPCM_IMA_AMV_DECODER)      += adpcm.o adpcm_data.o
... ...
@@ -45,7 +45,7 @@ static const AVOption eac3_options[] = {
45 45
 /* other metadata options */
46 46
 {"copyright", "Copyright Bit", OFFSET(copyright), AV_OPT_TYPE_INT, {.dbl = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, 1, AC3ENC_PARAM},
47 47
 {"dialnorm", "Dialogue Level (dB)", OFFSET(dialogue_level), AV_OPT_TYPE_INT, {.dbl = -31 }, -31, -1, AC3ENC_PARAM},
48
-{"dsur_mode", "Dolby Surround Mode", OFFSET(dolby_surround_mode), AV_OPT_TYPE_INT, {.dbl = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_MODE_OFF, AC3ENC_PARAM, "dsur_mode"},
48
+{"dsur_mode", "Dolby Surround Mode", OFFSET(dolby_surround_mode), AV_OPT_TYPE_INT, {.dbl = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_MODE_ON, AC3ENC_PARAM, "dsur_mode"},
49 49
     {"notindicated", "Not Indicated (default)",    0, AV_OPT_TYPE_CONST, {.dbl = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
50 50
     {"on",           "Dolby Surround Encoded",     0, AV_OPT_TYPE_CONST, {.dbl = AC3ENC_OPT_MODE_ON       }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
51 51
     {"off",          "Not Dolby Surround Encoded", 0, AV_OPT_TYPE_CONST, {.dbl = AC3ENC_OPT_MODE_OFF      }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsur_mode"},
... ...
@@ -59,11 +59,11 @@ static const AVOption eac3_options[] = {
59 59
 {"ltrt_surmixlev", "Lt/Rt Surround Mix Level", OFFSET(ltrt_surround_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
60 60
 {"loro_cmixlev", "Lo/Ro Center Mix Level", OFFSET(loro_center_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
61 61
 {"loro_surmixlev", "Lo/Ro Surround Mix Level", OFFSET(loro_surround_mix_level), AV_OPT_TYPE_FLOAT, {.dbl = -1.0 }, -1.0, 2.0, AC3ENC_PARAM},
62
-{"dsurex_mode", "Dolby Surround EX Mode", OFFSET(dolby_surround_ex_mode), AV_OPT_TYPE_INT, {.dbl = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_MODE_OFF, AC3ENC_PARAM, "dsurex_mode"},
62
+{"dsurex_mode", "Dolby Surround EX Mode", OFFSET(dolby_surround_ex_mode), AV_OPT_TYPE_INT, {.dbl = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_MODE_ON, AC3ENC_PARAM, "dsurex_mode"},
63 63
     {"notindicated", "Not Indicated (default)",       0, AV_OPT_TYPE_CONST, {.dbl = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
64 64
     {"on",           "Dolby Surround EX Encoded",     0, AV_OPT_TYPE_CONST, {.dbl = AC3ENC_OPT_MODE_ON       }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
65 65
     {"off",          "Not Dolby Surround EX Encoded", 0, AV_OPT_TYPE_CONST, {.dbl = AC3ENC_OPT_MODE_OFF      }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dsurex_mode"},
66
-{"dheadphone_mode", "Dolby Headphone Mode", OFFSET(dolby_headphone_mode), AV_OPT_TYPE_INT, {.dbl = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_MODE_OFF, AC3ENC_PARAM, "dheadphone_mode"},
66
+{"dheadphone_mode", "Dolby Headphone Mode", OFFSET(dolby_headphone_mode), AV_OPT_TYPE_INT, {.dbl = AC3ENC_OPT_NONE }, AC3ENC_OPT_NONE, AC3ENC_OPT_MODE_ON, AC3ENC_PARAM, "dheadphone_mode"},
67 67
     {"notindicated", "Not Indicated (default)",     0, AV_OPT_TYPE_CONST, {.dbl = AC3ENC_OPT_NOT_INDICATED }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
68 68
     {"on",           "Dolby Headphone Encoded",     0, AV_OPT_TYPE_CONST, {.dbl = AC3ENC_OPT_MODE_ON       }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
69 69
     {"off",          "Not Dolby Headphone Encoded", 0, AV_OPT_TYPE_CONST, {.dbl = AC3ENC_OPT_MODE_OFF      }, INT_MIN, INT_MAX, AC3ENC_PARAM, "dheadphone_mode"},
... ...
@@ -29,11 +29,11 @@
29 29
 #endif
30 30
 
31 31
 /* MpegEncContext */
32
-#define Y_DC_SCALE               0xac
33
-#define C_DC_SCALE               0xb0
34
-#define AC_PRED                  0xb4
35
-#define BLOCK_LAST_INDEX         0xb8
36
-#define H263_AIC                 0xe8
37
-#define INTER_SCANTAB_RASTER_END 0x130
32
+#define Y_DC_SCALE               0xa8
33
+#define C_DC_SCALE               0xac
34
+#define AC_PRED                  0xb0
35
+#define BLOCK_LAST_INDEX         0xb4
36
+#define H263_AIC                 0xe4
37
+#define INTER_SCANTAB_RASTER_END 0x12c
38 38
 
39 39
 #endif /* AVCODEC_ARM_ASM_OFFSETS_H */
... ...
@@ -36,45 +36,8 @@
36 36
  *       respectively of each byte are ignored.
37 37
  */
38 38
 
39
-#include "avcodec.h"
40 39
 #include "mathops.h"
41
-#include "get_bits.h"
42
-
43
-#define PREV_SAMPLES_BUF_SIZE 1024
44
-
45
-#define FREEZE_INTERVAL 128
46
-
47
-typedef struct {
48
-    int16_t prev_samples[PREV_SAMPLES_BUF_SIZE]; ///< memory of past decoded samples
49
-    int     prev_samples_pos;        ///< the number of values in prev_samples
50
-
51
-    /**
52
-     * The band[0] and band[1] correspond respectively to the lower band and higher band.
53
-     */
54
-    struct G722Band {
55
-        int16_t s_predictor;         ///< predictor output value
56
-        int32_t s_zero;              ///< previous output signal from zero predictor
57
-        int8_t  part_reconst_mem[2]; ///< signs of previous partially reconstructed signals
58
-        int16_t prev_qtzd_reconst;   ///< previous quantized reconstructed signal (internal value, using low_inv_quant4)
59
-        int16_t pole_mem[2];         ///< second-order pole section coefficient buffer
60
-        int32_t diff_mem[6];         ///< quantizer difference signal memory
61
-        int16_t zero_mem[6];         ///< Seventh-order zero section coefficient buffer
62
-        int16_t log_factor;          ///< delayed 2-logarithmic quantizer factor
63
-        int16_t scale_factor;        ///< delayed quantizer scale factor
64
-    } band[2];
65
-
66
-    struct TrellisNode {
67
-        struct G722Band state;
68
-        uint32_t ssd;
69
-        int path;
70
-    } *node_buf[2], **nodep_buf[2];
71
-
72
-    struct TrellisPath {
73
-        int value;
74
-        int prev;
75
-    } *paths[2];
76
-} G722Context;
77
-
40
+#include "g722.h"
78 41
 
79 42
 static const int8_t sign_lookup[2] = { -1, 1 };
80 43
 
... ...
@@ -85,7 +48,7 @@ static const int16_t inv_log2_table[32] = {
85 85
     3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008
86 86
 };
87 87
 static const int16_t high_log_factor_step[2] = { 798, -214 };
88
-static const int16_t high_inv_quant[4] = { -926, -202, 926, 202 };
88
+const int16_t ff_g722_high_inv_quant[4] = { -926, -202, 926, 202 };
89 89
 /**
90 90
  * low_log_factor_step[index] == wl[rl42[index]]
91 91
  */
... ...
@@ -93,11 +56,11 @@ static const int16_t low_log_factor_step[16] = {
93 93
      -60, 3042, 1198, 538, 334, 172,  58, -30,
94 94
     3042, 1198,  538, 334, 172,  58, -30, -60
95 95
 };
96
-static const int16_t low_inv_quant4[16] = {
96
+const int16_t ff_g722_low_inv_quant4[16] = {
97 97
        0, -2557, -1612, -1121,  -786,  -530,  -323,  -150,
98 98
     2557,  1612,  1121,   786,   530,   323,   150,     0
99 99
 };
100
-static const int16_t low_inv_quant6[64] = {
100
+const int16_t ff_g722_low_inv_quant6[64] = {
101 101
      -17,   -17,   -17,   -17, -3101, -2738, -2376, -2088,
102 102
    -1873, -1689, -1535, -1399, -1279, -1170, -1072,  -982,
103 103
     -899,  -822,  -750,  -682,  -618,  -558,  -501,  -447,
... ...
@@ -173,10 +136,10 @@ static int inline linear_scale_factor(const int log_factor)
173 173
     return shift < 0 ? wd1 >> -shift : wd1 << shift;
174 174
 }
175 175
 
176
-static void update_low_predictor(struct G722Band *band, const int ilow)
176
+void ff_g722_update_low_predictor(struct G722Band *band, const int ilow)
177 177
 {
178 178
     do_adaptive_prediction(band,
179
-                           band->scale_factor * low_inv_quant4[ilow] >> 10);
179
+                           band->scale_factor * ff_g722_low_inv_quant4[ilow] >> 10);
180 180
 
181 181
     // quantizer adaptation
182 182
     band->log_factor   = av_clip((band->log_factor * 127 >> 7) +
... ...
@@ -184,7 +147,7 @@ static void update_low_predictor(struct G722Band *band, const int ilow)
184 184
     band->scale_factor = linear_scale_factor(band->log_factor - (8 << 11));
185 185
 }
186 186
 
187
-static void update_high_predictor(struct G722Band *band, const int dhigh,
187
+void ff_g722_update_high_predictor(struct G722Band *band, const int dhigh,
188 188
                                   const int ihigh)
189 189
 {
190 190
     do_adaptive_prediction(band, dhigh);
... ...
@@ -195,7 +158,7 @@ static void update_high_predictor(struct G722Band *band, const int dhigh,
195 195
     band->scale_factor = linear_scale_factor(band->log_factor - (10 << 11));
196 196
 }
197 197
 
198
-static void apply_qmf(const int16_t *prev_samples, int *xout1, int *xout2)
198
+void ff_g722_apply_qmf(const int16_t *prev_samples, int *xout1, int *xout2)
199 199
 {
200 200
     int i;
201 201
 
... ...
@@ -206,377 +169,3 @@ static void apply_qmf(const int16_t *prev_samples, int *xout1, int *xout2)
206 206
         MAC16(*xout1, prev_samples[2*i+1], qmf_coeffs[11-i]);
207 207
     }
208 208
 }
209
-
210
-static av_cold int g722_init(AVCodecContext * avctx)
211
-{
212
-    G722Context *c = avctx->priv_data;
213
-
214
-    if (avctx->channels != 1) {
215
-        av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
216
-        return AVERROR_INVALIDDATA;
217
-    }
218
-    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
219
-
220
-    switch (avctx->bits_per_coded_sample) {
221
-    case 8:
222
-    case 7:
223
-    case 6:
224
-        break;
225
-    default:
226
-        av_log(avctx, AV_LOG_WARNING, "Unsupported bits_per_coded_sample [%d], "
227
-                                      "assuming 8\n",
228
-                                      avctx->bits_per_coded_sample);
229
-    case 0:
230
-        avctx->bits_per_coded_sample = 8;
231
-        break;
232
-    }
233
-
234
-    c->band[0].scale_factor = 8;
235
-    c->band[1].scale_factor = 2;
236
-    c->prev_samples_pos = 22;
237
-
238
-    if (avctx->lowres)
239
-        avctx->sample_rate /= 2;
240
-
241
-    if (avctx->trellis) {
242
-        int frontier = 1 << avctx->trellis;
243
-        int max_paths = frontier * FREEZE_INTERVAL;
244
-        int i;
245
-        for (i = 0; i < 2; i++) {
246
-            c->paths[i] = av_mallocz(max_paths * sizeof(**c->paths));
247
-            c->node_buf[i] = av_mallocz(2 * frontier * sizeof(**c->node_buf));
248
-            c->nodep_buf[i] = av_mallocz(2 * frontier * sizeof(**c->nodep_buf));
249
-        }
250
-    }
251
-
252
-    return 0;
253
-}
254
-
255
-static av_cold int g722_close(AVCodecContext *avctx)
256
-{
257
-    G722Context *c = avctx->priv_data;
258
-    int i;
259
-    for (i = 0; i < 2; i++) {
260
-        av_freep(&c->paths[i]);
261
-        av_freep(&c->node_buf[i]);
262
-        av_freep(&c->nodep_buf[i]);
263
-    }
264
-    return 0;
265
-}
266
-
267
-#if CONFIG_ADPCM_G722_DECODER
268
-static const int16_t low_inv_quant5[32] = {
269
-     -35,   -35, -2919, -2195, -1765, -1458, -1219, -1023,
270
-    -858,  -714,  -587,  -473,  -370,  -276,  -190,  -110,
271
-    2919,  2195,  1765,  1458,  1219,  1023,   858,   714,
272
-     587,   473,   370,   276,   190,   110,    35,   -35
273
-};
274
-
275
-static const int16_t *low_inv_quants[3] = { low_inv_quant6, low_inv_quant5,
276
-                                 low_inv_quant4 };
277
-
278
-static int g722_decode_frame(AVCodecContext *avctx, void *data,
279
-                             int *data_size, AVPacket *avpkt)
280
-{
281
-    G722Context *c = avctx->priv_data;
282
-    int16_t *out_buf = data;
283
-    int j, out_len = 0;
284
-    const int skip = 8 - avctx->bits_per_coded_sample;
285
-    const int16_t *quantizer_table = low_inv_quants[skip];
286
-    GetBitContext gb;
287
-
288
-    init_get_bits(&gb, avpkt->data, avpkt->size * 8);
289
-
290
-    for (j = 0; j < avpkt->size; j++) {
291
-        int ilow, ihigh, rlow;
292
-
293
-        ihigh = get_bits(&gb, 2);
294
-        ilow = get_bits(&gb, 6 - skip);
295
-        skip_bits(&gb, skip);
296
-
297
-        rlow = av_clip((c->band[0].scale_factor * quantizer_table[ilow] >> 10)
298
-                      + c->band[0].s_predictor, -16384, 16383);
299
-
300
-        update_low_predictor(&c->band[0], ilow >> (2 - skip));
301
-
302
-        if (!avctx->lowres) {
303
-            const int dhigh = c->band[1].scale_factor *
304
-                              high_inv_quant[ihigh] >> 10;
305
-            const int rhigh = av_clip(dhigh + c->band[1].s_predictor,
306
-                                      -16384, 16383);
307
-            int xout1, xout2;
308
-
309
-            update_high_predictor(&c->band[1], dhigh, ihigh);
310
-
311
-            c->prev_samples[c->prev_samples_pos++] = rlow + rhigh;
312
-            c->prev_samples[c->prev_samples_pos++] = rlow - rhigh;
313
-            apply_qmf(c->prev_samples + c->prev_samples_pos - 24,
314
-                      &xout1, &xout2);
315
-            out_buf[out_len++] = av_clip_int16(xout1 >> 12);
316
-            out_buf[out_len++] = av_clip_int16(xout2 >> 12);
317
-            if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
318
-                memmove(c->prev_samples,
319
-                        c->prev_samples + c->prev_samples_pos - 22,
320
-                        22 * sizeof(c->prev_samples[0]));
321
-                c->prev_samples_pos = 22;
322
-            }
323
-        } else
324
-            out_buf[out_len++] = rlow;
325
-    }
326
-    *data_size = out_len << 1;
327
-    return avpkt->size;
328
-}
329
-
330
-AVCodec ff_adpcm_g722_decoder = {
331
-    .name           = "g722",
332
-    .type           = AVMEDIA_TYPE_AUDIO,
333
-    .id             = CODEC_ID_ADPCM_G722,
334
-    .priv_data_size = sizeof(G722Context),
335
-    .init           = g722_init,
336
-    .decode         = g722_decode_frame,
337
-    .long_name      = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
338
-    .max_lowres     = 1,
339
-};
340
-#endif
341
-
342
-#if CONFIG_ADPCM_G722_ENCODER
343
-static const int16_t low_quant[33] = {
344
-      35,   72,  110,  150,  190,  233,  276,  323,
345
-     370,  422,  473,  530,  587,  650,  714,  786,
346
-     858,  940, 1023, 1121, 1219, 1339, 1458, 1612,
347
-    1765, 1980, 2195, 2557, 2919
348
-};
349
-
350
-static inline void filter_samples(G722Context *c, const int16_t *samples,
351
-                                  int *xlow, int *xhigh)
352
-{
353
-    int xout1, xout2;
354
-    c->prev_samples[c->prev_samples_pos++] = samples[0];
355
-    c->prev_samples[c->prev_samples_pos++] = samples[1];
356
-    apply_qmf(c->prev_samples + c->prev_samples_pos - 24, &xout1, &xout2);
357
-    *xlow  = xout1 + xout2 >> 13;
358
-    *xhigh = xout1 - xout2 >> 13;
359
-    if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
360
-        memmove(c->prev_samples,
361
-                c->prev_samples + c->prev_samples_pos - 22,
362
-                22 * sizeof(c->prev_samples[0]));
363
-        c->prev_samples_pos = 22;
364
-    }
365
-}
366
-
367
-static inline int encode_high(const struct G722Band *state, int xhigh)
368
-{
369
-    int diff = av_clip_int16(xhigh - state->s_predictor);
370
-    int pred = 141 * state->scale_factor >> 8;
371
-           /* = diff >= 0 ? (diff < pred) + 2 : diff >= -pred */
372
-    return ((diff ^ (diff >> (sizeof(diff)*8-1))) < pred) + 2*(diff >= 0);
373
-}
374
-
375
-static inline int encode_low(const struct G722Band* state, int xlow)
376
-{
377
-    int diff  = av_clip_int16(xlow - state->s_predictor);
378
-           /* = diff >= 0 ? diff : -(diff + 1) */
379
-    int limit = diff ^ (diff >> (sizeof(diff)*8-1));
380
-    int i = 0;
381
-    limit = limit + 1 << 10;
382
-    if (limit > low_quant[8] * state->scale_factor)
383
-        i = 9;
384
-    while (i < 29 && limit > low_quant[i] * state->scale_factor)
385
-        i++;
386
-    return (diff < 0 ? (i < 2 ? 63 : 33) : 61) - i;
387
-}
388
-
389
-static int g722_encode_trellis(AVCodecContext *avctx,
390
-                               uint8_t *dst, int buf_size, void *data)
391
-{
392
-    G722Context *c = avctx->priv_data;
393
-    const int16_t *samples = data;
394
-    int i, j, k;
395
-    int frontier = 1 << avctx->trellis;
396
-    struct TrellisNode **nodes[2];
397
-    struct TrellisNode **nodes_next[2];
398
-    int pathn[2] = {0, 0}, froze = -1;
399
-    struct TrellisPath *p[2];
400
-
401
-    for (i = 0; i < 2; i++) {
402
-        nodes[i] = c->nodep_buf[i];
403
-        nodes_next[i] = c->nodep_buf[i] + frontier;
404
-        memset(c->nodep_buf[i], 0, 2 * frontier * sizeof(*c->nodep_buf));
405
-        nodes[i][0] = c->node_buf[i] + frontier;
406
-        nodes[i][0]->ssd = 0;
407
-        nodes[i][0]->path = 0;
408
-        nodes[i][0]->state = c->band[i];
409
-    }
410
-
411
-    for (i = 0; i < buf_size >> 1; i++) {
412
-        int xlow, xhigh;
413
-        struct TrellisNode *next[2];
414
-        int heap_pos[2] = {0, 0};
415
-
416
-        for (j = 0; j < 2; j++) {
417
-            next[j] = c->node_buf[j] + frontier*(i & 1);
418
-            memset(nodes_next[j], 0, frontier * sizeof(**nodes_next));
419
-        }
420
-
421
-        filter_samples(c, &samples[2*i], &xlow, &xhigh);
422
-
423
-        for (j = 0; j < frontier && nodes[0][j]; j++) {
424
-            /* Only k >> 2 affects the future adaptive state, therefore testing
425
-             * small steps that don't change k >> 2 is useless, the orignal
426
-             * value from encode_low is better than them. Since we step k
427
-             * in steps of 4, make sure range is a multiple of 4, so that
428
-             * we don't miss the original value from encode_low. */
429
-            int range = j < frontier/2 ? 4 : 0;
430
-            struct TrellisNode *cur_node = nodes[0][j];
431
-
432
-            int ilow = encode_low(&cur_node->state, xlow);
433
-
434
-            for (k = ilow - range; k <= ilow + range && k <= 63; k += 4) {
435
-                int decoded, dec_diff, pos;
436
-                uint32_t ssd;
437
-                struct TrellisNode* node;
438
-
439
-                if (k < 0)
440
-                    continue;
441
-
442
-                decoded = av_clip((cur_node->state.scale_factor *
443
-                                  low_inv_quant6[k] >> 10)
444
-                                + cur_node->state.s_predictor, -16384, 16383);
445
-                dec_diff = xlow - decoded;
446
-
447
-#define STORE_NODE(index, UPDATE, VALUE)\
448
-                ssd = cur_node->ssd + dec_diff*dec_diff;\
449
-                /* Check for wraparound. Using 64 bit ssd counters would \
450
-                 * be simpler, but is slower on x86 32 bit. */\
451
-                if (ssd < cur_node->ssd)\
452
-                    continue;\
453
-                if (heap_pos[index] < frontier) {\
454
-                    pos = heap_pos[index]++;\
455
-                    assert(pathn[index] < FREEZE_INTERVAL * frontier);\
456
-                    node = nodes_next[index][pos] = next[index]++;\
457
-                    node->path = pathn[index]++;\
458
-                } else {\
459
-                    /* Try to replace one of the leaf nodes with the new \
460
-                     * one, but not always testing the same leaf position */\
461
-                    pos = (frontier>>1) + (heap_pos[index] & ((frontier>>1) - 1));\
462
-                    if (ssd >= nodes_next[index][pos]->ssd)\
463
-                        continue;\
464
-                    heap_pos[index]++;\
465
-                    node = nodes_next[index][pos];\
466
-                }\
467
-                node->ssd = ssd;\
468
-                node->state = cur_node->state;\
469
-                UPDATE;\
470
-                c->paths[index][node->path].value = VALUE;\
471
-                c->paths[index][node->path].prev = cur_node->path;\
472
-                /* Sift the newly inserted node up in the heap to restore \
473
-                 * the heap property */\
474
-                while (pos > 0) {\
475
-                    int parent = (pos - 1) >> 1;\
476
-                    if (nodes_next[index][parent]->ssd <= ssd)\
477
-                        break;\
478
-                    FFSWAP(struct TrellisNode*, nodes_next[index][parent],\
479
-                                                nodes_next[index][pos]);\
480
-                    pos = parent;\
481
-                }
482
-                STORE_NODE(0, update_low_predictor(&node->state, k >> 2), k);
483
-            }
484
-        }
485
-
486
-        for (j = 0; j < frontier && nodes[1][j]; j++) {
487
-            int ihigh;
488
-            struct TrellisNode *cur_node = nodes[1][j];
489
-
490
-            /* We don't try to get any initial guess for ihigh via
491
-             * encode_high - since there's only 4 possible values, test
492
-             * them all. Testing all of these gives a much, much larger
493
-             * gain than testing a larger range around ilow. */
494
-            for (ihigh = 0; ihigh < 4; ihigh++) {
495
-                int dhigh, decoded, dec_diff, pos;
496
-                uint32_t ssd;
497
-                struct TrellisNode* node;
498
-
499
-                dhigh = cur_node->state.scale_factor *
500
-                        high_inv_quant[ihigh] >> 10;
501
-                decoded = av_clip(dhigh + cur_node->state.s_predictor,
502
-                                  -16384, 16383);
503
-                dec_diff = xhigh - decoded;
504
-
505
-                STORE_NODE(1, update_high_predictor(&node->state, dhigh, ihigh), ihigh);
506
-            }
507
-        }
508
-
509
-        for (j = 0; j < 2; j++) {
510
-            FFSWAP(struct TrellisNode**, nodes[j], nodes_next[j]);
511
-
512
-            if (nodes[j][0]->ssd > (1 << 16)) {
513
-                for (k = 1; k < frontier && nodes[j][k]; k++)
514
-                    nodes[j][k]->ssd -= nodes[j][0]->ssd;
515
-                nodes[j][0]->ssd = 0;
516
-            }
517
-        }
518
-
519
-        if (i == froze + FREEZE_INTERVAL) {
520
-            p[0] = &c->paths[0][nodes[0][0]->path];
521
-            p[1] = &c->paths[1][nodes[1][0]->path];
522
-            for (j = i; j > froze; j--) {
523
-                dst[j] = p[1]->value << 6 | p[0]->value;
524
-                p[0] = &c->paths[0][p[0]->prev];
525
-                p[1] = &c->paths[1][p[1]->prev];
526
-            }
527
-            froze = i;
528
-            pathn[0] = pathn[1] = 0;
529
-            memset(nodes[0] + 1, 0, (frontier - 1)*sizeof(**nodes));
530
-            memset(nodes[1] + 1, 0, (frontier - 1)*sizeof(**nodes));
531
-        }
532
-    }
533
-
534
-    p[0] = &c->paths[0][nodes[0][0]->path];
535
-    p[1] = &c->paths[1][nodes[1][0]->path];
536
-    for (j = i; j > froze; j--) {
537
-        dst[j] = p[1]->value << 6 | p[0]->value;
538
-        p[0] = &c->paths[0][p[0]->prev];
539
-        p[1] = &c->paths[1][p[1]->prev];
540
-    }
541
-    c->band[0] = nodes[0][0]->state;
542
-    c->band[1] = nodes[1][0]->state;
543
-
544
-    return i;
545
-}
546
-
547
-static int g722_encode_frame(AVCodecContext *avctx,
548
-                             uint8_t *dst, int buf_size, void *data)
549
-{
550
-    G722Context *c = avctx->priv_data;
551
-    const int16_t *samples = data;
552
-    int i;
553
-
554
-    if (avctx->trellis)
555
-        return g722_encode_trellis(avctx, dst, buf_size, data);
556
-
557
-    for (i = 0; i < buf_size >> 1; i++) {
558
-        int xlow, xhigh, ihigh, ilow;
559
-        filter_samples(c, &samples[2*i], &xlow, &xhigh);
560
-        ihigh = encode_high(&c->band[1], xhigh);
561
-        ilow  = encode_low(&c->band[0], xlow);
562
-        update_high_predictor(&c->band[1], c->band[1].scale_factor *
563
-                              high_inv_quant[ihigh] >> 10, ihigh);
564
-        update_low_predictor(&c->band[0], ilow >> 2);
565
-        *dst++ = ihigh << 6 | ilow;
566
-    }
567
-    return i;
568
-}
569
-
570
-AVCodec ff_adpcm_g722_encoder = {
571
-    .name           = "g722",
572
-    .type           = AVMEDIA_TYPE_AUDIO,
573
-    .id             = CODEC_ID_ADPCM_G722,
574
-    .priv_data_size = sizeof(G722Context),
575
-    .init           = g722_init,
576
-    .close          = g722_close,
577
-    .encode         = g722_encode_frame,
578
-    .long_name      = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
579
-    .sample_fmts    = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
580
-};
581
-#endif
582
-
583 209
new file mode 100644
... ...
@@ -0,0 +1,74 @@
0
+/*
1
+ * Copyright (c) CMU 1993 Computer Science, Speech Group
2
+ *                        Chengxiang Lu and Alex Hauptmann
3
+ * Copyright (c) 2005 Steve Underwood <steveu at coppice.org>
4
+ * Copyright (c) 2009 Kenan Gillet
5
+ * Copyright (c) 2010 Martin Storsjo
6
+ *
7
+ * This file is part of Libav.
8
+ *
9
+ * Libav is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU Lesser General Public
11
+ * License as published by the Free Software Foundation; either
12
+ * version 2.1 of the License, or (at your option) any later version.
13
+ *
14
+ * Libav is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
+ * Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with Libav; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ */
23
+
24
+#ifndef AVCODEC_G722_H
25
+#define AVCODEC_G722_H
26
+
27
+#include <stdint.h>
28
+
29
+#define PREV_SAMPLES_BUF_SIZE 1024
30
+
31
+typedef struct {
32
+    int16_t prev_samples[PREV_SAMPLES_BUF_SIZE]; ///< memory of past decoded samples
33
+    int     prev_samples_pos;        ///< the number of values in prev_samples
34
+
35
+    /**
36
+     * The band[0] and band[1] correspond respectively to the lower band and higher band.
37
+     */
38
+    struct G722Band {
39
+        int16_t s_predictor;         ///< predictor output value
40
+        int32_t s_zero;              ///< previous output signal from zero predictor
41
+        int8_t  part_reconst_mem[2]; ///< signs of previous partially reconstructed signals
42
+        int16_t prev_qtzd_reconst;   ///< previous quantized reconstructed signal (internal value, using low_inv_quant4)
43
+        int16_t pole_mem[2];         ///< second-order pole section coefficient buffer
44
+        int32_t diff_mem[6];         ///< quantizer difference signal memory
45
+        int16_t zero_mem[6];         ///< Seventh-order zero section coefficient buffer
46
+        int16_t log_factor;          ///< delayed 2-logarithmic quantizer factor
47
+        int16_t scale_factor;        ///< delayed quantizer scale factor
48
+    } band[2];
49
+
50
+    struct TrellisNode {
51
+        struct G722Band state;
52
+        uint32_t ssd;
53
+        int path;
54
+    } *node_buf[2], **nodep_buf[2];
55
+
56
+    struct TrellisPath {
57
+        int value;
58
+        int prev;
59
+    } *paths[2];
60
+} G722Context;
61
+
62
+extern const int16_t ff_g722_high_inv_quant[4];
63
+extern const int16_t ff_g722_low_inv_quant4[16];
64
+extern const int16_t ff_g722_low_inv_quant6[64];
65
+
66
+void ff_g722_update_low_predictor(struct G722Band *band, const int ilow);
67
+
68
+void ff_g722_update_high_predictor(struct G722Band *band, const int dhigh,
69
+                                   const int ihigh);
70
+
71
+void ff_g722_apply_qmf(const int16_t *prev_samples, int *xout1, int *xout2);
72
+
73
+#endif /* AVCODEC_G722_H */
0 74
new file mode 100644
... ...
@@ -0,0 +1,147 @@
0
+/*
1
+ * Copyright (c) CMU 1993 Computer Science, Speech Group
2
+ *                        Chengxiang Lu and Alex Hauptmann
3
+ * Copyright (c) 2005 Steve Underwood <steveu at coppice.org>
4
+ * Copyright (c) 2009 Kenan Gillet
5
+ * Copyright (c) 2010 Martin Storsjo
6
+ *
7
+ * This file is part of Libav.
8
+ *
9
+ * Libav is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU Lesser General Public
11
+ * License as published by the Free Software Foundation; either
12
+ * version 2.1 of the License, or (at your option) any later version.
13
+ *
14
+ * Libav is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
+ * Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with Libav; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ */
23
+
24
+/**
25
+ * @file
26
+ * G.722 ADPCM audio decoder
27
+ *
28
+ * This G.722 decoder is a bit-exact implementation of the ITU G.722
29
+ * specification for all three specified bitrates - 64000bps, 56000bps
30
+ * and 48000bps. It passes the ITU tests.
31
+ *
32
+ * @note For the 56000bps and 48000bps bitrates, the lowest 1 or 2 bits
33
+ *       respectively of each byte are ignored.
34
+ */
35
+
36
+#include "avcodec.h"
37
+#include "get_bits.h"
38
+#include "g722.h"
39
+
40
+static av_cold int g722_decode_init(AVCodecContext * avctx)
41
+{
42
+    G722Context *c = avctx->priv_data;
43
+
44
+    if (avctx->channels != 1) {
45
+        av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
46
+        return AVERROR_INVALIDDATA;
47
+    }
48
+    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
49
+
50
+    switch (avctx->bits_per_coded_sample) {
51
+    case 8:
52
+    case 7:
53
+    case 6:
54
+        break;
55
+    default:
56
+        av_log(avctx, AV_LOG_WARNING, "Unsupported bits_per_coded_sample [%d], "
57
+                                      "assuming 8\n",
58
+                                      avctx->bits_per_coded_sample);
59
+    case 0:
60
+        avctx->bits_per_coded_sample = 8;
61
+        break;
62
+    }
63
+
64
+    c->band[0].scale_factor = 8;
65
+    c->band[1].scale_factor = 2;
66
+    c->prev_samples_pos = 22;
67
+
68
+    if (avctx->lowres)
69
+        avctx->sample_rate /= 2;
70
+
71
+    return 0;
72
+}
73
+
74
+static const int16_t low_inv_quant5[32] = {
75
+     -35,   -35, -2919, -2195, -1765, -1458, -1219, -1023,
76
+    -858,  -714,  -587,  -473,  -370,  -276,  -190,  -110,
77
+    2919,  2195,  1765,  1458,  1219,  1023,   858,   714,
78
+     587,   473,   370,   276,   190,   110,    35,   -35
79
+};
80
+
81
+static const int16_t *low_inv_quants[3] = { ff_g722_low_inv_quant6,
82
+                                                    low_inv_quant5,
83
+                                            ff_g722_low_inv_quant4 };
84
+
85
+static int g722_decode_frame(AVCodecContext *avctx, void *data,
86
+                             int *data_size, AVPacket *avpkt)
87
+{
88
+    G722Context *c = avctx->priv_data;
89
+    int16_t *out_buf = data;
90
+    int j, out_len = 0;
91
+    const int skip = 8 - avctx->bits_per_coded_sample;
92
+    const int16_t *quantizer_table = low_inv_quants[skip];
93
+    GetBitContext gb;
94
+
95
+    init_get_bits(&gb, avpkt->data, avpkt->size * 8);
96
+
97
+    for (j = 0; j < avpkt->size; j++) {
98
+        int ilow, ihigh, rlow;
99
+
100
+        ihigh = get_bits(&gb, 2);
101
+        ilow = get_bits(&gb, 6 - skip);
102
+        skip_bits(&gb, skip);
103
+
104
+        rlow = av_clip((c->band[0].scale_factor * quantizer_table[ilow] >> 10)
105
+                      + c->band[0].s_predictor, -16384, 16383);
106
+
107
+        ff_g722_update_low_predictor(&c->band[0], ilow >> (2 - skip));
108
+
109
+        if (!avctx->lowres) {
110
+            const int dhigh = c->band[1].scale_factor *
111
+                              ff_g722_high_inv_quant[ihigh] >> 10;
112
+            const int rhigh = av_clip(dhigh + c->band[1].s_predictor,
113
+                                      -16384, 16383);
114
+            int xout1, xout2;
115
+
116
+            ff_g722_update_high_predictor(&c->band[1], dhigh, ihigh);
117
+
118
+            c->prev_samples[c->prev_samples_pos++] = rlow + rhigh;
119
+            c->prev_samples[c->prev_samples_pos++] = rlow - rhigh;
120
+            ff_g722_apply_qmf(c->prev_samples + c->prev_samples_pos - 24,
121
+                              &xout1, &xout2);
122
+            out_buf[out_len++] = av_clip_int16(xout1 >> 12);
123
+            out_buf[out_len++] = av_clip_int16(xout2 >> 12);
124
+            if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
125
+                memmove(c->prev_samples,
126
+                        c->prev_samples + c->prev_samples_pos - 22,
127
+                        22 * sizeof(c->prev_samples[0]));
128
+                c->prev_samples_pos = 22;
129
+            }
130
+        } else
131
+            out_buf[out_len++] = rlow;
132
+    }
133
+    *data_size = out_len << 1;
134
+    return avpkt->size;
135
+}
136
+
137
+AVCodec ff_adpcm_g722_decoder = {
138
+    .name           = "g722",
139
+    .type           = AVMEDIA_TYPE_AUDIO,
140
+    .id             = CODEC_ID_ADPCM_G722,
141
+    .priv_data_size = sizeof(G722Context),
142
+    .init           = g722_decode_init,
143
+    .decode         = g722_decode_frame,
144
+    .long_name      = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
145
+    .max_lowres     = 1,
146
+};
0 147
new file mode 100644
... ...
@@ -0,0 +1,311 @@
0
+/*
1
+ * Copyright (c) CMU 1993 Computer Science, Speech Group
2
+ *                        Chengxiang Lu and Alex Hauptmann
3
+ * Copyright (c) 2005 Steve Underwood <steveu at coppice.org>
4
+ * Copyright (c) 2009 Kenan Gillet
5
+ * Copyright (c) 2010 Martin Storsjo
6
+ *
7
+ * This file is part of Libav.
8
+ *
9
+ * Libav is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU Lesser General Public
11
+ * License as published by the Free Software Foundation; either
12
+ * version 2.1 of the License, or (at your option) any later version.
13
+ *
14
+ * Libav is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
+ * Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with Libav; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ */
23
+
24
+/**
25
+ * @file
26
+ * G.722 ADPCM audio encoder
27
+ */
28
+
29
+#include "avcodec.h"
30
+#include "g722.h"
31
+
32
+#define FREEZE_INTERVAL 128
33
+
34
+static av_cold int g722_encode_init(AVCodecContext * avctx)
35
+{
36
+    G722Context *c = avctx->priv_data;
37
+
38
+    if (avctx->channels != 1) {
39
+        av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
40
+        return AVERROR_INVALIDDATA;
41
+    }
42
+
43
+    c->band[0].scale_factor = 8;
44
+    c->band[1].scale_factor = 2;
45
+    c->prev_samples_pos = 22;
46
+
47
+    if (avctx->trellis) {
48
+        int frontier = 1 << avctx->trellis;
49
+        int max_paths = frontier * FREEZE_INTERVAL;
50
+        int i;
51
+        for (i = 0; i < 2; i++) {
52
+            c->paths[i] = av_mallocz(max_paths * sizeof(**c->paths));
53
+            c->node_buf[i] = av_mallocz(2 * frontier * sizeof(**c->node_buf));
54
+            c->nodep_buf[i] = av_mallocz(2 * frontier * sizeof(**c->nodep_buf));
55
+        }
56
+    }
57
+
58
+    return 0;
59
+}
60
+
61
+static av_cold int g722_encode_close(AVCodecContext *avctx)
62
+{
63
+    G722Context *c = avctx->priv_data;
64
+    int i;
65
+    for (i = 0; i < 2; i++) {
66
+        av_freep(&c->paths[i]);
67
+        av_freep(&c->node_buf[i]);
68
+        av_freep(&c->nodep_buf[i]);
69
+    }
70
+    return 0;
71
+}
72
+
73
+static const int16_t low_quant[33] = {
74
+      35,   72,  110,  150,  190,  233,  276,  323,
75
+     370,  422,  473,  530,  587,  650,  714,  786,
76
+     858,  940, 1023, 1121, 1219, 1339, 1458, 1612,
77
+    1765, 1980, 2195, 2557, 2919
78
+};
79
+
80
+static inline void filter_samples(G722Context *c, const int16_t *samples,
81
+                                  int *xlow, int *xhigh)
82
+{
83
+    int xout1, xout2;
84
+    c->prev_samples[c->prev_samples_pos++] = samples[0];
85
+    c->prev_samples[c->prev_samples_pos++] = samples[1];
86
+    ff_g722_apply_qmf(c->prev_samples + c->prev_samples_pos - 24, &xout1, &xout2);
87
+    *xlow  = xout1 + xout2 >> 13;
88
+    *xhigh = xout1 - xout2 >> 13;
89
+    if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
90
+        memmove(c->prev_samples,
91
+                c->prev_samples + c->prev_samples_pos - 22,
92
+                22 * sizeof(c->prev_samples[0]));
93
+        c->prev_samples_pos = 22;
94
+    }
95
+}
96
+
97
+static inline int encode_high(const struct G722Band *state, int xhigh)
98
+{
99
+    int diff = av_clip_int16(xhigh - state->s_predictor);
100
+    int pred = 141 * state->scale_factor >> 8;
101
+           /* = diff >= 0 ? (diff < pred) + 2 : diff >= -pred */
102
+    return ((diff ^ (diff >> (sizeof(diff)*8-1))) < pred) + 2*(diff >= 0);
103
+}
104
+
105
+static inline int encode_low(const struct G722Band* state, int xlow)
106
+{
107
+    int diff  = av_clip_int16(xlow - state->s_predictor);
108
+           /* = diff >= 0 ? diff : -(diff + 1) */
109
+    int limit = diff ^ (diff >> (sizeof(diff)*8-1));
110
+    int i = 0;
111
+    limit = limit + 1 << 10;
112
+    if (limit > low_quant[8] * state->scale_factor)
113
+        i = 9;
114
+    while (i < 29 && limit > low_quant[i] * state->scale_factor)
115
+        i++;
116
+    return (diff < 0 ? (i < 2 ? 63 : 33) : 61) - i;
117
+}
118
+
119
+static int g722_encode_trellis(AVCodecContext *avctx,
120
+                               uint8_t *dst, int buf_size, void *data)
121
+{
122
+    G722Context *c = avctx->priv_data;
123
+    const int16_t *samples = data;
124
+    int i, j, k;
125
+    int frontier = 1 << avctx->trellis;
126
+    struct TrellisNode **nodes[2];
127
+    struct TrellisNode **nodes_next[2];
128
+    int pathn[2] = {0, 0}, froze = -1;
129
+    struct TrellisPath *p[2];
130
+
131
+    for (i = 0; i < 2; i++) {
132
+        nodes[i] = c->nodep_buf[i];
133
+        nodes_next[i] = c->nodep_buf[i] + frontier;
134
+        memset(c->nodep_buf[i], 0, 2 * frontier * sizeof(*c->nodep_buf));
135
+        nodes[i][0] = c->node_buf[i] + frontier;
136
+        nodes[i][0]->ssd = 0;
137
+        nodes[i][0]->path = 0;
138
+        nodes[i][0]->state = c->band[i];
139
+    }
140
+
141
+    for (i = 0; i < buf_size >> 1; i++) {
142
+        int xlow, xhigh;
143
+        struct TrellisNode *next[2];
144
+        int heap_pos[2] = {0, 0};
145
+
146
+        for (j = 0; j < 2; j++) {
147
+            next[j] = c->node_buf[j] + frontier*(i & 1);
148
+            memset(nodes_next[j], 0, frontier * sizeof(**nodes_next));
149
+        }
150
+
151
+        filter_samples(c, &samples[2*i], &xlow, &xhigh);
152
+
153
+        for (j = 0; j < frontier && nodes[0][j]; j++) {
154
+            /* Only k >> 2 affects the future adaptive state, therefore testing
155
+             * small steps that don't change k >> 2 is useless, the orignal
156
+             * value from encode_low is better than them. Since we step k
157
+             * in steps of 4, make sure range is a multiple of 4, so that
158
+             * we don't miss the original value from encode_low. */
159
+            int range = j < frontier/2 ? 4 : 0;
160
+            struct TrellisNode *cur_node = nodes[0][j];
161
+
162
+            int ilow = encode_low(&cur_node->state, xlow);
163
+
164
+            for (k = ilow - range; k <= ilow + range && k <= 63; k += 4) {
165
+                int decoded, dec_diff, pos;
166
+                uint32_t ssd;
167
+                struct TrellisNode* node;
168
+
169
+                if (k < 0)
170
+                    continue;
171
+
172
+                decoded = av_clip((cur_node->state.scale_factor *
173
+                                  ff_g722_low_inv_quant6[k] >> 10)
174
+                                + cur_node->state.s_predictor, -16384, 16383);
175
+                dec_diff = xlow - decoded;
176
+
177
+#define STORE_NODE(index, UPDATE, VALUE)\
178
+                ssd = cur_node->ssd + dec_diff*dec_diff;\
179
+                /* Check for wraparound. Using 64 bit ssd counters would \
180
+                 * be simpler, but is slower on x86 32 bit. */\
181
+                if (ssd < cur_node->ssd)\
182
+                    continue;\
183
+                if (heap_pos[index] < frontier) {\
184
+                    pos = heap_pos[index]++;\
185
+                    assert(pathn[index] < FREEZE_INTERVAL * frontier);\
186
+                    node = nodes_next[index][pos] = next[index]++;\
187
+                    node->path = pathn[index]++;\
188
+                } else {\
189
+                    /* Try to replace one of the leaf nodes with the new \
190
+                     * one, but not always testing the same leaf position */\
191
+                    pos = (frontier>>1) + (heap_pos[index] & ((frontier>>1) - 1));\
192
+                    if (ssd >= nodes_next[index][pos]->ssd)\
193
+                        continue;\
194
+                    heap_pos[index]++;\
195
+                    node = nodes_next[index][pos];\
196
+                }\
197
+                node->ssd = ssd;\
198
+                node->state = cur_node->state;\
199
+                UPDATE;\
200
+                c->paths[index][node->path].value = VALUE;\
201
+                c->paths[index][node->path].prev = cur_node->path;\
202
+                /* Sift the newly inserted node up in the heap to restore \
203
+                 * the heap property */\
204
+                while (pos > 0) {\
205
+                    int parent = (pos - 1) >> 1;\
206
+                    if (nodes_next[index][parent]->ssd <= ssd)\
207
+                        break;\
208
+                    FFSWAP(struct TrellisNode*, nodes_next[index][parent],\
209
+                                                nodes_next[index][pos]);\
210
+                    pos = parent;\
211
+                }
212
+                STORE_NODE(0, ff_g722_update_low_predictor(&node->state, k >> 2), k);
213
+            }
214
+        }
215
+
216
+        for (j = 0; j < frontier && nodes[1][j]; j++) {
217
+            int ihigh;
218
+            struct TrellisNode *cur_node = nodes[1][j];
219
+
220
+            /* We don't try to get any initial guess for ihigh via
221
+             * encode_high - since there's only 4 possible values, test
222
+             * them all. Testing all of these gives a much, much larger
223
+             * gain than testing a larger range around ilow. */
224
+            for (ihigh = 0; ihigh < 4; ihigh++) {
225
+                int dhigh, decoded, dec_diff, pos;
226
+                uint32_t ssd;
227
+                struct TrellisNode* node;
228
+
229
+                dhigh = cur_node->state.scale_factor *
230
+                        ff_g722_high_inv_quant[ihigh] >> 10;
231
+                decoded = av_clip(dhigh + cur_node->state.s_predictor,
232
+                                  -16384, 16383);
233
+                dec_diff = xhigh - decoded;
234
+
235
+                STORE_NODE(1, ff_g722_update_high_predictor(&node->state, dhigh, ihigh), ihigh);
236
+            }
237
+        }
238
+
239
+        for (j = 0; j < 2; j++) {
240
+            FFSWAP(struct TrellisNode**, nodes[j], nodes_next[j]);
241
+
242
+            if (nodes[j][0]->ssd > (1 << 16)) {
243
+                for (k = 1; k < frontier && nodes[j][k]; k++)
244
+                    nodes[j][k]->ssd -= nodes[j][0]->ssd;
245
+                nodes[j][0]->ssd = 0;
246
+            }
247
+        }
248
+
249
+        if (i == froze + FREEZE_INTERVAL) {
250
+            p[0] = &c->paths[0][nodes[0][0]->path];
251
+            p[1] = &c->paths[1][nodes[1][0]->path];
252
+            for (j = i; j > froze; j--) {
253
+                dst[j] = p[1]->value << 6 | p[0]->value;
254
+                p[0] = &c->paths[0][p[0]->prev];
255
+                p[1] = &c->paths[1][p[1]->prev];
256
+            }
257
+            froze = i;
258
+            pathn[0] = pathn[1] = 0;
259
+            memset(nodes[0] + 1, 0, (frontier - 1)*sizeof(**nodes));
260
+            memset(nodes[1] + 1, 0, (frontier - 1)*sizeof(**nodes));
261
+        }
262
+    }
263
+
264
+    p[0] = &c->paths[0][nodes[0][0]->path];
265
+    p[1] = &c->paths[1][nodes[1][0]->path];
266
+    for (j = i; j > froze; j--) {
267
+        dst[j] = p[1]->value << 6 | p[0]->value;
268
+        p[0] = &c->paths[0][p[0]->prev];
269
+        p[1] = &c->paths[1][p[1]->prev];
270
+    }
271
+    c->band[0] = nodes[0][0]->state;
272
+    c->band[1] = nodes[1][0]->state;
273
+
274
+    return i;
275
+}
276
+
277
+static int g722_encode_frame(AVCodecContext *avctx,
278
+                             uint8_t *dst, int buf_size, void *data)
279
+{
280
+    G722Context *c = avctx->priv_data;
281
+    const int16_t *samples = data;
282
+    int i;
283
+
284
+    if (avctx->trellis)
285
+        return g722_encode_trellis(avctx, dst, buf_size, data);
286
+
287
+    for (i = 0; i < buf_size >> 1; i++) {
288
+        int xlow, xhigh, ihigh, ilow;
289
+        filter_samples(c, &samples[2*i], &xlow, &xhigh);
290
+        ihigh = encode_high(&c->band[1], xhigh);
291
+        ilow  = encode_low(&c->band[0], xlow);
292
+        ff_g722_update_high_predictor(&c->band[1], c->band[1].scale_factor *
293
+                                      ff_g722_high_inv_quant[ihigh] >> 10, ihigh);
294
+        ff_g722_update_low_predictor(&c->band[0], ilow >> 2);
295
+        *dst++ = ihigh << 6 | ilow;
296
+    }
297
+    return i;
298
+}
299
+
300
+AVCodec ff_adpcm_g722_encoder = {
301
+    .name           = "g722",
302
+    .type           = AVMEDIA_TYPE_AUDIO,
303
+    .id             = CODEC_ID_ADPCM_G722,
304
+    .priv_data_size = sizeof(G722Context),
305
+    .init           = g722_encode_init,
306
+    .close          = g722_encode_close,
307
+    .encode         = g722_encode_frame,
308
+    .long_name      = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
309
+    .sample_fmts    = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
310
+};
... ...
@@ -200,48 +200,6 @@ static inline int get_p_cbp(MpegEncContext * s,
200 200
     return cbp;
201 201
 }
202 202
 
203
-static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64],
204
-                            int motion_x, int motion_y, int mb_type){
205
-    int cbp=0, i;
206
-
207
-    if(s->flags & CODEC_FLAG_CBP_RD){
208
-        int score=0;
209
-        const int lambda= s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
210
-
211
-        for(i=0; i<6; i++){
212
-            if(s->coded_score[i] < 0){
213
-                score += s->coded_score[i];
214
-                cbp |= 1 << (5 - i);
215
-            }
216
-        }
217
-
218
-        if(cbp){
219
-            int zero_score= -6;
220
-            if ((motion_x | motion_y | s->dquant | mb_type) == 0){
221
-                zero_score-= 4; //2*MV + mb_type + cbp bit
222
-            }
223
-
224
-            zero_score*= lambda;
225
-            if(zero_score <= score){
226
-                cbp=0;
227
-            }
228
-        }
229
-
230
-        for (i = 0; i < 6; i++) {
231
-            if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i))&1)==0 ){
232
-                s->block_last_index[i]= -1;
233
-                s->dsp.clear_block(s->block[i]);
234
-            }
235
-        }
236
-    }else{
237
-        for (i = 0; i < 6; i++) {
238
-            if (s->block_last_index[i] >= 0)
239
-                cbp |= 1 << (5 - i);
240
-        }
241
-    }
242
-    return cbp;
243
-}
244
-
245 203
 static inline void memsetw(short *tab, int val, int n)
246 204
 {
247 205
     int i;
... ...
@@ -1060,7 +1060,6 @@ static av_cold void common_init(H264Context *h){
1060 1060
 
1061 1061
     h->dequant_coeff_pps= -1;
1062 1062
     s->unrestricted_mv=1;
1063
-    s->decode=1; //FIXME
1064 1063
 
1065 1064
     s->dsp.dct_bits = 16;
1066 1065
     dsputil_init(&s->dsp, s->avctx); // needed so that idct permutation is known early
... ...
@@ -735,8 +735,9 @@ static void exchange_uv(MpegEncContext *s)
735 735
 #define MT_16X8  2
736 736
 #define MT_DMV   3
737 737
 
738
-static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
738
+static int mpeg_decode_mb(Mpeg1Context *s1, DCTELEM block[12][64])
739 739
 {
740
+    MpegEncContext *s = &s1->mpeg_enc_ctx;
740 741
     int i, j, k, cbp, val, mb_type, motion_type;
741 742
     const int mb_block_count = 4 + (1 << s->chroma_format);
742 743
 
... ...
@@ -910,7 +911,7 @@ static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
910 910
                             s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
911 911
                                 mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
912 912
                             /* full_pel: only for MPEG-1 */
913
-                            if (s->full_pel[i]) {
913
+                            if (s1->full_pel[i]) {
914 914
                                 s->mv[i][0][0] <<= 1;
915 915
                                 s->mv[i][0][1] <<= 1;
916 916
                             }
... ...
@@ -1112,20 +1113,6 @@ static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
1112 1112
     return 0;
1113 1113
 }
1114 1114
 
1115
-typedef struct Mpeg1Context {
1116
-    MpegEncContext mpeg_enc_ctx;
1117
-    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1118
-    int repeat_field; /* true if we must repeat the field */
1119
-    AVPanScan pan_scan;              /**< some temporary storage for the panscan */
1120
-    int slice_count;
1121
-    int swap_uv;//indicate VCR2
1122
-    int save_aspect_info;
1123
-    int save_width, save_height, save_progressive_seq;
1124
-    AVRational frame_rate_ext;       ///< MPEG-2 specific framerate modificator
1125
-    int sync;                        ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
1126
-    int tmpgexs;
1127
-} Mpeg1Context;
1128
-
1129 1115
 static av_cold int mpeg_decode_init(AVCodecContext *avctx)
1130 1116
 {
1131 1117
     Mpeg1Context *s = avctx->priv_data;
... ...
@@ -1376,7 +1363,7 @@ static int mpeg1_decode_picture(AVCodecContext *avctx,
1376 1376
 
1377 1377
     vbv_delay = get_bits(&s->gb, 16);
1378 1378
     if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
1379
-        s->full_pel[0] = get_bits1(&s->gb);
1379
+        s1->full_pel[0] = get_bits1(&s->gb);
1380 1380
         f_code = get_bits(&s->gb, 3);
1381 1381
         if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1382 1382
             return -1;
... ...
@@ -1384,7 +1371,7 @@ static int mpeg1_decode_picture(AVCodecContext *avctx,
1384 1384
         s->mpeg_f_code[0][1] = f_code;
1385 1385
     }
1386 1386
     if (s->pict_type == AV_PICTURE_TYPE_B) {
1387
-        s->full_pel[1] = get_bits1(&s->gb);
1387
+        s1->full_pel[1] = get_bits1(&s->gb);
1388 1388
         f_code = get_bits(&s->gb, 3);
1389 1389
         if (f_code == 0 && (avctx->err_recognition & AV_EF_BITSTREAM))
1390 1390
             return -1;
... ...
@@ -1532,7 +1519,7 @@ static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
1532 1532
 {
1533 1533
     MpegEncContext *s = &s1->mpeg_enc_ctx;
1534 1534
 
1535
-    s->full_pel[0] = s->full_pel[1] = 0;
1535
+    s1->full_pel[0] = s1->full_pel[1] = 0;
1536 1536
     s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
1537 1537
     s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
1538 1538
     s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
... ...
@@ -1763,7 +1750,7 @@ static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
1763 1763
         if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
1764 1764
             ff_xvmc_init_block(s); // set s->block
1765 1765
 
1766
-        if (mpeg_decode_mb(s, s->block) < 0)
1766
+        if (mpeg_decode_mb(s1, s->block) < 0)
1767 1767
             return -1;
1768 1768
 
1769 1769
         if (s->current_picture.f.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
... ...
@@ -2171,7 +2158,7 @@ static void mpeg_decode_gop(AVCodecContext *avctx,
2171 2171
     time_code_seconds  = get_bits(&s->gb, 6);
2172 2172
     time_code_pictures = get_bits(&s->gb, 6);
2173 2173
 
2174
-    s->closed_gop = get_bits1(&s->gb);
2174
+    s1->closed_gop = get_bits1(&s->gb);
2175 2175
     /*broken_link indicate that after editing the
2176 2176
       reference frames of the first B-Frames after GOP I-Frame
2177 2177
       are missing (open gop)*/
... ...
@@ -2321,7 +2308,8 @@ static int decode_chunks(AVCodecContext *avctx,
2321 2321
                 }
2322 2322
 
2323 2323
                 if (CONFIG_VDPAU && uses_vdpau(avctx))
2324
-                    ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
2324
+                    ff_vdpau_mpeg_picture_complete(s, buf, buf_size, s->slice_count);
2325
+
2325 2326
 
2326 2327
                 if (slice_end(avctx, picture)) {
2327 2328
                     if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
... ...
@@ -2450,7 +2438,7 @@ static int decode_chunks(AVCodecContext *avctx,
2450 2450
                 if (s2->last_picture_ptr == NULL) {
2451 2451
                 /* Skip B-frames if we do not have reference frames and gop is not closed */
2452 2452
                     if (s2->pict_type == AV_PICTURE_TYPE_B) {
2453
-                        if (!s2->closed_gop)
2453
+                        if (!s->closed_gop)
2454 2454
                             break;
2455 2455
                     }
2456 2456
                 }
... ...
@@ -2535,6 +2523,7 @@ static void flush(AVCodecContext *avctx)
2535 2535
     Mpeg1Context *s = avctx->priv_data;
2536 2536
 
2537 2537
     s->sync=0;
2538
+    s->closed_gop = 0;
2538 2539
 
2539 2540
     ff_mpeg_flush(avctx);
2540 2541
 }
... ...
@@ -30,6 +30,22 @@
30 30
 extern VLC ff_dc_lum_vlc;
31 31
 extern VLC ff_dc_chroma_vlc;
32 32
 
33
+typedef struct Mpeg1Context {
34
+    MpegEncContext mpeg_enc_ctx;
35
+    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
36
+    int repeat_field; /* true if we must repeat the field */
37
+    AVPanScan pan_scan;              /**< some temporary storage for the panscan */
38
+    int slice_count;
39
+    int swap_uv;//indicate VCR2
40
+    int save_aspect_info;
41
+    int save_width, save_height, save_progressive_seq;
42
+    AVRational frame_rate_ext;       ///< MPEG-2 specific framerate modificator
43
+    int sync;                        ///< Did we reach a sync point like a GOP/SEQ/KEYFrame?
44
+    int full_pel[2];
45
+    int closed_gop;                  ///< GOP is closed
46
+    int tmpgexs;
47
+} Mpeg1Context;
48
+
33 49
 extern uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
34 50
 
35 51
 void ff_mpeg12_common_init(MpegEncContext *s);
... ...
@@ -425,6 +425,46 @@ static inline void mpeg4_encode_blocks(MpegEncContext * s, DCTELEM block[6][64],
425 425
     }
426 426
 }
427 427
 
428
+static inline int get_b_cbp(MpegEncContext * s, DCTELEM block[6][64],
429
+                            int motion_x, int motion_y, int mb_type)
430
+{
431
+    int cbp = 0, i;
432
+
433
+    if (s->flags & CODEC_FLAG_CBP_RD) {
434
+        int score = 0;
435
+        const int lambda = s->lambda2 >> (FF_LAMBDA_SHIFT - 6);
436
+
437
+        for (i = 0; i < 6; i++)
438
+            if (s->coded_score[i] < 0) {
439
+                score += s->coded_score[i];
440
+                cbp   |= 1 << (5 - i);
441
+            }
442
+
443
+        if (cbp) {
444
+            int zero_score = -6;
445
+            if ((motion_x | motion_y | s->dquant | mb_type) == 0)
446
+                zero_score -= 4; //2*MV + mb_type + cbp bit
447
+
448
+            zero_score *= lambda;
449
+            if (zero_score <= score)
450
+                cbp = 0;
451
+        }
452
+
453
+        for (i = 0; i < 6; i++) {
454
+            if (s->block_last_index[i] >= 0 && ((cbp >> (5 - i)) & 1) == 0) {
455
+                s->block_last_index[i] = -1;
456
+                s->dsp.clear_block(s->block[i]);
457
+            }
458
+        }
459
+    } else {
460
+        for (i = 0; i < 6; i++) {
461
+            if (s->block_last_index[i] >= 0)
462
+                cbp |= 1 << (5 - i);
463
+        }
464
+    }
465
+    return cbp;
466
+}
467
+
428 468
 //FIXME this is duplicated to h263.c
429 469
 static const int dquant_code[5]= {1,0,9,2,3};
430 470
 
... ...
@@ -1213,7 +1253,6 @@ static av_cold int encode_init(AVCodecContext *avctx)
1213 1213
     s->inter_ac_vlc_length     = uni_mpeg4_inter_rl_len;
1214 1214
     s->inter_ac_vlc_last_length= uni_mpeg4_inter_rl_len + 128*64;
1215 1215
     s->luma_dc_vlc_length= uni_DCtab_lum_len;
1216
-    s->chroma_dc_vlc_length= uni_DCtab_chrom_len;
1217 1216
     s->ac_esc_length= 7+2+1+6+1+12+1;
1218 1217
     s->y_dc_scale_table= ff_mpeg4_y_dc_scale_table;
1219 1218
     s->c_dc_scale_table= ff_mpeg4_c_dc_scale_table;
... ...
@@ -2425,7 +2425,6 @@ void ff_mpeg_flush(AVCodecContext *avctx){
2425 2425
     s->current_picture_ptr = s->last_picture_ptr = s->next_picture_ptr = NULL;
2426 2426
 
2427 2427
     s->mb_x= s->mb_y= 0;
2428
-    s->closed_gop= 0;
2429 2428
 
2430 2429
     s->parse_context.state= -1;
2431 2430
     s->parse_context.frame_start_found= 0;
... ...
@@ -236,7 +236,6 @@ typedef struct MpegEncContext {
236 236
     int coded_picture_number;  ///< used to set pic->coded_picture_number, should not be used for/by anything else
237 237
     int picture_number;       //FIXME remove, unclear definition
238 238
     int picture_in_gop_number; ///< 0-> first pic in gop, ...
239
-    int b_frames_since_non_b;  ///< used for encoding, relative to not yet reordered input
240 239
     int mb_width, mb_height;   ///< number of MBs horizontally & vertically
241 240
     int mb_stride;             ///< mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11
242 241
     int b8_stride;             ///< 2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
... ...
@@ -305,7 +304,6 @@ typedef struct MpegEncContext {
305 305
     int last_dc[3];                ///< last DC values for MPEG1
306 306
     int16_t *dc_val_base;
307 307
     int16_t *dc_val[3];            ///< used for mpeg4 DC prediction, all 3 arrays must be continuous
308
-    int16_t dc_cache[4*5];
309 308
     const uint8_t *y_dc_scale_table;     ///< qscale -> y_dc_scale table
310 309
     const uint8_t *c_dc_scale_table;     ///< qscale -> c_dc_scale table
311 310
     const uint8_t *chroma_qscale_table;  ///< qscale -> chroma_qscale (h263)
... ...
@@ -333,7 +331,6 @@ typedef struct MpegEncContext {
333 333
     int *lambda_table;
334 334
     int adaptive_quant;         ///< use adaptive quantization
335 335
     int dquant;                 ///< qscale difference to prev qscale
336
-    int closed_gop;             ///< MPEG1/2 GOP is closed
337 336
     int pict_type;              ///< AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
338 337
     int last_pict_type; //FIXME removes
339 338
     int last_non_b_pict_type;   ///< used for mpeg4 gmc b-frames & ratecontrol
... ...
@@ -345,7 +342,6 @@ typedef struct MpegEncContext {
345 345
     /* motion compensation */
346 346
     int unrestricted_mv;        ///< mv can point outside of the coded picture
347 347
     int h263_long_vectors;      ///< use horrible h263v1 long vector mode
348
-    int decode;                 ///< if 0 then decoding will be skipped (for encoding b frames for example)
349 348
 
350 349
     DSPContext dsp;             ///< pointers for accelerated dsp functions
351 350
     int f_code;                 ///< forward MV resolution
... ...
@@ -440,7 +436,6 @@ typedef struct MpegEncContext {
440 440
     uint8_t *inter_ac_vlc_length;
441 441
     uint8_t *inter_ac_vlc_last_length;
442 442
     uint8_t *luma_dc_vlc_length;
443
-    uint8_t *chroma_dc_vlc_length;
444 443
 #define UNI_AC_ENC_INDEX(run,level) ((run)*128 + (level))
445 444
 
446 445
     int coded_score[8];
... ...
@@ -462,7 +457,6 @@ typedef struct MpegEncContext {
462 462
     void *opaque;              ///< private data for the user
463 463
 
464 464
     /* bit rate control */
465
-    int64_t wanted_bits;
466 465
     int64_t total_bits;
467 466
     int frame_bits;                ///< bits used for the current frame
468 467
     int next_lambda;               ///< next lambda used for retrying to encode a frame
... ...
@@ -643,7 +637,6 @@ typedef struct MpegEncContext {
643 643
     int chroma_y_shift;
644 644
 
645 645
     int progressive_frame;
646
-    int full_pel[2];
647 646
     int interlaced_dct;
648 647
     int first_slice;
649 648
     int first_field;         ///< is 1 for the first field of a field picture 0 otherwise
... ...
@@ -190,9 +190,10 @@ void ff_vdpau_h264_picture_complete(MpegEncContext *s)
190 190
     render->bitstream_buffers_used = 0;
191 191
 }
192 192
 
193
-void ff_vdpau_mpeg_picture_complete(MpegEncContext *s, const uint8_t *buf,
193
+void ff_vdpau_mpeg_picture_complete(Mpeg1Context *s1, const uint8_t *buf,
194 194
                                     int buf_size, int slice_count)
195 195
 {
196
+    MpegEncContext *s = &s1->mpeg_enc_ctx;
196 197
     struct vdpau_render_state *render, *last, *next;
197 198
     int i;
198 199
 
... ...
@@ -211,8 +212,8 @@ void ff_vdpau_mpeg_picture_complete(MpegEncContext *s, const uint8_t *buf,
211 211
     render->info.mpeg.alternate_scan             = s->alternate_scan;
212 212
     render->info.mpeg.q_scale_type               = s->q_scale_type;
213 213
     render->info.mpeg.top_field_first            = s->top_field_first;
214
-    render->info.mpeg.full_pel_forward_vector    = s->full_pel[0]; // MPEG-1 only.  Set 0 for MPEG-2
215
-    render->info.mpeg.full_pel_backward_vector   = s->full_pel[1]; // MPEG-1 only.  Set 0 for MPEG-2
214
+    render->info.mpeg.full_pel_forward_vector    = s1->full_pel[0]; // MPEG-1 only.  Set 0 for MPEG-2
215
+    render->info.mpeg.full_pel_backward_vector   = s1->full_pel[1]; // MPEG-1 only.  Set 0 for MPEG-2
216 216
     render->info.mpeg.f_code[0][0]               = s->mpeg_f_code[0][0]; // For MPEG-1 fill both horiz. & vert.
217 217
     render->info.mpeg.f_code[0][1]               = s->mpeg_f_code[0][1];
218 218
     render->info.mpeg.f_code[1][0]               = s->mpeg_f_code[1][0];
... ...
@@ -26,11 +26,12 @@
26 26
 
27 27
 #include <stdint.h>
28 28
 #include "mpegvideo.h"
29
+#include "mpeg12.h"
29 30
 
30 31
 void ff_vdpau_add_data_chunk(MpegEncContext *s, const uint8_t *buf,
31 32
                              int buf_size);
32 33
 
33
-void ff_vdpau_mpeg_picture_complete(MpegEncContext *s, const uint8_t *buf,
34
+void ff_vdpau_mpeg_picture_complete(Mpeg1Context *s1, const uint8_t *buf,
34 35
                                     int buf_size, int slice_count);
35 36
 
36 37
 void ff_vdpau_h264_picture_start(MpegEncContext *s);
... ...
@@ -966,7 +966,7 @@ static int vorbis_parse_id_hdr(vorbis_context *vc)
966 966
 
967 967
 static av_cold int vorbis_decode_init(AVCodecContext *avccontext)
968 968
 {
969
-    vorbis_context *vc = avccontext->priv_data ;
969
+    vorbis_context *vc = avccontext->priv_data;
970 970
     uint8_t *headers   = avccontext->extradata;
971 971
     int headers_len    = avccontext->extradata_size;
972 972
     uint8_t *header_start[3];
... ...
@@ -1030,7 +1030,7 @@ static av_cold int vorbis_decode_init(AVCodecContext *avccontext)
1030 1030
     avccontext->sample_rate = vc->audio_samplerate;
1031 1031
     avccontext->frame_size  = FFMIN(vc->blocksize[0], vc->blocksize[1]) >> 2;
1032 1032
 
1033
-    return 0 ;
1033
+    return 0;
1034 1034
 }
1035 1035
 
1036 1036
 // Decode audiopackets -------------------------------------------------
... ...
@@ -1608,10 +1608,10 @@ static int vorbis_decode_frame(AVCodecContext *avccontext,
1608 1608
 {
1609 1609
     const uint8_t *buf = avpkt->data;
1610 1610
     int buf_size       = avpkt->size;
1611
-    vorbis_context *vc = avccontext->priv_data ;
1611
+    vorbis_context *vc = avccontext->priv_data;
1612 1612
     GetBitContext *gb = &(vc->gb);
1613 1613
     const float *channel_ptrs[255];
1614
-    int i, len;
1614
+    int i, len, out_size;
1615 1615
 
1616 1616
     if (!buf_size)
1617 1617
         return 0;
... ...
@@ -1630,12 +1630,19 @@ static int vorbis_decode_frame(AVCodecContext *avccontext,
1630 1630
     if (!vc->first_frame) {
1631 1631
         vc->first_frame = 1;
1632 1632
         *data_size = 0;
1633
-        return buf_size ;
1633
+        return buf_size;
1634 1634
     }
1635 1635
 
1636 1636
     av_dlog(NULL, "parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
1637 1637
             get_bits_count(gb) / 8, get_bits_count(gb) % 8, len);
1638 1638
 
1639
+    out_size = len * vc->audio_channels *
1640
+               av_get_bytes_per_sample(avccontext->sample_fmt);
1641
+    if (*data_size < out_size) {
1642
+        av_log(avccontext, AV_LOG_ERROR, "output buffer is too small\n");
1643
+        return AVERROR(EINVAL);
1644
+    }
1645
+
1639 1646
     if (vc->audio_channels > 8) {
1640 1647
         for (i = 0; i < vc->audio_channels; i++)
1641 1648
             channel_ptrs[i] = vc->channel_floors + i * len;
... ...
@@ -1651,10 +1658,9 @@ static int vorbis_decode_frame(AVCodecContext *avccontext,
1651 1651
         vc->fmt_conv.float_to_int16_interleave(data, channel_ptrs, len,
1652 1652
                                                vc->audio_channels);
1653 1653
 
1654
-    *data_size = len * vc->audio_channels *
1655
-                 av_get_bytes_per_sample(avccontext->sample_fmt);
1654
+    *data_size = out_size;
1656 1655
 
1657
-    return buf_size ;
1656
+    return buf_size;
1658 1657
 }
1659 1658
 
1660 1659
 // Close decoder
... ...
@@ -1665,7 +1671,7 @@ static av_cold int vorbis_decode_close(AVCodecContext *avccontext)
1665 1665
 
1666 1666
     vorbis_free(vc);
1667 1667
 
1668
-    return 0 ;
1668
+    return 0;
1669 1669
 }
1670 1670
 
1671 1671
 AVCodec ff_vorbis_decoder = {
... ...
@@ -808,15 +808,13 @@ static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
808 808
     }
809 809
     s->frame_flags = AV_RL32(buf); buf += 4;
810 810
     if(s->frame_flags&0x80){
811
-        bpp = sizeof(float);
812 811
         avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
813 812
     } else if((s->frame_flags&0x03) <= 1){
814
-        bpp = 2;
815 813
         avctx->sample_fmt = AV_SAMPLE_FMT_S16;
816 814
     } else {
817
-        bpp = 4;
818 815
         avctx->sample_fmt = AV_SAMPLE_FMT_S32;
819 816
     }
817
+    bpp = av_get_bytes_per_sample(avctx->sample_fmt);
820 818
     samples = (uint8_t*)samples + bpp * wc->ch_offset;
821 819
 
822 820
     s->stereo = !(s->frame_flags & WV_MONO);
... ...
@@ -147,7 +147,7 @@ OBJS-$(CONFIG_MOV_MUXER)                 += movenc.o riff.o isom.o avc.o \
147 147
                                             movenchint.o rtpenc_chain.o
148 148
 OBJS-$(CONFIG_MP2_MUXER)                 += mp3enc.o rawenc.o
149 149
 OBJS-$(CONFIG_MP3_DEMUXER)               += mp3dec.o
150
-OBJS-$(CONFIG_MP3_MUXER)                 += mp3enc.o rawenc.o
150
+OBJS-$(CONFIG_MP3_MUXER)                 += mp3enc.o rawenc.o id3v2enc.o
151 151
 OBJS-$(CONFIG_MPC_DEMUXER)               += mpc.o apetag.o
152 152
 OBJS-$(CONFIG_MPC8_DEMUXER)              += mpc8.o
153 153
 OBJS-$(CONFIG_MPEG1SYSTEM_MUXER)         += mpegenc.o
... ...
@@ -87,6 +87,14 @@ void ff_id3v2_read(AVFormatContext *s, const char *magic);
87 87
 void ff_id3v2_read_all(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta);
88 88
 
89 89
 /**
90
+ * Write an ID3v2 tag.
91
+ * @param id3v2_version Subversion of ID3v2; supported values are 3 and 4
92
+ * @param magic magic bytes to identify the header
93
+ * If in doubt, use ID3v2_DEFAULT_MAGIC.
94
+ */
95
+int ff_id3v2_write(struct AVFormatContext *s, int id3v2_version, const char *magic);
96
+
97
+/**
90 98
  * Free memory allocated parsing special (non-text) metadata.
91 99
  * @param extra_meta Pointer to a pointer to the head of a ID3v2ExtraMeta list, *extra_meta is set to NULL.
92 100
  */
93 101
new file mode 100644
... ...
@@ -0,0 +1,146 @@
0
+/*
1
+ * ID3v2 header writer
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include <stdint.h>
21
+
22
+#include "libavutil/dict.h"
23
+#include "libavutil/intreadwrite.h"
24
+#include "avformat.h"
25
+#include "avio.h"
26
+#include "id3v2.h"
27
+
28
+static void id3v2_put_size(AVFormatContext *s, int size)
29
+{
30
+    avio_w8(s->pb, size >> 21 & 0x7f);
31
+    avio_w8(s->pb, size >> 14 & 0x7f);
32
+    avio_w8(s->pb, size >> 7  & 0x7f);
33
+    avio_w8(s->pb, size       & 0x7f);
34
+}
35
+
36
+static int string_is_ascii(const uint8_t *str)
37
+{
38
+    while (*str && *str < 128) str++;
39
+    return !*str;
40
+}
41
+
42
+/**
43
+ * Write a text frame with one (normal frames) or two (TXXX frames) strings
44
+ * according to encoding (only UTF-8 or UTF-16+BOM supported).
45
+ * @return number of bytes written or a negative error code.
46
+ */
47
+static int id3v2_put_ttag(AVFormatContext *s, const char *str1, const char *str2,
48
+                          uint32_t tag, enum ID3v2Encoding enc)
49
+{
50
+    int len;
51
+    uint8_t *pb;
52
+    int (*put)(AVIOContext*, const char*);
53
+    AVIOContext *dyn_buf;
54
+    if (avio_open_dyn_buf(&dyn_buf) < 0)
55
+        return AVERROR(ENOMEM);
56
+
57
+    /* check if the strings are ASCII-only and use UTF16 only if
58
+     * they're not */
59
+    if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
60
+        (!str2 || string_is_ascii(str2)))
61
+        enc = ID3v2_ENCODING_ISO8859;
62
+
63
+    avio_w8(dyn_buf, enc);
64
+    if (enc == ID3v2_ENCODING_UTF16BOM) {
65
+        avio_wl16(dyn_buf, 0xFEFF);      /* BOM */
66
+        put = avio_put_str16le;
67
+    } else
68
+        put = avio_put_str;
69
+
70
+    put(dyn_buf, str1);
71
+    if (str2)
72
+        put(dyn_buf, str2);
73
+    len = avio_close_dyn_buf(dyn_buf, &pb);
74
+
75
+    avio_wb32(s->pb, tag);
76
+    id3v2_put_size(s, len);
77
+    avio_wb16(s->pb, 0);
78
+    avio_write(s->pb, pb, len);
79
+
80
+    av_freep(&pb);
81
+    return len + ID3v2_HEADER_SIZE;
82
+}
83
+
84
+static int id3v2_check_write_tag(AVFormatContext *s, AVDictionaryEntry *t, const char table[][4],
85
+                                 enum ID3v2Encoding enc)
86
+{
87
+    uint32_t tag;
88
+    int i;
89
+
90
+    if (t->key[0] != 'T' || strlen(t->key) != 4)
91
+        return -1;
92
+    tag = AV_RB32(t->key);
93
+    for (i = 0; *table[i]; i++)
94
+        if (tag == AV_RB32(table[i]))
95
+            return id3v2_put_ttag(s, t->value, NULL, tag, enc);
96
+    return -1;
97
+}
98
+
99
+int ff_id3v2_write(struct AVFormatContext *s, int id3v2_version,
100
+                   const char *magic)
101
+{
102
+    int64_t size_pos, cur_pos;
103
+    AVDictionaryEntry *t = NULL;
104
+
105
+    int totlen = 0, enc = id3v2_version == 3 ? ID3v2_ENCODING_UTF16BOM :
106
+                                               ID3v2_ENCODING_UTF8;
107
+
108
+
109
+    avio_wb32(s->pb, MKBETAG(magic[0], magic[1], magic[2], id3v2_version));
110
+    avio_w8(s->pb, 0);
111
+    avio_w8(s->pb, 0); /* flags */
112
+
113
+    /* reserve space for size */
114
+    size_pos = avio_tell(s->pb);
115
+    avio_wb32(s->pb, 0);
116
+
117
+    ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
118
+    if (id3v2_version == 4)
119
+        ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
120
+
121
+    while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
122
+        int ret;
123
+
124
+        if ((ret = id3v2_check_write_tag(s, t, ff_id3v2_tags, enc)) > 0) {
125
+            totlen += ret;
126
+            continue;
127
+        }
128
+        if ((ret = id3v2_check_write_tag(s, t, id3v2_version == 3 ?
129
+                                               ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
130
+            totlen += ret;
131
+            continue;
132
+        }
133
+
134
+        /* unknown tag, write as TXXX frame */
135
+        if ((ret = id3v2_put_ttag(s, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
136
+            return ret;
137
+        totlen += ret;
138
+    }
139
+
140
+    cur_pos = avio_tell(s->pb);
141
+    avio_seek(s->pb, size_pos, SEEK_SET);
142
+    id3v2_put_size(s, totlen);
143
+    avio_seek(s->pb, cur_pos, SEEK_SET);
144
+    return 0;
145
+}
... ...
@@ -78,66 +78,9 @@ static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
78 78
     return count;
79 79
 }
80 80
 
81
-/* simple formats */
82
-
83
-static void id3v2_put_size(AVFormatContext *s, int size)
84
-{
85
-    avio_w8(s->pb, size >> 21 & 0x7f);
86
-    avio_w8(s->pb, size >> 14 & 0x7f);
87
-    avio_w8(s->pb, size >> 7  & 0x7f);
88
-    avio_w8(s->pb, size       & 0x7f);
89
-}
90
-
91
-static int string_is_ascii(const uint8_t *str)
92
-{
93
-    while (*str && *str < 128) str++;
94
-    return !*str;
95
-}
96
-
97
-/**
98
- * Write a text frame with one (normal frames) or two (TXXX frames) strings
99
- * according to encoding (only UTF-8 or UTF-16+BOM supported).
100
- * @return number of bytes written or a negative error code.
101
- */
102
-static int id3v2_put_ttag(AVFormatContext *s, const char *str1, const char *str2,
103
-                          uint32_t tag, enum ID3v2Encoding enc)
104
-{
105
-    int len;
106
-    uint8_t *pb;
107
-    int (*put)(AVIOContext*, const char*);
108
-    AVIOContext *dyn_buf;
109
-    if (avio_open_dyn_buf(&dyn_buf) < 0)
110
-        return AVERROR(ENOMEM);
111
-
112
-    /* check if the strings are ASCII-only and use UTF16 only if
113
-     * they're not */
114
-    if (enc == ID3v2_ENCODING_UTF16BOM && string_is_ascii(str1) &&
115
-        (!str2 || string_is_ascii(str2)))
116
-        enc = ID3v2_ENCODING_ISO8859;
117
-
118
-    avio_w8(dyn_buf, enc);
119
-    if (enc == ID3v2_ENCODING_UTF16BOM) {
120
-        avio_wl16(dyn_buf, 0xFEFF);      /* BOM */
121
-        put = avio_put_str16le;
122
-    } else
123
-        put = avio_put_str;
124
-
125
-    put(dyn_buf, str1);
126
-    if (str2)
127
-        put(dyn_buf, str2);
128
-    len = avio_close_dyn_buf(dyn_buf, &pb);
129
-
130
-    avio_wb32(s->pb, tag);
131
-    id3v2_put_size(s, len);
132
-    avio_wb16(s->pb, 0);
133
-    avio_write(s->pb, pb, len);
134
-
135
-    av_freep(&pb);
136
-    return len + ID3v2_HEADER_SIZE;
137
-}
138
-
139 81
 #define VBR_NUM_BAGS 400
140 82
 #define VBR_TOC_SIZE 100
83
+
141 84
 typedef struct MP3Context {
142 85
     const AVClass *class;
143 86
     int id3v2_version;
... ...
@@ -204,21 +147,6 @@ static const AVClass mp3_muxer_class = {
204 204
     .version        = LIBAVUTIL_VERSION_INT,
205 205
 };
206 206
 
207
-static int id3v2_check_write_tag(AVFormatContext *s, AVDictionaryEntry *t, const char table[][4],
208
-                                 enum ID3v2Encoding enc)
209
-{
210
-    uint32_t tag;
211
-    int i;
212
-
213
-    if (t->key[0] != 'T' || strlen(t->key) != 4)
214
-        return -1;
215
-    tag = AV_RB32(t->key);
216
-    for (i = 0; *table[i]; i++)
217
-        if (tag == AV_RB32(table[i]))
218
-            return id3v2_put_ttag(s, t->value, NULL, tag, enc);
219
-    return -1;
220
-}
221
-
222 207
 static const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
223 208
 
224 209
 /*
... ...
@@ -363,46 +291,11 @@ static void mp3_fix_xing(AVFormatContext *s)
363 363
 static int mp3_write_header(struct AVFormatContext *s)
364 364
 {
365 365
     MP3Context  *mp3 = s->priv_data;
366
-    AVDictionaryEntry *t = NULL;
367
-    int totlen = 0, enc = mp3->id3v2_version == 3 ? ID3v2_ENCODING_UTF16BOM :
368
-                                                    ID3v2_ENCODING_UTF8;
369
-    int64_t size_pos, cur_pos;
370
-
371
-    avio_wb32(s->pb, MKBETAG('I', 'D', '3', mp3->id3v2_version));
372
-    avio_w8(s->pb, 0);
373
-    avio_w8(s->pb, 0); /* flags */
374
-
375
-    /* reserve space for size */
376
-    size_pos = avio_tell(s->pb);
377
-    avio_wb32(s->pb, 0);
378
-
379
-    ff_metadata_conv(&s->metadata, ff_id3v2_34_metadata_conv, NULL);
380
-    if (mp3->id3v2_version == 4)
381
-        ff_metadata_conv(&s->metadata, ff_id3v2_4_metadata_conv, NULL);
382
-
383
-    while ((t = av_dict_get(s->metadata, "", t, AV_DICT_IGNORE_SUFFIX))) {
384
-        int ret;
366
+    int ret;
385 367
 
386
-        if ((ret = id3v2_check_write_tag(s, t, ff_id3v2_tags, enc)) > 0) {
387
-            totlen += ret;
388
-            continue;
389
-        }
390
-        if ((ret = id3v2_check_write_tag(s, t, mp3->id3v2_version == 3 ?
391
-                                               ff_id3v2_3_tags : ff_id3v2_4_tags, enc)) > 0) {
392
-            totlen += ret;
393
-            continue;
394
-        }
395
-
396
-        /* unknown tag, write as TXXX frame */
397
-        if ((ret = id3v2_put_ttag(s, t->key, t->value, MKBETAG('T', 'X', 'X', 'X'), enc)) < 0)
398
-            return ret;
399
-        totlen += ret;
400
-    }
401
-
402
-    cur_pos = avio_tell(s->pb);
403
-    avio_seek(s->pb, size_pos, SEEK_SET);
404
-    id3v2_put_size(s, totlen);
405
-    avio_seek(s->pb, cur_pos, SEEK_SET);
368
+    ret = ff_id3v2_write(s, mp3->id3v2_version, ID3v2_DEFAULT_MAGIC);
369
+    if (ret < 0)
370
+        return ret;
406 371
 
407 372
     if (s->pb->seekable)
408 373
         mp3_write_xing(s);
... ...
@@ -520,7 +520,7 @@ cglobal yuv2planeX_%2_%1, %4, 7, %3
520 520
 
521 521
     xor             r5,  r5
522 522
 
523
-.pixelloop
523
+.pixelloop:
524 524
 %assign %%i 0
525 525
     ; the rep here is for the 8bit output mmx case, where dither covers
526 526
     ; 8 pixels but we can only handle 2 pixels per register, and thus 4
... ...
@@ -543,7 +543,7 @@ cglobal yuv2planeX_%2_%1, %4, 7, %3
543 543
     mova            m2,  m1
544 544
 %endif ; %2 == 8/9/10/16
545 545
     movsx     cntr_reg,  r1m
546
-.filterloop_ %+ %%i
546
+.filterloop_ %+ %%i:
547 547
     ; input pixels
548 548
     mov             r6, [r2+gprsize*cntr_reg-2*gprsize]
549 549
 %if %2 == 16