Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master: (36 commits)
adpcmenc: Use correct frame_size for Yamaha ADPCM.
avcodec: add ff_samples_to_time_base() convenience function to internal.h
adx parser: set duration
mlp parser: set duration instead of frame_size
gsm parser: set duration
mpegaudio parser: set duration instead of frame_size
(e)ac3 parser: set duration instead of frame_size
flac parser: set duration instead of frame_size
avcodec: add duration field to AVCodecParserContext
avutil: add av_rescale_q_rnd() to allow different rounding
pnmdec: remove useless .pix_fmts
libmp3lame: support float and s32 sample formats
libmp3lame: renaming, rearrangement, alignment, and comments
libmp3lame: use the LAME default bit rate
libmp3lame: use avpriv_mpegaudio_decode_header() for output frame parsing
libmp3lame: cosmetics: remove some pointless comments
libmp3lame: convert some debugging code to av_dlog()
libmp3lame: remove outdated comment.
libmp3lame: do not set coded_frame->key_frame.
libmp3lame: improve error handling in MP3lame_encode_init()
...

Conflicts:
doc/APIchanges
libavcodec/libmp3lame.c
libavcodec/pcxenc.c
libavcodec/pnmdec.c
libavcodec/pnmenc.c
libavcodec/sgienc.c
libavcodec/utils.c
libavformat/hls.c
libavutil/avutil.h
libswscale/x86/swscale_mmx.c

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

Michael Niedermayer authored on 2012/02/21 10:49:41
Showing 34 changed files
... ...
@@ -32,6 +32,12 @@ API changes, most recent first:
32 32
 2012-01-24 - xxxxxxx - lavfi 2.60.100
33 33
   Add avfilter_graph_dump.
34 34
 
35
+2012-xx-xx - xxxxxxx - lavc 54.x.x
36
+  Add duration field to AVCodecParserContext
37
+
38
+2012-02-xx - xxxxxxx - lavu 51.23.1 - mathematics.h
39
+  Add av_rescale_q_rnd()
40
+
35 41
 2012-02-xx - xxxxxxx - lavu 51.22.1 - pixdesc.h
36 42
   Add PIX_FMT_PSEUDOPAL flag.
37 43
 
... ...
@@ -630,7 +630,7 @@ OBJS-$(CONFIG_LIBGSM_DECODER)             += libgsm.o
630 630
 OBJS-$(CONFIG_LIBGSM_ENCODER)             += libgsm.o
631 631
 OBJS-$(CONFIG_LIBGSM_MS_DECODER)          += libgsm.o
632 632
 OBJS-$(CONFIG_LIBGSM_MS_ENCODER)          += libgsm.o
633
-OBJS-$(CONFIG_LIBMP3LAME_ENCODER)         += libmp3lame.o
633
+OBJS-$(CONFIG_LIBMP3LAME_ENCODER)         += libmp3lame.o mpegaudiodecheader.o
634 634
 OBJS-$(CONFIG_LIBOPENCORE_AMRNB_DECODER)  += libopencore-amr.o
635 635
 OBJS-$(CONFIG_LIBOPENCORE_AMRNB_ENCODER)  += libopencore-amr.o
636 636
 OBJS-$(CONFIG_LIBOPENCORE_AMRWB_DECODER)  += libopencore-amr.o
... ...
@@ -93,7 +93,7 @@ get_next:
93 93
             avctx->channels = s->channels;
94 94
             avctx->channel_layout = s->channel_layout;
95 95
         }
96
-        avctx->frame_size = s->samples;
96
+        s1->duration = s->samples;
97 97
         avctx->audio_service_type = s->service_type;
98 98
     }
99 99
 
... ...
@@ -125,7 +125,7 @@ static av_cold int adpcm_encode_init(AVCodecContext *avctx)
125 125
         }
126 126
         break;
127 127
     case CODEC_ID_ADPCM_YAMAHA:
128
-        avctx->frame_size  = BLKSIZE * avctx->channels;
128
+        avctx->frame_size  = BLKSIZE * 2 / avctx->channels;
129 129
         avctx->block_align = BLKSIZE;
130 130
         break;
131 131
     case CODEC_ID_ADPCM_SWF:
... ...
@@ -80,6 +80,9 @@ static int adx_parse(AVCodecParserContext *s1,
80 80
         *poutbuf_size = 0;
81 81
         return buf_size;
82 82
     }
83
+
84
+    s1->duration = BLOCK_SAMPLES;
85
+
83 86
     *poutbuf = buf;
84 87
     *poutbuf_size = buf_size;
85 88
     return next;
... ...
@@ -4182,6 +4182,13 @@ typedef struct AVCodecParserContext {
4182 4182
      * Previous frame byte position.
4183 4183
      */
4184 4184
     int64_t last_pos;
4185
+
4186
+    /**
4187
+     * Duration of the current frame.
4188
+     * For audio, this is in units of 1 / AVCodecContext.sample_rate.
4189
+     * For all other types, this is in units of AVCodecContext.time_base.
4190
+     */
4191
+    int duration;
4185 4192
 } AVCodecParserContext;
4186 4193
 
4187 4194
 typedef struct AVCodecParser {
... ...
@@ -71,6 +71,7 @@ typedef struct FLACHeaderMarker {
71 71
 } FLACHeaderMarker;
72 72
 
73 73
 typedef struct FLACParseContext {
74
+    AVCodecParserContext *pc;      /**< parent context                        */
74 75
     AVCodecContext *avctx;         /**< codec context pointer for logging     */
75 76
     FLACHeaderMarker *headers;     /**< linked-list that starts at the first
76 77
                                         CRC-8 verified header within buffer   */
... ...
@@ -458,7 +459,7 @@ static int get_best_header(FLACParseContext* fpc, const uint8_t **poutbuf,
458 458
 
459 459
     fpc->avctx->sample_rate = header->fi.samplerate;
460 460
     fpc->avctx->channels    = header->fi.channels;
461
-    fpc->avctx->frame_size  = header->fi.blocksize;
461
+    fpc->pc->duration       = header->fi.blocksize;
462 462
     *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size,
463 463
                                         &fpc->wrap_buf,
464 464
                                         &fpc->wrap_buf_allocated_size);
... ...
@@ -484,7 +485,7 @@ static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
484 484
     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
485 485
         FLACFrameInfo fi;
486 486
         if (frame_header_is_valid(avctx, buf, &fi))
487
-            avctx->frame_size = fi.blocksize;
487
+            s->duration = fi.blocksize;
488 488
         *poutbuf      = buf;
489 489
         *poutbuf_size = buf_size;
490 490
         return buf_size;
... ...
@@ -630,8 +631,8 @@ static int flac_parse(AVCodecParserContext *s, AVCodecContext *avctx,
630 630
             av_log(avctx, AV_LOG_DEBUG, "Junk frame till offset %i\n",
631 631
                    fpc->best_header->offset);
632 632
 
633
-            /* Set frame_size to 0. It is unknown or invalid in a junk frame. */
634
-            avctx->frame_size = 0;
633
+            /* Set duration to 0. It is unknown or invalid in a junk frame. */
634
+            s->duration = 0;
635 635
             *poutbuf_size     = fpc->best_header->offset;
636 636
             *poutbuf          = flac_fifo_read_wrap(fpc, 0, *poutbuf_size,
637 637
                                                     &fpc->wrap_buf,
... ...
@@ -652,6 +653,7 @@ handle_error:
652 652
 static int flac_parse_init(AVCodecParserContext *c)
653 653
 {
654 654
     FLACParseContext *fpc = c->priv_data;
655
+    fpc->pc = c;
655 656
     /* There will generally be FLAC_MIN_HEADERS buffered in the fifo before
656 657
        it drains.  This is allocated early to avoid slow reallocation. */
657 658
     fpc->fifo_buf = av_fifo_alloc(FLAC_AVG_FRAME_SIZE * (FLAC_MIN_HEADERS + 3));
... ...
@@ -31,6 +31,7 @@
31 31
 typedef struct GSMParseContext {
32 32
     ParseContext pc;
33 33
     int block_size;
34
+    int duration;
34 35
     int remaining;
35 36
 } GSMParseContext;
36 37
 
... ...
@@ -44,8 +45,14 @@ static int gsm_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
44 44
 
45 45
     if (!s->block_size) {
46 46
         switch (avctx->codec_id) {
47
-        case CODEC_ID_GSM:    s->block_size = GSM_BLOCK_SIZE;    break;
48
-        case CODEC_ID_GSM_MS: s->block_size = GSM_MS_BLOCK_SIZE; break;
47
+        case CODEC_ID_GSM:
48
+            s->block_size = GSM_BLOCK_SIZE;
49
+            s->duration   = GSM_FRAME_SIZE;
50
+            break;
51
+        case CODEC_ID_GSM_MS:
52
+            s->block_size = GSM_MS_BLOCK_SIZE;
53
+            s->duration   = GSM_FRAME_SIZE * 2;
54
+            break;
49 55
         default:
50 56
             return AVERROR(EINVAL);
51 57
         }
... ...
@@ -66,6 +73,9 @@ static int gsm_parse(AVCodecParserContext *s1, AVCodecContext *avctx,
66 66
         *poutbuf_size = 0;
67 67
         return buf_size;
68 68
     }
69
+
70
+    s1->duration = s->duration;
71
+
69 72
     *poutbuf      = buf;
70 73
     *poutbuf_size = buf_size;
71 74
     return next;
... ...
@@ -1261,9 +1261,10 @@ static av_cold int decode_end(AVCodecContext *avctx)
1261 1261
 #endif /* CONFIG_HUFFYUV_DECODER || CONFIG_FFVHUFF_DECODER */
1262 1262
 
1263 1263
 #if CONFIG_HUFFYUV_ENCODER || CONFIG_FFVHUFF_ENCODER
1264
-static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
1264
+static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1265
+                        const AVFrame *pict, int *got_packet)
1266
+{
1265 1267
     HYuvContext *s = avctx->priv_data;
1266
-    AVFrame *pict = data;
1267 1268
     const int width= s->width;
1268 1269
     const int width2= s->width>>1;
1269 1270
     const int height= s->height;
... ...
@@ -1271,7 +1272,13 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1271 1271
     const int fake_ustride= s->interlaced ? pict->linesize[1]*2  : pict->linesize[1];
1272 1272
     const int fake_vstride= s->interlaced ? pict->linesize[2]*2  : pict->linesize[2];
1273 1273
     AVFrame * const p= &s->picture;
1274
-    int i, j, size=0;
1274
+    int i, j, size = 0, ret;
1275
+
1276
+    if (!pkt->data &&
1277
+        (ret = av_new_packet(pkt, width * height * 3 * 4 + FF_MIN_BUFFER_SIZE)) < 0) {
1278
+        av_log(avctx, AV_LOG_ERROR, "Error allocating output packet.\n");
1279
+        return ret;
1280
+    }
1275 1281
 
1276 1282
     *p = *pict;
1277 1283
     p->pict_type= AV_PICTURE_TYPE_I;
... ...
@@ -1282,7 +1289,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1282 1282
             generate_len_table(s->len[i], s->stats[i]);
1283 1283
             if(generate_bits_table(s->bits[i], s->len[i])<0)
1284 1284
                 return -1;
1285
-            size+= store_table(s, s->len[i], &buf[size]);
1285
+            size += store_table(s, s->len[i], &pkt->data[size]);
1286 1286
         }
1287 1287
 
1288 1288
         for(i=0; i<3; i++)
... ...
@@ -1290,7 +1297,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1290 1290
                 s->stats[i][j] >>= 1;
1291 1291
     }
1292 1292
 
1293
-    init_put_bits(&s->pb, buf+size, buf_size-size);
1293
+    init_put_bits(&s->pb, pkt->data + size, pkt->size - size);
1294 1294
 
1295 1295
     if(avctx->pix_fmt == PIX_FMT_YUV422P || avctx->pix_fmt == PIX_FMT_YUV420P){
1296 1296
         int lefty, leftu, leftv, y, cy;
... ...
@@ -1473,12 +1480,16 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1473 1473
         avctx->stats_out[0] = '\0';
1474 1474
     if(!(s->avctx->flags2 & CODEC_FLAG2_NO_OUTPUT)){
1475 1475
         flush_put_bits(&s->pb);
1476
-        s->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size);
1476
+        s->dsp.bswap_buf((uint32_t*)pkt->data, (uint32_t*)pkt->data, size);
1477 1477
     }
1478 1478
 
1479 1479
     s->picture_number++;
1480 1480
 
1481
-    return size*4;
1481
+    pkt->size   = size*4;
1482
+    pkt->flags |= AV_PKT_FLAG_KEY;
1483
+    *got_packet = 1;
1484
+
1485
+    return 0;
1482 1486
 }
1483 1487
 
1484 1488
 static av_cold int encode_end(AVCodecContext *avctx)
... ...
@@ -1531,7 +1542,7 @@ AVCodec ff_huffyuv_encoder = {
1531 1531
     .id             = CODEC_ID_HUFFYUV,
1532 1532
     .priv_data_size = sizeof(HYuvContext),
1533 1533
     .init           = encode_init,
1534
-    .encode         = encode_frame,
1534
+    .encode2        = encode_frame,
1535 1535
     .close          = encode_end,
1536 1536
     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV422P, PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_NONE},
1537 1537
     .long_name = NULL_IF_CONFIG_SMALL("Huffyuv / HuffYUV"),
... ...
@@ -1545,7 +1556,7 @@ AVCodec ff_ffvhuff_encoder = {
1545 1545
     .id             = CODEC_ID_FFVHUFF,
1546 1546
     .priv_data_size = sizeof(HYuvContext),
1547 1547
     .init           = encode_init,
1548
-    .encode         = encode_frame,
1548
+    .encode2        = encode_frame,
1549 1549
     .close          = encode_end,
1550 1550
     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_NONE},
1551 1551
     .long_name = NULL_IF_CONFIG_SMALL("Huffyuv FFmpeg variant"),
... ...
@@ -26,6 +26,7 @@
26 26
 
27 27
 #include <stdint.h>
28 28
 
29
+#include "libavutil/mathematics.h"
29 30
 #include "libavutil/pixfmt.h"
30 31
 #include "avcodec.h"
31 32
 
... ...
@@ -137,4 +138,14 @@ int avpriv_unlock_avformat(void);
137 137
  */
138 138
 int ff_alloc_packet(AVPacket *avpkt, int size);
139 139
 
140
+/**
141
+ * Rescale from sample rate to AVCodecContext.time_base.
142
+ */
143
+static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx,
144
+                                                        int64_t samples)
145
+{
146
+    return av_rescale_q(samples, (AVRational){ 1, avctx->sample_rate },
147
+                        avctx->time_base);
148
+}
149
+
140 150
 #endif /* AVCODEC_INTERNAL_H */
... ...
@@ -24,261 +24,227 @@
24 24
  * Interface to libmp3lame for mp3 encoding.
25 25
  */
26 26
 
27
+#include <lame/lame.h>
28
+
27 29
 #include "libavutil/intreadwrite.h"
28 30
 #include "libavutil/log.h"
29 31
 #include "libavutil/opt.h"
30 32
 #include "avcodec.h"
33
+#include "internal.h"
31 34
 #include "mpegaudio.h"
32
-#include <lame/lame.h>
35
+#include "mpegaudiodecheader.h"
33 36
 
34 37
 #define BUFFER_SIZE (7200 + 2 * MPA_FRAME_SIZE + MPA_FRAME_SIZE / 4+1000) // FIXME: Buffer size to small? Adding 1000 to make up for it.
35
-typedef struct Mp3AudioContext {
38
+
39
+typedef struct LAMEContext {
36 40
     AVClass *class;
41
+    AVCodecContext *avctx;
37 42
     lame_global_flags *gfp;
38
-    int stereo;
39 43
     uint8_t buffer[BUFFER_SIZE];
40 44
     int buffer_index;
41
-    struct {
42
-        int *left;
43
-        int *right;
44
-    } s32_data;
45 45
     int reservoir;
46
-} Mp3AudioContext;
46
+    void *planar_samples[2];
47
+} LAMEContext;
48
+
47 49
 
48
-static av_cold int MP3lame_encode_init(AVCodecContext *avctx)
50
+static av_cold int mp3lame_encode_close(AVCodecContext *avctx)
49 51
 {
50
-    Mp3AudioContext *s = avctx->priv_data;
52
+    LAMEContext *s = avctx->priv_data;
51 53
 
54
+    av_freep(&avctx->coded_frame);
55
+    av_freep(&s->planar_samples[0]);
56
+    av_freep(&s->planar_samples[1]);
57
+
58
+    lame_close(s->gfp);
59
+    return 0;
60
+}
61
+
62
+static av_cold int mp3lame_encode_init(AVCodecContext *avctx)
63
+{
64
+    LAMEContext *s = avctx->priv_data;
65
+    int ret;
66
+
67
+    s->avctx = avctx;
68
+
69
+    /* initialize LAME and get defaults */
70
+    if ((s->gfp = lame_init()) == NULL)
71
+        return AVERROR(ENOMEM);
72
+
73
+    /* channels */
52 74
     if (avctx->channels > 2) {
53 75
         av_log(avctx, AV_LOG_ERROR,
54 76
                "Invalid number of channels %d, must be <= 2\n", avctx->channels);
55
-        return AVERROR(EINVAL);
77
+        ret =  AVERROR(EINVAL);
78
+        goto error;
56 79
     }
80
+    lame_set_num_channels(s->gfp, avctx->channels);
81
+    lame_set_mode(s->gfp, avctx->channels > 1 ? JOINT_STEREO : MONO);
57 82
 
58
-    s->stereo = avctx->channels > 1 ? 1 : 0;
59
-
60
-    if ((s->gfp = lame_init()) == NULL)
61
-        goto err;
62
-    lame_set_in_samplerate(s->gfp, avctx->sample_rate);
83
+    /* sample rate */
84
+    lame_set_in_samplerate (s->gfp, avctx->sample_rate);
63 85
     lame_set_out_samplerate(s->gfp, avctx->sample_rate);
64
-    lame_set_num_channels(s->gfp, avctx->channels);
65
-    if (avctx->compression_level == FF_COMPRESSION_DEFAULT) {
86
+
87
+    /* algorithmic quality */
88
+    if (avctx->compression_level == FF_COMPRESSION_DEFAULT)
66 89
         lame_set_quality(s->gfp, 5);
67
-    } else {
90
+    else
68 91
         lame_set_quality(s->gfp, avctx->compression_level);
69
-    }
70
-    lame_set_mode(s->gfp, s->stereo ? JOINT_STEREO : MONO);
71
-    lame_set_brate(s->gfp, avctx->bit_rate / 1000);
92
+
93
+    /* rate control */
72 94
     if (avctx->flags & CODEC_FLAG_QSCALE) {
73
-        lame_set_brate(s->gfp, 0);
74 95
         lame_set_VBR(s->gfp, vbr_default);
75 96
         lame_set_VBR_quality(s->gfp, avctx->global_quality / (float)FF_QP2LAMBDA);
97
+    } else {
98
+        if (avctx->bit_rate)
99
+            lame_set_brate(s->gfp, avctx->bit_rate / 1000);
76 100
     }
77
-    lame_set_bWriteVbrTag(s->gfp,0);
78
-    lame_set_disable_reservoir(s->gfp, !s->reservoir);
79
-    if (lame_init_params(s->gfp) < 0)
80
-        goto err_close;
81 101
 
82
-    avctx->frame_size             = lame_get_framesize(s->gfp);
102
+    /* do not get a Xing VBR header frame from LAME */
103
+    lame_set_bWriteVbrTag(s->gfp,0);
83 104
 
84
-    if(!(avctx->coded_frame= avcodec_alloc_frame())) {
85
-        lame_close(s->gfp);
105
+    /* bit reservoir usage */
106
+    lame_set_disable_reservoir(s->gfp, !s->reservoir);
86 107
 
87
-        return AVERROR(ENOMEM);
108
+    /* set specified parameters */
109
+    if (lame_init_params(s->gfp) < 0) {
110
+        ret = -1;
111
+        goto error;
88 112
     }
89 113
 
90
-    if(AV_SAMPLE_FMT_S32 == avctx->sample_fmt && s->stereo) {
91
-        int nelem = 2 * avctx->frame_size;
92
-
93
-        if(! (s->s32_data.left = av_malloc(nelem * sizeof(int)))) {
94
-            av_freep(&avctx->coded_frame);
95
-            lame_close(s->gfp);
114
+    avctx->frame_size  = lame_get_framesize(s->gfp);
115
+    avctx->coded_frame = avcodec_alloc_frame();
116
+    if (!avctx->coded_frame) {
117
+        ret = AVERROR(ENOMEM);
118
+        goto error;
119
+    }
96 120
 
97
-            return AVERROR(ENOMEM);
121
+    /* sample format */
122
+    if (avctx->sample_fmt == AV_SAMPLE_FMT_S32 ||
123
+        avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
124
+        int ch;
125
+        for (ch = 0; ch < avctx->channels; ch++) {
126
+            s->planar_samples[ch] = av_malloc(avctx->frame_size *
127
+                                              av_get_bytes_per_sample(avctx->sample_fmt));
128
+            if (!s->planar_samples[ch]) {
129
+                ret = AVERROR(ENOMEM);
130
+                goto error;
131
+            }
98 132
         }
99
-
100
-        s->s32_data.right = s->s32_data.left + avctx->frame_size;
101 133
     }
102 134
 
103 135
     return 0;
104
-
105
-err_close:
106
-    lame_close(s->gfp);
107
-err:
108
-    return -1;
136
+error:
137
+    mp3lame_encode_close(avctx);
138
+    return ret;
109 139
 }
110 140
 
111
-static const int sSampleRates[] = {
112
-    44100, 48000,  32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
113
-};
114
-
115
-static const int sBitRates[2][3][15] = {
116
-    {
117
-        { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448 },
118
-        { 0, 32, 48, 56, 64,  80,  96,  112, 128, 160, 192, 224, 256, 320, 384 },
119
-        { 0, 32, 40, 48, 56,  64,  80,  96,  112, 128, 160, 192, 224, 256, 320 }
120
-    },
121
-    {
122
-        { 0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256 },
123
-        { 0,  8, 16, 24, 32, 40, 48,  56,  64,  80,  96, 112, 128, 144, 160 },
124
-        { 0,  8, 16, 24, 32, 40, 48,  56,  64,  80,  96, 112, 128, 144, 160 }
125
-    },
126
-};
127
-
128
-static const int sSamplesPerFrame[2][3] = {
129
-    { 384, 1152, 1152 },
130
-    { 384, 1152,  576 }
131
-};
132
-
133
-static const int sBitsPerSlot[3] = { 32, 8, 8 };
134
-
135
-static int mp3len(void *data, int *samplesPerFrame, int *sampleRate)
141
+#define DEINTERLEAVE(type, scale) do {                  \
142
+    int ch, i;                                          \
143
+    for (ch = 0; ch < s->avctx->channels; ch++) {       \
144
+        const type *input = samples;                    \
145
+        type      *output = s->planar_samples[ch];      \
146
+        input += ch;                                    \
147
+        for (i = 0; i < s->avctx->frame_size; i++) {    \
148
+            output[i] = *input * scale;                 \
149
+            input += s->avctx->channels;                \
150
+        }                                               \
151
+    }                                                   \
152
+} while (0)
153
+
154
+static int encode_frame_int16(LAMEContext *s, void *samples)
136 155
 {
137
-    uint32_t header  = AV_RB32(data);
138
-    int layerID      = 3 - ((header >> 17) & 0x03);
139
-    int bitRateID    = ((header >> 12) & 0x0f);
140
-    int sampleRateID = ((header >> 10) & 0x03);
141
-    int bitsPerSlot  = sBitsPerSlot[layerID];
142
-    int isPadded     = ((header >> 9) & 0x01);
143
-    static int const mode_tab[4] = { 2, 3, 1, 0 };
144
-    int mode    = mode_tab[(header >> 19) & 0x03];
145
-    int mpeg_id = mode > 0;
146
-    int temp0, temp1, bitRate;
147
-
148
-    if (((header >> 21) & 0x7ff) != 0x7ff || mode == 3 || layerID == 3 ||
149
-        sampleRateID == 3) {
150
-        return -1;
156
+    if (s->avctx->channels > 1) {
157
+        return lame_encode_buffer_interleaved(s->gfp, samples,
158
+                                              s->avctx->frame_size,
159
+                                              s->buffer + s->buffer_index,
160
+                                              BUFFER_SIZE - s->buffer_index);
161
+    } else {
162
+        return lame_encode_buffer(s->gfp, samples, NULL, s->avctx->frame_size,
163
+                                  s->buffer + s->buffer_index,
164
+                                  BUFFER_SIZE - s->buffer_index);
151 165
     }
166
+}
152 167
 
153
-    if (!samplesPerFrame)
154
-        samplesPerFrame = &temp0;
155
-    if (!sampleRate)
156
-        sampleRate      = &temp1;
168
+static int encode_frame_int32(LAMEContext *s, void *samples)
169
+{
170
+    DEINTERLEAVE(int32_t, 1);
157 171
 
158
-    //*isMono = ((header >>  6) & 0x03) == 0x03;
172
+    return lame_encode_buffer_int(s->gfp,
173
+                                  s->planar_samples[0], s->planar_samples[1],
174
+                                  s->avctx->frame_size,
175
+                                  s->buffer + s->buffer_index,
176
+                                  BUFFER_SIZE - s->buffer_index);
177
+}
159 178
 
160
-    *sampleRate      = sSampleRates[sampleRateID] >> mode;
161
-    bitRate          = sBitRates[mpeg_id][layerID][bitRateID] * 1000;
162
-    *samplesPerFrame = sSamplesPerFrame[mpeg_id][layerID];
163
-    //av_log(NULL, AV_LOG_DEBUG,
164
-    //       "sr:%d br:%d spf:%d l:%d m:%d\n",
165
-    //       *sampleRate, bitRate, *samplesPerFrame, layerID, mode);
179
+static int encode_frame_float(LAMEContext *s, void *samples)
180
+{
181
+    DEINTERLEAVE(float, 32768.0f);
166 182
 
167
-    return *samplesPerFrame * bitRate / (bitsPerSlot * *sampleRate) + isPadded;
183
+    return lame_encode_buffer_float(s->gfp,
184
+                                    s->planar_samples[0], s->planar_samples[1],
185
+                                    s->avctx->frame_size,
186
+                                    s->buffer + s->buffer_index,
187
+                                    BUFFER_SIZE - s->buffer_index);
168 188
 }
169 189
 
170
-static int MP3lame_encode_frame(AVCodecContext *avctx, unsigned char *frame,
190
+static int mp3lame_encode_frame(AVCodecContext *avctx, unsigned char *frame,
171 191
                                 int buf_size, void *data)
172 192
 {
173
-    Mp3AudioContext *s = avctx->priv_data;
193
+    LAMEContext *s = avctx->priv_data;
194
+    MPADecodeHeader hdr;
174 195
     int len;
175 196
     int lame_result;
176 197
 
177
-    /* lame 3.91 dies on '1-channel interleaved' data */
178
-
179
-    if (!data){
180
-        lame_result= lame_encode_flush(
181
-                s->gfp,
182
-                s->buffer + s->buffer_index,
183
-                BUFFER_SIZE - s->buffer_index
184
-                );
185
-#if 2147483647 == INT_MAX
186
-    }else if(AV_SAMPLE_FMT_S32 == avctx->sample_fmt){
187
-        if (s->stereo) {
188
-            int32_t *rp = data;
189
-            int32_t *mp = rp + 2*avctx->frame_size;
190
-            int *wpl = s->s32_data.left;
191
-            int *wpr = s->s32_data.right;
192
-
193
-            while (rp < mp) {
194
-                *wpl++ = *rp++;
195
-                *wpr++ = *rp++;
196
-            }
197
-
198
-            lame_result = lame_encode_buffer_int(
199
-                s->gfp,
200
-                s->s32_data.left,
201
-                s->s32_data.right,
202
-                avctx->frame_size,
203
-                s->buffer + s->buffer_index,
204
-                BUFFER_SIZE - s->buffer_index
205
-                );
206
-        } else {
207
-            lame_result = lame_encode_buffer_int(
208
-                s->gfp,
209
-                data,
210
-                data,
211
-                avctx->frame_size,
212
-                s->buffer + s->buffer_index,
213
-                BUFFER_SIZE - s->buffer_index
214
-                );
215
-        }
216
-#endif
217
-    }else{
218
-        if (s->stereo) {
219
-            lame_result = lame_encode_buffer_interleaved(
220
-                s->gfp,
221
-                data,
222
-                avctx->frame_size,
223
-                s->buffer + s->buffer_index,
224
-                BUFFER_SIZE - s->buffer_index
225
-                );
226
-        } else {
227
-            lame_result = lame_encode_buffer(
228
-                s->gfp,
229
-                data,
230
-                data,
231
-                avctx->frame_size,
232
-                s->buffer + s->buffer_index,
233
-                BUFFER_SIZE - s->buffer_index
234
-                );
198
+    if (data) {
199
+        switch (avctx->sample_fmt) {
200
+        case AV_SAMPLE_FMT_S16:
201
+            lame_result = encode_frame_int16(s, data);
202
+            break;
203
+        case AV_SAMPLE_FMT_S32:
204
+            lame_result = encode_frame_int32(s, data);
205
+            break;
206
+        case AV_SAMPLE_FMT_FLT:
207
+            lame_result = encode_frame_float(s, data);
208
+            break;
209
+        default:
210
+            return AVERROR_BUG;
235 211
         }
212
+    } else {
213
+        lame_result = lame_encode_flush(s->gfp, s->buffer + s->buffer_index,
214
+                                        BUFFER_SIZE - s->buffer_index);
236 215
     }
237
-
238 216
     if (lame_result < 0) {
239 217
         if (lame_result == -1) {
240
-            /* output buffer too small */
241 218
             av_log(avctx, AV_LOG_ERROR,
242 219
                    "lame: output buffer too small (buffer index: %d, free bytes: %d)\n",
243 220
                    s->buffer_index, BUFFER_SIZE - s->buffer_index);
244 221
         }
245 222
         return -1;
246 223
     }
247
-
248 224
     s->buffer_index += lame_result;
249 225
 
226
+    /* Move 1 frame from the LAME buffer to the output packet, if available.
227
+       We have to parse the first frame header in the output buffer to
228
+       determine the frame size. */
250 229
     if (s->buffer_index < 4)
251 230
         return 0;
252
-
253
-    len = mp3len(s->buffer, NULL, NULL);
254
-    //av_log(avctx, AV_LOG_DEBUG, "in:%d packet-len:%d index:%d\n",
255
-    //       avctx->frame_size, len, s->buffer_index);
231
+    if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) {
232
+        av_log(avctx, AV_LOG_ERROR, "free format output not supported\n");
233
+        return -1;
234
+    }
235
+    len = hdr.frame_size;
236
+    av_dlog(avctx, "in:%d packet-len:%d index:%d\n", avctx->frame_size, len,
237
+            s->buffer_index);
256 238
     if (len <= s->buffer_index) {
257 239
         memcpy(frame, s->buffer, len);
258 240
         s->buffer_index -= len;
259
-
260 241
         memmove(s->buffer, s->buffer + len, s->buffer_index);
261
-        // FIXME fix the audio codec API, so we do not need the memcpy()
262
-        /*for(i=0; i<len; i++) {
263
-            av_log(avctx, AV_LOG_DEBUG, "%2X ", frame[i]);
264
-        }*/
265 242
         return len;
266 243
     } else
267 244
         return 0;
268 245
 }
269 246
 
270
-static av_cold int MP3lame_encode_close(AVCodecContext *avctx)
271
-{
272
-    Mp3AudioContext *s = avctx->priv_data;
273
-
274
-    av_freep(&s->s32_data.left);
275
-    av_freep(&avctx->coded_frame);
276
-
277
-    lame_close(s->gfp);
278
-    return 0;
279
-}
280
-
281
-#define OFFSET(x) offsetof(Mp3AudioContext, x)
247
+#define OFFSET(x) offsetof(LAMEContext, x)
282 248
 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
283 249
 static const AVOption options[] = {
284 250
     { "reservoir", "Use bit reservoir.", OFFSET(reservoir), AV_OPT_TYPE_INT, { 1 }, 0, 1, AE },
... ...
@@ -292,21 +258,30 @@ static const AVClass libmp3lame_class = {
292 292
     .version    = LIBAVUTIL_VERSION_INT,
293 293
 };
294 294
 
295
+static const AVCodecDefault libmp3lame_defaults[] = {
296
+    { "b",          "0" },
297
+    { NULL },
298
+};
299
+
300
+static const int libmp3lame_sample_rates[] = {
301
+    44100, 48000,  32000, 22050, 24000, 16000, 11025, 12000, 8000, 0
302
+};
303
+
295 304
 AVCodec ff_libmp3lame_encoder = {
296 305
     .name                  = "libmp3lame",
297 306
     .type                  = AVMEDIA_TYPE_AUDIO,
298 307
     .id                    = CODEC_ID_MP3,
299
-    .priv_data_size        = sizeof(Mp3AudioContext),
300
-    .init                  = MP3lame_encode_init,
301
-    .encode                = MP3lame_encode_frame,
302
-    .close                 = MP3lame_encode_close,
308
+    .priv_data_size        = sizeof(LAMEContext),
309
+    .init                  = mp3lame_encode_init,
310
+    .encode                = mp3lame_encode_frame,
311
+    .close                 = mp3lame_encode_close,
303 312
     .capabilities          = CODEC_CAP_DELAY,
304
-    .sample_fmts           = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
305
-#if 2147483647 == INT_MAX
306
-    AV_SAMPLE_FMT_S32,
307
-#endif
313
+    .sample_fmts           = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S32,
314
+                                                             AV_SAMPLE_FMT_FLT,
315
+                                                             AV_SAMPLE_FMT_S16,
308 316
                                                              AV_SAMPLE_FMT_NONE },
309
-    .supported_samplerates = sSampleRates,
317
+    .supported_samplerates = libmp3lame_sample_rates,
310 318
     .long_name             = NULL_IF_CONFIG_SMALL("libmp3lame MP3 (MPEG audio layer 3)"),
311 319
     .priv_class            = &libmp3lame_class,
320
+    .defaults              = libmp3lame_defaults,
312 321
 };
... ...
@@ -67,7 +67,6 @@
67 67
 #include <speex/speex.h>
68 68
 #include <speex/speex_header.h>
69 69
 #include <speex/speex_stereo.h>
70
-#include "libavutil/mathematics.h"
71 70
 #include "libavutil/opt.h"
72 71
 #include "avcodec.h"
73 72
 #include "internal.h"
... ...
@@ -258,9 +257,7 @@ static int encode_frame(AVCodecContext *avctx, uint8_t *frame, int buf_size,
258 258
     /* write output if all frames for the packet have been encoded */
259 259
     if (s->pkt_frame_count == s->frames_per_packet) {
260 260
         s->pkt_frame_count = 0;
261
-        avctx->coded_frame->pts =
262
-            av_rescale_q(s->next_pts, (AVRational){ 1, avctx->sample_rate },
263
-                         avctx->time_base);
261
+        avctx->coded_frame->pts = ff_samples_to_time_base(avctx, s->next_pts);
264 262
         s->next_pts += s->pkt_sample_count;
265 263
         s->pkt_sample_count = 0;
266 264
         if (buf_size > speex_bits_nbytes(&s->bits)) {
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/opt.h"
30 30
 #include "avcodec.h"
31 31
 #include "bytestream.h"
32
+#include "internal.h"
32 33
 #include "vorbis.h"
33
-#include "libavutil/mathematics.h"
34 34
 
35 35
 #undef NDEBUG
36 36
 #include <assert.h>
... ...
@@ -268,7 +268,8 @@ static int oggvorbis_encode_frame(AVCodecContext *avccontext,
268 268
         op2->packet = context->buffer + sizeof(ogg_packet);
269 269
 
270 270
         l = op2->bytes;
271
-        avccontext->coded_frame->pts = av_rescale_q(op2->granulepos, (AVRational) { 1, avccontext->sample_rate }, avccontext->time_base);
271
+        avccontext->coded_frame->pts = ff_samples_to_time_base(avccontext,
272
+                                                               op2->granulepos);
272 273
         //FIXME we should reorder the user supplied pts and not assume that they are spaced by 1/sample_rate
273 274
 
274 275
         if (l > buf_size) {
... ...
@@ -314,7 +314,7 @@ static int mlp_parse(AVCodecParserContext *s,
314 314
         else
315 315
             avctx->sample_fmt = AV_SAMPLE_FMT_S16;
316 316
         avctx->sample_rate = mh.group1_samplerate;
317
-        avctx->frame_size = mh.access_unit_size;
317
+        s->duration = mh.access_unit_size;
318 318
 
319 319
         if (mh.stream_type == 0xbb) {
320 320
             /* MLP stream */
... ...
@@ -78,7 +78,7 @@ static int mpegaudio_parse(AVCodecParserContext *s1,
78 78
                     if(s->header_count > 1){
79 79
                         avctx->sample_rate= sr;
80 80
                         avctx->channels   = channels;
81
-                        avctx->frame_size = frame_size;
81
+                        s1->duration      = frame_size;
82 82
                         avctx->bit_rate   = bit_rate;
83 83
                     }
84 84
                     break;
... ...
@@ -29,6 +29,7 @@
29 29
 #include "avcodec.h"
30 30
 #include "bytestream.h"
31 31
 #include "libavutil/imgutils.h"
32
+#include "internal.h"
32 33
 
33 34
 typedef struct PCXContext {
34 35
     AVFrame picture;
... ...
@@ -96,20 +97,20 @@ static int pcx_rle_encode(      uint8_t *dst, int dst_size,
96 96
     return dst - dst_start;
97 97
 }
98 98
 
99
-static int pcx_encode_frame(AVCodecContext *avctx,
100
-                            unsigned char *buf, int buf_size, void *data)
99
+static int pcx_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
100
+                            const AVFrame *frame, int *got_packet)
101 101
 {
102 102
     PCXContext *s = avctx->priv_data;
103 103
     AVFrame *const pict = &s->picture;
104
-    const uint8_t *buf_start = buf;
105
-    const uint8_t *buf_end   = buf + buf_size;
104
+    const uint8_t *buf_end;
105
+    uint8_t *buf;
106 106
 
107
-    int bpp, nplanes, i, y, line_bytes, written;
107
+    int bpp, nplanes, i, y, line_bytes, written, ret, max_pkt_size;
108 108
     const uint32_t *pal = NULL;
109 109
     uint32_t palette256[256];
110 110
     const uint8_t *src;
111 111
 
112
-    *pict = *(AVFrame *)data;
112
+    *pict = *frame;
113 113
     pict->pict_type = AV_PICTURE_TYPE_I;
114 114
     pict->key_frame = 1;
115 115
 
... ...
@@ -151,6 +152,14 @@ static int pcx_encode_frame(AVCodecContext *avctx,
151 151
     line_bytes = (avctx->width * bpp + 7) >> 3;
152 152
     line_bytes = (line_bytes + 1) & ~1;
153 153
 
154
+    max_pkt_size = 128 + avctx->height * 2 * line_bytes * nplanes + (pal ? 256*3 + 1 : 0);
155
+    if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
156
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
157
+        return ret;
158
+    }
159
+    buf     = pkt->data;
160
+    buf_end = pkt->data + pkt->size;
161
+
154 162
     bytestream_put_byte(&buf, 10);                  // manufacturer
155 163
     bytestream_put_byte(&buf, 5);                   // version
156 164
     bytestream_put_byte(&buf, 1);                   // encoding
... ...
@@ -167,7 +176,7 @@ static int pcx_encode_frame(AVCodecContext *avctx,
167 167
     bytestream_put_byte(&buf, nplanes);             // number of planes
168 168
     bytestream_put_le16(&buf, line_bytes);          // scanline plane size in bytes
169 169
 
170
-    while (buf - buf_start < 128)
170
+    while (buf - pkt->data < 128)
171 171
         *buf++= 0;
172 172
 
173 173
     src = pict->data[0];
... ...
@@ -193,7 +202,11 @@ static int pcx_encode_frame(AVCodecContext *avctx,
193 193
         }
194 194
     }
195 195
 
196
-    return buf - buf_start;
196
+    pkt->size   = buf - pkt->data;
197
+    pkt->flags |= AV_PKT_FLAG_KEY;
198
+    *got_packet = 1;
199
+
200
+    return 0;
197 201
 }
198 202
 
199 203
 AVCodec ff_pcx_encoder = {
... ...
@@ -202,7 +215,7 @@ AVCodec ff_pcx_encoder = {
202 202
     .id             = CODEC_ID_PCX,
203 203
     .priv_data_size = sizeof(PCXContext),
204 204
     .init           = pcx_encode_init,
205
-    .encode         = pcx_encode_frame,
205
+    .encode2        = pcx_encode_frame,
206 206
     .pix_fmts = (const enum PixelFormat[]){
207 207
         PIX_FMT_RGB24,
208 208
         PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8,
... ...
@@ -201,7 +201,6 @@ AVCodec ff_pgm_decoder = {
201 201
     .close          = ff_pnm_end,
202 202
     .decode         = pnm_decode_frame,
203 203
     .capabilities   = CODEC_CAP_DR1,
204
-    .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
205 204
     .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
206 205
 };
207 206
 #endif
... ...
@@ -216,7 +215,6 @@ AVCodec ff_pgmyuv_decoder = {
216 216
     .close          = ff_pnm_end,
217 217
     .decode         = pnm_decode_frame,
218 218
     .capabilities   = CODEC_CAP_DR1,
219
-    .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
220 219
     .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
221 220
 };
222 221
 #endif
... ...
@@ -231,7 +229,6 @@ AVCodec ff_ppm_decoder = {
231 231
     .close          = ff_pnm_end,
232 232
     .decode         = pnm_decode_frame,
233 233
     .capabilities   = CODEC_CAP_DR1,
234
-    .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
235 234
     .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
236 235
 };
237 236
 #endif
... ...
@@ -246,7 +243,6 @@ AVCodec ff_pbm_decoder = {
246 246
     .close          = ff_pnm_end,
247 247
     .decode         = pnm_decode_frame,
248 248
     .capabilities   = CODEC_CAP_DR1,
249
-    .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
250 249
     .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
251 250
 };
252 251
 #endif
... ...
@@ -261,7 +257,6 @@ AVCodec ff_pam_decoder = {
261 261
     .close          = ff_pnm_end,
262 262
     .decode         = pnm_decode_frame,
263 263
     .capabilities   = CODEC_CAP_DR1,
264
-    .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB32, PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, PIX_FMT_NONE},
265 264
     .long_name = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
266 265
 };
267 266
 #endif
... ...
@@ -20,21 +20,23 @@
20 20
  */
21 21
 
22 22
 #include "avcodec.h"
23
+#include "internal.h"
23 24
 #include "pnm.h"
24 25
 
25 26
 
26
-static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
27
-                            int buf_size, void *data)
27
+static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
28
+                            const AVFrame *pict, int *got_packet)
28 29
 {
29 30
     PNMContext *s     = avctx->priv_data;
30
-    AVFrame *pict     = data;
31 31
     AVFrame * const p = (AVFrame*)&s->picture;
32
-    int i, h, h1, c, n, linesize;
32
+    int i, h, h1, c, n, linesize, ret;
33 33
     uint8_t *ptr, *ptr1, *ptr2;
34 34
 
35
-    if (buf_size < avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height) + 200) {
35
+    if ((ret = ff_alloc_packet(pkt, avpicture_get_size(avctx->pix_fmt,
36
+                                                       avctx->width,
37
+                                                       avctx->height) + 200)) < 0) {
36 38
         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
37
-        return -1;
39
+        return ret;
38 40
     }
39 41
 
40 42
     *p           = *pict;
... ...
@@ -42,8 +44,8 @@ static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
42 42
     p->key_frame = 1;
43 43
 
44 44
     s->bytestream_start =
45
-    s->bytestream       = outbuf;
46
-    s->bytestream_end   = outbuf + buf_size;
45
+    s->bytestream       = pkt->data;
46
+    s->bytestream_end   = pkt->data + pkt->size;
47 47
 
48 48
     h  = avctx->height;
49 49
     h1 = h;
... ...
@@ -107,7 +109,11 @@ static int pnm_encode_frame(AVCodecContext *avctx, unsigned char *outbuf,
107 107
                 ptr2 += p->linesize[2];
108 108
         }
109 109
     }
110
-    return s->bytestream - s->bytestream_start;
110
+    pkt->size   = s->bytestream - s->bytestream_start;
111
+    pkt->flags |= AV_PKT_FLAG_KEY;
112
+    *got_packet = 1;
113
+
114
+    return 0;
111 115
 }
112 116
 
113 117
 
... ...
@@ -118,7 +124,7 @@ AVCodec ff_pgm_encoder = {
118 118
     .id             = CODEC_ID_PGM,
119 119
     .priv_data_size = sizeof(PNMContext),
120 120
     .init           = ff_pnm_init,
121
-    .encode         = pnm_encode_frame,
121
+    .encode2        = pnm_encode_frame,
122 122
     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE},
123 123
     .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
124 124
 };
... ...
@@ -131,7 +137,7 @@ AVCodec ff_pgmyuv_encoder = {
131 131
     .id             = CODEC_ID_PGMYUV,
132 132
     .priv_data_size = sizeof(PNMContext),
133 133
     .init           = ff_pnm_init,
134
-    .encode         = pnm_encode_frame,
134
+    .encode2        = pnm_encode_frame,
135 135
     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
136 136
     .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
137 137
 };
... ...
@@ -144,7 +150,7 @@ AVCodec ff_ppm_encoder = {
144 144
     .id             = CODEC_ID_PPM,
145 145
     .priv_data_size = sizeof(PNMContext),
146 146
     .init           = ff_pnm_init,
147
-    .encode         = pnm_encode_frame,
147
+    .encode2        = pnm_encode_frame,
148 148
     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE},
149 149
     .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
150 150
 };
... ...
@@ -157,7 +163,7 @@ AVCodec ff_pbm_encoder = {
157 157
     .id             = CODEC_ID_PBM,
158 158
     .priv_data_size = sizeof(PNMContext),
159 159
     .init           = ff_pnm_init,
160
-    .encode         = pnm_encode_frame,
160
+    .encode2        = pnm_encode_frame,
161 161
     .pix_fmts  = (const enum PixelFormat[]){PIX_FMT_MONOWHITE, PIX_FMT_NONE},
162 162
     .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
163 163
 };
... ...
@@ -69,7 +69,7 @@ typedef struct RoqContext {
69 69
 
70 70
     unsigned int framesSinceKeyframe;
71 71
 
72
-    AVFrame *frame_to_enc;
72
+    const AVFrame *frame_to_enc;
73 73
     uint8_t *out_buf;
74 74
     struct RoqTempData *tmpData;
75 75
 } RoqContext;
... ...
@@ -59,6 +59,7 @@
59 59
 #include "roqvideo.h"
60 60
 #include "bytestream.h"
61 61
 #include "elbg.h"
62
+#include "internal.h"
62 63
 #include "mathops.h"
63 64
 
64 65
 #define CHROMA_BIAS 1
... ...
@@ -112,7 +113,7 @@ static inline int square(int x)
112 112
     return x*x;
113 113
 }
114 114
 
115
-static inline int eval_sse(uint8_t *a, uint8_t *b, int count)
115
+static inline int eval_sse(const uint8_t *a, const uint8_t *b, int count)
116 116
 {
117 117
     int diff=0;
118 118
 
... ...
@@ -124,8 +125,8 @@ static inline int eval_sse(uint8_t *a, uint8_t *b, int count)
124 124
 
125 125
 // FIXME Could use DSPContext.sse, but it is not so speed critical (used
126 126
 // just for motion estimation).
127
-static int block_sse(uint8_t **buf1, uint8_t **buf2, int x1, int y1, int x2,
128
-                     int y2, int *stride1, int *stride2, int size)
127
+static int block_sse(uint8_t * const *buf1, uint8_t * const *buf2, int x1, int y1,
128
+                     int x2, int y2, const int *stride1, const int *stride2, int size)
129 129
 {
130 130
     int i, k;
131 131
     int sse=0;
... ...
@@ -260,7 +261,7 @@ static void create_cel_evals(RoqContext *enc, RoqTempdata *tempData)
260 260
 /**
261 261
  * Get macroblocks from parts of the image
262 262
  */
263
-static void get_frame_mb(AVFrame *frame, int x, int y, uint8_t mb[], int dim)
263
+static void get_frame_mb(const AVFrame *frame, int x, int y, uint8_t mb[], int dim)
264 264
 {
265 265
     int i, j, cp;
266 266
 
... ...
@@ -754,8 +755,8 @@ static void reconstruct_and_encode_image(RoqContext *enc, RoqTempdata *tempData,
754 754
 /**
755 755
  * Create a single YUV cell from a 2x2 section of the image
756 756
  */
757
-static inline void frame_block_to_cell(uint8_t *block, uint8_t **data,
758
-                                       int top, int left, int *stride)
757
+static inline void frame_block_to_cell(uint8_t *block, uint8_t * const *data,
758
+                                       int top, int left, const int *stride)
759 759
 {
760 760
     int i, j, u=0, v=0;
761 761
 
... ...
@@ -775,7 +776,7 @@ static inline void frame_block_to_cell(uint8_t *block, uint8_t **data,
775 775
 /**
776 776
  * Create YUV clusters for the entire image
777 777
  */
778
-static void create_clusters(AVFrame *frame, int w, int h, uint8_t *yuvClusters)
778
+static void create_clusters(const AVFrame *frame, int w, int h, uint8_t *yuvClusters)
779 779
 {
780 780
     int i, j, k, l;
781 781
 
... ...
@@ -1001,13 +1002,12 @@ static void roq_write_video_info_chunk(RoqContext *enc)
1001 1001
     bytestream_put_byte(&enc->out_buf, 0x00);
1002 1002
 }
1003 1003
 
1004
-static int roq_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
1004
+static int roq_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1005
+                            const AVFrame *frame, int *got_packet)
1005 1006
 {
1006 1007
     RoqContext *enc = avctx->priv_data;
1007
-    AVFrame *frame= data;
1008
-    uint8_t *buf_start = buf;
1008
+    int size, ret;
1009 1009
 
1010
-    enc->out_buf = buf;
1011 1010
     enc->avctx = avctx;
1012 1011
 
1013 1012
     enc->frame_to_enc = frame;
... ...
@@ -1019,10 +1019,12 @@ static int roq_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_s
1019 1019
 
1020 1020
     /* 138 bits max per 8x8 block +
1021 1021
      *     256 codebooks*(6 bytes 2x2 + 4 bytes 4x4) + 8 bytes frame header */
1022
-    if (((enc->width*enc->height/64)*138+7)/8 + 256*(6+4) + 8 > buf_size) {
1023
-        av_log(avctx, AV_LOG_ERROR, "  RoQ: Output buffer too small!\n");
1024
-        return -1;
1022
+    size = ((enc->width * enc->height / 64) * 138 + 7) / 8 + 256 * (6 + 4) + 8;
1023
+    if ((ret = ff_alloc_packet(pkt, size)) < 0) {
1024
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet with size %d.\n", size);
1025
+        return ret;
1025 1026
     }
1027
+    enc->out_buf = pkt->data;
1026 1028
 
1027 1029
     /* Check for I frame */
1028 1030
     if (enc->framesSinceKeyframe == avctx->gop_size)
... ...
@@ -1046,7 +1048,12 @@ static int roq_encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_s
1046 1046
     /* Encode the actual frame */
1047 1047
     roq_encode_video(enc);
1048 1048
 
1049
-    return enc->out_buf - buf_start;
1049
+    pkt->size   = enc->out_buf - pkt->data;
1050
+    if (enc->framesSinceKeyframe == 1)
1051
+        pkt->flags |= AV_PKT_FLAG_KEY;
1052
+    *got_packet = 1;
1053
+
1054
+    return 0;
1050 1055
 }
1051 1056
 
1052 1057
 static int roq_encode_end(AVCodecContext *avctx)
... ...
@@ -1071,7 +1078,7 @@ AVCodec ff_roq_encoder = {
1071 1071
     .id                   = CODEC_ID_ROQ,
1072 1072
     .priv_data_size       = sizeof(RoqContext),
1073 1073
     .init                 = roq_encode_init,
1074
-    .encode               = roq_encode_frame,
1074
+    .encode2              = roq_encode_frame,
1075 1075
     .close                = roq_encode_end,
1076 1076
     .supported_framerates = (const AVRational[]){{30,1}, {0,0}},
1077 1077
     .pix_fmts             = (const enum PixelFormat[]){PIX_FMT_YUV444P, PIX_FMT_NONE},
... ...
@@ -21,6 +21,7 @@
21 21
 
22 22
 #include "avcodec.h"
23 23
 #include "bytestream.h"
24
+#include "internal.h"
24 25
 #include "sgi.h"
25 26
 #include "rle.h"
26 27
 
... ...
@@ -41,17 +42,17 @@ static av_cold int encode_init(AVCodecContext *avctx)
41 41
     return 0;
42 42
 }
43 43
 
44
-static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
45
-                        int buf_size, void *data)
44
+static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
45
+                        const AVFrame *frame, int *got_packet)
46 46
 {
47 47
     SgiContext *s = avctx->priv_data;
48 48
     AVFrame * const p = &s->picture;
49
-    uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf;
50
-    int x, y, z, length, tablesize;
49
+    uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
50
+    int x, y, z, length, tablesize, ret;
51 51
     unsigned int width, height, depth, dimension, bytes_per_channel, pixmax, put_be;
52
-    unsigned char *orig_buf = buf, *end_buf = buf + buf_size;
52
+    unsigned char *end_buf;
53 53
 
54
-    *p = *(AVFrame*)data;
54
+    *p = *frame;
55 55
     p->pict_type = AV_PICTURE_TYPE_I;
56 56
     p->key_frame = 1;
57 57
 
... ...
@@ -106,12 +107,18 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
106 106
     }
107 107
 
108 108
     tablesize = depth * height * 4;
109
-    length = tablesize * 2 + SGI_HEADER_SIZE;
110
-
111
-    if (buf_size < length) {
112
-        av_log(avctx, AV_LOG_ERROR, "buf_size too small(need %d, got %d)\n", length, buf_size);
113
-        return -1;
109
+    length = SGI_HEADER_SIZE;
110
+    if (avctx->coder_type == FF_CODER_TYPE_RAW)
111
+        length += depth * height * width;
112
+    else // assume ff_rl_encode() produces at most 2x size of input
113
+        length += tablesize * 2 + depth * height * (2 * width + 1);
114
+
115
+    if ((ret = ff_alloc_packet(pkt, length)) < 0) {
116
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
117
+        return ret;
114 118
     }
119
+    buf     = pkt->data;
120
+    end_buf = pkt->data + pkt->size;
115 121
 
116 122
     /* Encode header. */
117 123
     bytestream_put_be16(&buf, SGI_MAGIC);
... ...
@@ -153,7 +160,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
153 153
             in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
154 154
 
155 155
             for (y = 0; y < height; y++) {
156
-                bytestream_put_be32(&offsettab, buf - orig_buf);
156
+                bytestream_put_be32(&offsettab, buf - pkt->data);
157 157
 
158 158
                 for (x = 0; x < width; x++)
159 159
                     encode_buf[x] = in_buf[depth * x];
... ...
@@ -193,7 +200,11 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
193 193
     }
194 194
 
195 195
     /* total length */
196
-    return buf - orig_buf;
196
+    pkt->size = buf - pkt->data;
197
+    pkt->flags |= AV_PKT_FLAG_KEY;
198
+    *got_packet = 1;
199
+
200
+    return 0;
197 201
 }
198 202
 
199 203
 AVCodec ff_sgi_encoder = {
... ...
@@ -202,7 +213,7 @@ AVCodec ff_sgi_encoder = {
202 202
     .id             = CODEC_ID_SGI,
203 203
     .priv_data_size = sizeof(SgiContext),
204 204
     .init           = encode_init,
205
-    .encode         = encode_frame,
205
+    .encode2        = encode_frame,
206 206
     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA,
207 207
                                           PIX_FMT_RGB48LE, PIX_FMT_RGB48BE,
208 208
                                           PIX_FMT_RGBA64LE, PIX_FMT_RGBA64BE,
... ...
@@ -22,6 +22,7 @@
22 22
 #include "libavutil/intreadwrite.h"
23 23
 #include "libavutil/pixdesc.h"
24 24
 #include "avcodec.h"
25
+#include "internal.h"
25 26
 #include "rle.h"
26 27
 #include "targa.h"
27 28
 
... ...
@@ -39,7 +40,7 @@ typedef struct TargaContext {
39 39
  * @param h Image height
40 40
  * @return Size of output in bytes, or -1 if larger than out_size
41 41
  */
42
-static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
42
+static int targa_encode_rle(uint8_t *outbuf, int out_size, const AVFrame *pic,
43 43
                             int bpp, int w, int h)
44 44
 {
45 45
     int y,ret;
... ...
@@ -59,7 +60,7 @@ static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
59 59
     return out - outbuf;
60 60
 }
61 61
 
62
-static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
62
+static int targa_encode_normal(uint8_t *outbuf, const AVFrame *pic, int bpp, int w, int h)
63 63
 {
64 64
     int i, n = bpp * w;
65 65
     uint8_t *out = outbuf;
... ...
@@ -74,11 +75,10 @@ static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, in
74 74
     return out - outbuf;
75 75
 }
76 76
 
77
-static int targa_encode_frame(AVCodecContext *avctx,
78
-                              unsigned char *outbuf,
79
-                              int buf_size, void *data){
80
-    AVFrame *p = data;
81
-    int bpp, picsize, datasize = -1;
77
+static int targa_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
78
+                              const AVFrame *p, int *got_packet)
79
+{
80
+    int bpp, picsize, datasize = -1, ret;
82 81
     uint8_t *out;
83 82
 
84 83
     if(avctx->width > 0xffff || avctx->height > 0xffff) {
... ...
@@ -86,46 +86,43 @@ static int targa_encode_frame(AVCodecContext *avctx,
86 86
         return AVERROR(EINVAL);
87 87
     }
88 88
     picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
89
-    if(buf_size < picsize + 45) {
89
+    if ((ret = ff_alloc_packet(pkt, picsize + 45)) < 0) {
90 90
         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
91
-        return AVERROR(EINVAL);
91
+        return ret;
92 92
     }
93 93
 
94
-    p->pict_type= AV_PICTURE_TYPE_I;
95
-    p->key_frame= 1;
96
-
97 94
     /* zero out the header and only set applicable fields */
98
-    memset(outbuf, 0, 12);
99
-    AV_WL16(outbuf+12, avctx->width);
100
-    AV_WL16(outbuf+14, avctx->height);
95
+    memset(pkt->data, 0, 12);
96
+    AV_WL16(pkt->data+12, avctx->width);
97
+    AV_WL16(pkt->data+14, avctx->height);
101 98
     /* image descriptor byte: origin is always top-left, bits 0-3 specify alpha */
102
-    outbuf[17] = 0x20 | (avctx->pix_fmt == PIX_FMT_BGRA ? 8 : 0);
99
+    pkt->data[17] = 0x20 | (avctx->pix_fmt == PIX_FMT_BGRA ? 8 : 0);
103 100
 
104 101
     switch(avctx->pix_fmt) {
105 102
     case PIX_FMT_GRAY8:
106
-        outbuf[2] = TGA_BW;      /* uncompressed grayscale image */
107
-        outbuf[16] = 8;          /* bpp */
103
+        pkt->data[2]  = TGA_BW;     /* uncompressed grayscale image */
104
+        pkt->data[16] = 8;          /* bpp */
108 105
         break;
109 106
     case PIX_FMT_RGB555LE:
110
-        outbuf[2] = TGA_RGB;     /* uncompresses true-color image */
111
-        outbuf[16] = 16;         /* bpp */
107
+        pkt->data[2]  = TGA_RGB;    /* uncompresses true-color image */
108
+        pkt->data[16] = 16;         /* bpp */
112 109
         break;
113 110
     case PIX_FMT_BGR24:
114
-        outbuf[2] = TGA_RGB;     /* uncompressed true-color image */
115
-        outbuf[16] = 24;         /* bpp */
111
+        pkt->data[2]  = TGA_RGB;    /* uncompressed true-color image */
112
+        pkt->data[16] = 24;         /* bpp */
116 113
         break;
117 114
     case PIX_FMT_BGRA:
118
-        outbuf[2] = TGA_RGB;     /* uncompressed true-color image */
119
-        outbuf[16] = 32;         /* bpp */
115
+        pkt->data[2]  = TGA_RGB;    /* uncompressed true-color image */
116
+        pkt->data[16] = 32;         /* bpp */
120 117
         break;
121 118
     default:
122 119
         av_log(avctx, AV_LOG_ERROR, "Pixel format '%s' not supported.\n",
123 120
                av_get_pix_fmt_name(avctx->pix_fmt));
124 121
         return AVERROR(EINVAL);
125 122
     }
126
-    bpp = outbuf[16] >> 3;
123
+    bpp = pkt->data[16] >> 3;
127 124
 
128
-    out = outbuf + 18;  /* skip past the header we just output */
125
+    out = pkt->data + 18;  /* skip past the header we just output */
129 126
 
130 127
     /* try RLE compression */
131 128
     if (avctx->coder_type != FF_CODER_TYPE_RAW)
... ...
@@ -133,7 +130,7 @@ static int targa_encode_frame(AVCodecContext *avctx,
133 133
 
134 134
     /* if that worked well, mark the picture as RLE compressed */
135 135
     if(datasize >= 0)
136
-        outbuf[2] |= 8;
136
+        pkt->data[2] |= 8;
137 137
 
138 138
     /* if RLE didn't make it smaller, go back to no compression */
139 139
     else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
... ...
@@ -145,7 +142,11 @@ static int targa_encode_frame(AVCodecContext *avctx,
145 145
      * aspect ratio and encoder ID fields available? */
146 146
     memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
147 147
 
148
-    return out + 26 - outbuf;
148
+    pkt->size   = out + 26 - pkt->data;
149
+    pkt->flags |= AV_PKT_FLAG_KEY;
150
+    *got_packet = 1;
151
+
152
+    return 0;
149 153
 }
150 154
 
151 155
 static av_cold int targa_encode_init(AVCodecContext *avctx)
... ...
@@ -154,6 +155,7 @@ static av_cold int targa_encode_init(AVCodecContext *avctx)
154 154
 
155 155
     avcodec_get_frame_defaults(&s->picture);
156 156
     s->picture.key_frame= 1;
157
+    s->picture.pict_type = AV_PICTURE_TYPE_I;
157 158
     avctx->coded_frame= &s->picture;
158 159
 
159 160
     return 0;
... ...
@@ -165,7 +167,7 @@ AVCodec ff_targa_encoder = {
165 165
     .id = CODEC_ID_TARGA,
166 166
     .priv_data_size = sizeof(TargaContext),
167 167
     .init = targa_encode_init,
168
-    .encode = targa_encode_frame,
168
+    .encode2 = targa_encode_frame,
169 169
     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_RGB555LE, PIX_FMT_GRAY8, PIX_FMT_NONE},
170 170
     .long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"),
171 171
 };
... ...
@@ -993,9 +993,8 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
993 993
         if (!ret && *got_packet_ptr) {
994 994
             if (!(avctx->codec->capabilities & CODEC_CAP_DELAY)) {
995 995
                 avpkt->pts = frame->pts;
996
-                avpkt->duration = av_rescale_q(frame->nb_samples,
997
-                                               (AVRational){ 1, avctx->sample_rate },
998
-                                               avctx->time_base);
996
+                avpkt->duration = ff_samples_to_time_base(avctx,
997
+                                                          frame->nb_samples);
999 998
             }
1000 999
             avpkt->dts = avpkt->pts;
1001 1000
         } else {
... ...
@@ -1053,9 +1052,8 @@ int attribute_align_arg avcodec_encode_audio2(AVCodecContext *avctx,
1053 1053
                    once all encoders supporting CODEC_CAP_SMALL_LAST_FRAME use
1054 1054
                    encode2() */
1055 1055
                 if (fs_tmp) {
1056
-                    avpkt->duration = av_rescale_q(avctx->frame_size,
1057
-                                                   (AVRational){ 1, avctx->sample_rate },
1058
-                                                   avctx->time_base);
1056
+                    avpkt->duration = ff_samples_to_time_base(avctx,
1057
+                                                              avctx->frame_size);
1059 1058
                 }
1060 1059
             }
1061 1060
             avpkt->size = ret;
... ...
@@ -1128,9 +1126,8 @@ int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx,
1128 1128
            this is needed because the avcodec_encode_audio() API does not have
1129 1129
            a way for the user to provide pts */
1130 1130
         if(avctx->sample_rate && avctx->time_base.num)
1131
-            frame->pts = av_rescale_q(avctx->internal->sample_count,
1132
-                                  (AVRational){ 1, avctx->sample_rate },
1133
-                                  avctx->time_base);
1131
+            frame->pts = ff_samples_to_time_base(avctx,
1132
+                                                avctx->internal->sample_count);
1134 1133
         else
1135 1134
             frame->pts = AV_NOPTS_VALUE;
1136 1135
         avctx->internal->sample_count += frame->nb_samples;
... ...
@@ -22,6 +22,7 @@
22 22
 
23 23
 #include "libavutil/intreadwrite.h"
24 24
 #include "avcodec.h"
25
+#include "internal.h"
25 26
 
26 27
 static av_cold int v410_encode_init(AVCodecContext *avctx)
27 28
 {
... ...
@@ -40,20 +41,19 @@ static av_cold int v410_encode_init(AVCodecContext *avctx)
40 40
     return 0;
41 41
 }
42 42
 
43
-static int v410_encode_frame(AVCodecContext *avctx, uint8_t *buf,
44
-                             int buf_size, void *data)
43
+static int v410_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
44
+                             const AVFrame *pic, int *got_packet)
45 45
 {
46
-    AVFrame *pic = data;
47
-    uint8_t *dst = buf;
46
+    uint8_t *dst;
48 47
     uint16_t *y, *u, *v;
49 48
     uint32_t val;
50
-    int i, j;
51
-    int output_size = 0;
49
+    int i, j, ret;
52 50
 
53
-    if (buf_size < avctx->width * avctx->height * 4) {
54
-        av_log(avctx, AV_LOG_ERROR, "Out buffer is too small.\n");
55
-        return AVERROR(ENOMEM);
51
+    if ((ret = ff_alloc_packet(pkt, avctx->width * avctx->height * 4)) < 0) {
52
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
53
+        return ret;
56 54
     }
55
+    dst = pkt->data;
57 56
 
58 57
     avctx->coded_frame->reference = 0;
59 58
     avctx->coded_frame->key_frame = 1;
... ...
@@ -70,14 +70,15 @@ static int v410_encode_frame(AVCodecContext *avctx, uint8_t *buf,
70 70
             val |= (uint32_t) v[j] << 22;
71 71
             AV_WL32(dst, val);
72 72
             dst += 4;
73
-            output_size += 4;
74 73
         }
75 74
         y += pic->linesize[0] >> 1;
76 75
         u += pic->linesize[1] >> 1;
77 76
         v += pic->linesize[2] >> 1;
78 77
     }
79 78
 
80
-    return output_size;
79
+    pkt->flags |= AV_PKT_FLAG_KEY;
80
+    *got_packet = 1;
81
+    return 0;
81 82
 }
82 83
 
83 84
 static av_cold int v410_encode_close(AVCodecContext *avctx)
... ...
@@ -92,7 +93,7 @@ AVCodec ff_v410_encoder = {
92 92
     .type         = AVMEDIA_TYPE_VIDEO,
93 93
     .id           = CODEC_ID_V410,
94 94
     .init         = v410_encode_init,
95
-    .encode       = v410_encode_frame,
95
+    .encode2      = v410_encode_frame,
96 96
     .close        = v410_encode_close,
97 97
     .pix_fmts     = (const enum PixelFormat[]){ PIX_FMT_YUV444P10, PIX_FMT_NONE },
98 98
     .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed 4:4:4 10-bit"),
... ...
@@ -830,9 +830,13 @@ cglobal deblock_v_chroma_8_mmxext, 5,6
830 830
 ; void ff_deblock_h_chroma( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 )
831 831
 ;-----------------------------------------------------------------------------
832 832
 cglobal deblock_h_chroma_8_mmxext, 5,7
833
-%if ARCH_X86_64
833
+%if UNIX64
834 834
     %define buf0 [rsp-24]
835 835
     %define buf1 [rsp-16]
836
+%elif WIN64
837
+    sub   rsp, 16
838
+    %define buf0 [rsp]
839
+    %define buf1 [rsp+8]
836 840
 %else
837 841
     %define buf0 r0m
838 842
     %define buf1 r2m
... ...
@@ -849,6 +853,9 @@ cglobal deblock_h_chroma_8_mmxext, 5,7
849 849
     movq  m0, buf0
850 850
     movq  m3, buf1
851 851
     TRANSPOSE8x4B_STORE PASS8ROWS(t5, r0, r1, t6)
852
+%if WIN64
853
+    add   rsp, 16
854
+%endif
852 855
     RET
853 856
 
854 857
 ALIGN 16
... ...
@@ -29,6 +29,7 @@
29 29
 
30 30
 #include "libavutil/intreadwrite.h"
31 31
 #include "avcodec.h"
32
+#include "internal.h"
32 33
 
33 34
 #include <zlib.h>
34 35
 
... ...
@@ -115,19 +116,18 @@ static int zmbv_me(ZmbvEncContext *c, uint8_t *src, int sstride, uint8_t *prev,
115 115
     return bv;
116 116
 }
117 117
 
118
-static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
118
+static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
119
+                        const AVFrame *pict, int *got_packet)
119 120
 {
120 121
     ZmbvEncContext * const c = avctx->priv_data;
121
-    AVFrame *pict = data;
122 122
     AVFrame * const p = &c->pic;
123
-    uint8_t *src, *prev;
123
+    uint8_t *src, *prev, *buf;
124 124
     uint32_t *palptr;
125
-    int len = 0;
126 125
     int keyframe, chpal;
127 126
     int fl;
128
-    int work_size = 0;
127
+    int work_size = 0, pkt_size;
129 128
     int bw, bh;
130
-    int i, j;
129
+    int i, j, ret;
131 130
 
132 131
     keyframe = !c->curfrm;
133 132
     c->curfrm++;
... ...
@@ -138,17 +138,6 @@ static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void
138 138
     p->key_frame= keyframe;
139 139
     chpal = !keyframe && memcmp(p->data[1], c->pal2, 1024);
140 140
 
141
-    fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
142
-    *buf++ = fl; len++;
143
-    if(keyframe){
144
-        deflateReset(&c->zstream);
145
-        *buf++ = 0; len++; // hi ver
146
-        *buf++ = 1; len++; // lo ver
147
-        *buf++ = 1; len++; // comp
148
-        *buf++ = 4; len++; // format - 8bpp
149
-        *buf++ = ZMBV_BLOCK; len++; // block width
150
-        *buf++ = ZMBV_BLOCK; len++; // block height
151
-    }
152 141
     palptr = (uint32_t*)p->data[1];
153 142
     src = p->data[0];
154 143
     prev = c->prev;
... ...
@@ -223,6 +212,9 @@ static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void
223 223
         src += p->linesize[0];
224 224
     }
225 225
 
226
+    if (keyframe)
227
+        deflateReset(&c->zstream);
228
+
226 229
     c->zstream.next_in = c->work_buf;
227 230
     c->zstream.avail_in = work_size;
228 231
     c->zstream.total_in = 0;
... ...
@@ -235,8 +227,29 @@ static int encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void
235 235
         return -1;
236 236
     }
237 237
 
238
+    pkt_size = c->zstream.total_out + 1 + 6*keyframe;
239
+    if ((ret = ff_alloc_packet(pkt, pkt_size)) < 0) {
240
+        av_log(avctx, AV_LOG_ERROR, "Error getting packet of size %d.\n", pkt_size);
241
+        return ret;
242
+    }
243
+    buf = pkt->data;
244
+
245
+    fl = (keyframe ? ZMBV_KEYFRAME : 0) | (chpal ? ZMBV_DELTAPAL : 0);
246
+    *buf++ = fl;
247
+    if (keyframe) {
248
+        *buf++ = 0; // hi ver
249
+        *buf++ = 1; // lo ver
250
+        *buf++ = 1; // comp
251
+        *buf++ = 4; // format - 8bpp
252
+        *buf++ = ZMBV_BLOCK; // block width
253
+        *buf++ = ZMBV_BLOCK; // block height
254
+    }
238 255
     memcpy(buf, c->comp_buf, c->zstream.total_out);
239
-    return len + c->zstream.total_out;
256
+
257
+    pkt->flags |= AV_PKT_FLAG_KEY*keyframe;
258
+    *got_packet = 1;
259
+
260
+    return 0;
240 261
 }
241 262
 
242 263
 
... ...
@@ -329,7 +342,7 @@ AVCodec ff_zmbv_encoder = {
329 329
     .id             = CODEC_ID_ZMBV,
330 330
     .priv_data_size = sizeof(ZmbvEncContext),
331 331
     .init           = encode_init,
332
-    .encode         = encode_frame,
332
+    .encode2        = encode_frame,
333 333
     .close          = encode_end,
334 334
     .pix_fmts = (const enum PixelFormat[]){PIX_FMT_PAL8, PIX_FMT_NONE},
335 335
     .long_name = NULL_IF_CONFIG_SMALL("Zip Motion Blocks Video"),
... ...
@@ -547,7 +547,7 @@ static int hls_read_header(AVFormatContext *s)
547 547
 
548 548
     c->first_packet = 1;
549 549
     c->first_timestamp = AV_NOPTS_VALUE;
550
-    c->seek_timestamp = AV_NOPTS_VALUE;
550
+    c->seek_timestamp  = AV_NOPTS_VALUE;
551 551
 
552 552
     return 0;
553 553
 fail:
... ...
@@ -609,16 +609,13 @@ start:
609 609
         if (var->needed && !var->pkt.data) {
610 610
             while (1) {
611 611
                 int64_t ts_diff;
612
+                AVStream *st;
612 613
                 ret = av_read_frame(var->ctx, &var->pkt);
613 614
                 if (ret < 0) {
614
-                    if (!url_feof(&var->pb)) {
615
+                    if (!url_feof(&var->pb))
615 616
                         return ret;
616
-                    } else {
617
-                        if ((var->cur_seq_no - var->start_seq_no) == (var->n_segments)) {
618
-                            return AVERROR_EOF;
619
-                        }
620
-                    }
621 617
                     reset_packet(&var->pkt);
618
+                    break;
622 619
                 } else {
623 620
                     if (c->first_timestamp == AV_NOPTS_VALUE)
624 621
                         c->first_timestamp = var->pkt.dts;
... ...
@@ -632,18 +629,14 @@ start:
632 632
                     break;
633 633
                 }
634 634
 
635
-                ts_diff = var->pkt.dts - c->seek_timestamp;
636
-                if (ts_diff >= 0) {
637
-                    if (c->seek_flags & AVSEEK_FLAG_ANY) {
638
-                        c->seek_timestamp = AV_NOPTS_VALUE;
639
-                        break;
640
-                    }
641
-
642
-                    /* Seek to keyframe */
643
-                    if (var->pkt.flags & AV_PKT_FLAG_KEY) {
644
-                        c->seek_timestamp = AV_NOPTS_VALUE;
645
-                        break;
646
-                    }
635
+                st = var->ctx->streams[var->pkt.stream_index];
636
+                ts_diff = av_rescale_rnd(var->pkt.dts, AV_TIME_BASE,
637
+                                         st->time_base.den, AV_ROUND_DOWN) -
638
+                          c->seek_timestamp;
639
+                if (ts_diff >= 0 && (c->seek_flags  & AVSEEK_FLAG_ANY ||
640
+                                     var->pkt.flags & AV_PKT_FLAG_KEY)) {
641
+                    c->seek_timestamp = AV_NOPTS_VALUE;
642
+                    break;
647 643
                 }
648 644
             }
649 645
         }
... ...
@@ -685,8 +678,12 @@ static int hls_read_seek(AVFormatContext *s, int stream_index,
685 685
     if ((flags & AVSEEK_FLAG_BYTE) || !c->variants[0]->finished)
686 686
         return AVERROR(ENOSYS);
687 687
 
688
-    c->seek_timestamp = timestamp;
689
-    c->seek_flags = flags;
688
+    c->seek_flags     = flags;
689
+    c->seek_timestamp = stream_index < 0 ? timestamp :
690
+                        av_rescale_rnd(timestamp, AV_TIME_BASE,
691
+                                       s->streams[stream_index]->time_base.den,
692
+                                       flags & AVSEEK_FLAG_BACKWARD ?
693
+                                       AV_ROUND_DOWN : AV_ROUND_UP);
690 694
     timestamp = av_rescale_rnd(timestamp, 1, stream_index >= 0 ?
691 695
                                s->streams[stream_index]->time_base.den :
692 696
                                AV_TIME_BASE, flags & AVSEEK_FLAG_BACKWARD ?
... ...
@@ -712,6 +709,10 @@ static int hls_read_seek(AVFormatContext *s, int stream_index,
712 712
         av_free_packet(&var->pkt);
713 713
         reset_packet(&var->pkt);
714 714
         var->pb.eof_reached = 0;
715
+        /* Clear any buffered data */
716
+        var->pb.buf_end = var->pb.buf_ptr = var->pb.buffer;
717
+        /* Reset the pos, to let the mpegts demuxer know we've seeked. */
718
+        var->pb.pos = 0;
715 719
 
716 720
         /* Locate the segment that contains the target timestamp */
717 721
         for (j = 0; j < var->n_segments; j++) {
... ...
@@ -723,7 +724,7 @@ static int hls_read_seek(AVFormatContext *s, int stream_index,
723 723
             }
724 724
             pos += var->segments[j]->duration;
725 725
         }
726
-        if (ret != 0)
726
+        if (ret)
727 727
             c->seek_timestamp = AV_NOPTS_VALUE;
728 728
     }
729 729
     return ret;
... ...
@@ -942,11 +942,10 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
942 942
         compute_frame_duration(&num, &den, st, pc, pkt);
943 943
         if (den && num) {
944 944
             pkt->duration = av_rescale_rnd(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num, AV_ROUND_DOWN);
945
-
946
-            if(pkt->duration != 0 && s->packet_buffer)
947
-                update_initial_durations(s, st, pkt);
948 945
         }
949 946
     }
947
+    if(pkt->duration != 0 && s->packet_buffer)
948
+        update_initial_durations(s, st, pkt);
950 949
 
951 950
     /* correct timestamps with byte offset if demuxers only have timestamps
952 951
        on packet boundaries */
... ...
@@ -1099,6 +1098,20 @@ static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
1099 1099
                 if (pkt->size) {
1100 1100
                 got_packet:
1101 1101
                     pkt->duration = 0;
1102
+                    if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
1103
+                        if (st->codec->sample_rate > 0) {
1104
+                            pkt->duration = av_rescale_q_rnd(st->parser->duration,
1105
+                                                             (AVRational){ 1, st->codec->sample_rate },
1106
+                                                             st->time_base,
1107
+                                                             AV_ROUND_DOWN);
1108
+                        }
1109
+                    } else if (st->codec->time_base.num != 0 &&
1110
+                               st->codec->time_base.den != 0) {
1111
+                        pkt->duration = av_rescale_q_rnd(st->parser->duration,
1112
+                                                         st->codec->time_base,
1113
+                                                         st->time_base,
1114
+                                                         AV_ROUND_DOWN);
1115
+                    }
1102 1116
                     pkt->stream_index = st->index;
1103 1117
                     pkt->pts = st->parser->pts;
1104 1118
                     pkt->dts = st->parser->dts;
... ...
@@ -154,7 +154,7 @@
154 154
  */
155 155
 
156 156
 #define LIBAVUTIL_VERSION_MAJOR 51
157
-#define LIBAVUTIL_VERSION_MINOR 39
157
+#define LIBAVUTIL_VERSION_MINOR 40
158 158
 #define LIBAVUTIL_VERSION_MICRO 100
159 159
 
160 160
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
... ...
@@ -131,10 +131,17 @@ int64_t av_rescale(int64_t a, int64_t b, int64_t c){
131 131
     return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
132 132
 }
133 133
 
134
-int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){
134
+int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq,
135
+                         enum AVRounding rnd)
136
+{
135 137
     int64_t b= bq.num * (int64_t)cq.den;
136 138
     int64_t c= cq.num * (int64_t)bq.den;
137
-    return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
139
+    return av_rescale_rnd(a, b, c, rnd);
140
+}
141
+
142
+int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
143
+{
144
+    return av_rescale_q_rnd(a, bq, cq, AV_ROUND_NEAR_INF);
138 145
 }
139 146
 
140 147
 int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b){
... ...
@@ -96,6 +96,12 @@ int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding) av_cons
96 96
 int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const;
97 97
 
98 98
 /**
99
+ * Rescale a 64-bit integer by 2 rational numbers with specified rounding.
100
+ */
101
+int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq,
102
+                         enum AVRounding) av_const;
103
+
104
+/**
99 105
  * Compare 2 timestamps each in its own timebases.
100 106
  * The result of the function is undefined if one of the timestamps
101 107
  * is outside the int64_t range when represented in the others timebase.
... ...
@@ -358,10 +358,11 @@ typedef struct SwsContext {
358 358
 #define U_TEMP                "11*8+4*4*256*2+24"
359 359
 #define V_TEMP                "11*8+4*4*256*2+32"
360 360
 #define Y_TEMP                "11*8+4*4*256*2+40"
361
-#define UV_OFF_PX             "11*8+4*4*256*2+48"
362
-#define UV_OFF_BYTE           "11*8+4*4*256*2+56"
363
-#define DITHER16              "11*8+4*4*256*2+64"
364
-#define DITHER32              "11*8+4*4*256*2+80"
361
+#define ALP_MMX_FILTER_OFFSET "11*8+4*4*256*2+48"
362
+#define UV_OFF_PX             "11*8+4*4*256*3+48"
363
+#define UV_OFF_BYTE           "11*8+4*4*256*3+56"
364
+#define DITHER16              "11*8+4*4*256*3+64"
365
+#define DITHER32              "11*8+4*4*256*3+80"
365 366
 
366 367
     DECLARE_ALIGNED(8, uint64_t, redDither);
367 368
     DECLARE_ALIGNED(8, uint64_t, greenDither);
... ...
@@ -383,6 +384,7 @@ typedef struct SwsContext {
383 383
     DECLARE_ALIGNED(8, uint64_t, u_temp);
384 384
     DECLARE_ALIGNED(8, uint64_t, v_temp);
385 385
     DECLARE_ALIGNED(8, uint64_t, y_temp);
386
+    int32_t alpMmxFilter[4 * MAX_FILTER_SIZE];
386 387
     // alignment of these values is not necessary, but merely here
387 388
     // to maintain the same offset across x8632 and x86-64. Once we
388 389
     // use proper offset macros in the asm, they can be removed.
... ...
@@ -421,7 +423,6 @@ typedef struct SwsContext {
421 421
 #if HAVE_VIS
422 422
     DECLARE_ALIGNED(8, uint64_t, sparc_coeffs)[10];
423 423
 #endif
424
-    int32_t alpMmxFilter[4 * MAX_FILTER_SIZE];
425 424
     int use_mmx_vfilter;
426 425
 
427 426
     /* function pointers for swScale() */
... ...
@@ -342,7 +342,7 @@ static void RENAME(yuv2rgb32_X_ar)(SwsContext *c, const int16_t *lumFilter,
342 342
         "movq                      %%mm2, "U_TEMP"(%0)  \n\t"
343 343
         "movq                      %%mm4, "V_TEMP"(%0)  \n\t"
344 344
         "movq                      %%mm5, "Y_TEMP"(%0)  \n\t"
345
-        YSCALEYUV2PACKEDX_ACCURATE_YA(LUM_MMX_FILTER_OFFSET)
345
+        YSCALEYUV2PACKEDX_ACCURATE_YA(ALP_MMX_FILTER_OFFSET)
346 346
         "movq               "Y_TEMP"(%0), %%mm5         \n\t"
347 347
         "psraw                        $3, %%mm1         \n\t"
348 348
         "psraw                        $3, %%mm7         \n\t"
... ...
@@ -372,7 +372,7 @@ static void RENAME(yuv2rgb32_X)(SwsContext *c, const int16_t *lumFilter,
372 372
     if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
373 373
         YSCALEYUV2PACKEDX
374 374
         YSCALEYUV2RGBX
375
-        YSCALEYUV2PACKEDX_YA(LUM_MMX_FILTER_OFFSET, %%mm0, %%mm3, %%mm6, %%mm1, %%mm7)
375
+        YSCALEYUV2PACKEDX_YA(ALP_MMX_FILTER_OFFSET, %%mm0, %%mm3, %%mm6, %%mm1, %%mm7)
376 376
         "psraw                        $3, %%mm1         \n\t"
377 377
         "psraw                        $3, %%mm7         \n\t"
378 378
         "packuswb                  %%mm7, %%mm1         \n\t"
... ...
@@ -1,4 +1,4 @@
1
-006f8dc92eb4f7bab82eded314ca1124 *./tests/data/acodec/adpcm_yam.wav
2
-266298 ./tests/data/acodec/adpcm_yam.wav
3
-c36a9d5a1e0ad57fbe9665a31373b7c1 *./tests/data/adpcm_yam.acodec.out.wav
4
-stddev: 1247.60 PSNR: 34.41 MAXDIFF:39895 bytes:  1064960/  1058400
1
+e9c14f701d25947317db9367b9dc772d *./tests/data/acodec/adpcm_yam.wav
2
+265274 ./tests/data/acodec/adpcm_yam.wav
3
+1488b5974fa040a65f0d407fc0224c6a *./tests/data/adpcm_yam.acodec.out.wav
4
+stddev: 1247.60 PSNR: 34.41 MAXDIFF:39895 bytes:  1060864/  1058400