Browse code

Merge remote-tracking branch 'qatar/master'

* qatar/master: (40 commits)
swf: check return values for av_get/new_packet().
wavpack: Don't shift minclip/maxclip
rtpenc: Expose the max packet size via an avoption
rtpenc: Move max_packet_size to a context variable
rtpenc: Add an option for not sending RTCP packets
lavc: drop encode() support for video.
snowenc: switch to encode2().
snowenc: don't abuse input picture for storing information.
a64multienc: switch to encode2().
a64multienc: don't write into output buffer when there's no output.
libxvid: switch to encode2().
tiffenc: switch to encode2().
tiffenc: properly forward error codes in encode_frame().
lavc: drop libdirac encoder.
gifenc: switch to encode2().
libvpxenc: switch to encode2().
flashsvenc: switch to encode2().
Remove libpostproc.
lcl: don't overwrite input memory.
swscale: take first/lastline over/underflows into account for MMX.
...

Conflicts:
.gitignore
Makefile
cmdutils.c
configure
doc/APIchanges
libavcodec/Makefile
libavcodec/allcodecs.c
libavcodec/libdiracenc.c
libavcodec/libxvidff.c
libavcodec/qtrleenc.c
libavcodec/tiffenc.c
libavcodec/utils.c
libavformat/mov.c
libavformat/movenc.c
libpostproc/Makefile
libpostproc/postprocess.c
libpostproc/postprocess.h
libpostproc/postprocess_altivec_template.c
libpostproc/postprocess_internal.h
libpostproc/postprocess_template.c
libswscale/swscale.c
libswscale/utils.c

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

Michael Niedermayer authored on 2012/02/24 10:57:18
Showing 44 changed files
... ...
@@ -1545,7 +1545,6 @@ h264_parser_select="golomb h264chroma h264dsp h264pred"
1545 1545
 libaacplus_encoder_deps="libaacplus"
1546 1546
 libcelt_decoder_deps="libcelt"
1547 1547
 libdirac_decoder_deps="libdirac !libschroedinger"
1548
-libdirac_encoder_deps="libdirac"
1549 1548
 libfaac_encoder_deps="libfaac"
1550 1549
 libgsm_decoder_deps="libgsm"
1551 1550
 libgsm_encoder_deps="libgsm"
... ...
@@ -3145,8 +3144,8 @@ enabled libcelt    && require libcelt celt/celt.h celt_decode -lcelt0 &&
3145 3145
                       { check_lib celt/celt.h celt_decoder_create_custom -lcelt0 ||
3146 3146
                         die "ERROR: libcelt version must be >= 0.11.0."; }
3147 3147
 enabled libdirac   && require_pkg_config dirac                          \
3148
-    "libdirac_decoder/dirac_parser.h libdirac_encoder/dirac_encoder.h"  \
3149
-    "dirac_decoder_init dirac_encoder_init"
3148
+    "libdirac_decoder/dirac_parser.h"  \
3149
+    "dirac_decoder_init"
3150 3150
 enabled libfaac    && require2 libfaac "stdint.h faac.h" faacEncGetVersion -lfaac
3151 3151
 enabled libfreetype && require_pkg_config freetype2 "ft2build.h freetype/freetype.h" FT_Init_FreeType
3152 3152
 enabled libgsm     && require  libgsm gsm/gsm.h gsm_create -lgsm
... ...
@@ -603,7 +603,7 @@ OBJS-$(CONFIG_MATROSKA_MUXER)          += xiph.o mpeg4audio.o \
603 603
                                           flacdec.o flacdata.o flac.o \
604 604
                                           mpegaudiodata.o vorbis_data.o
605 605
 OBJS-$(CONFIG_MP3_MUXER)               += mpegaudiodata.o mpegaudiodecheader.o
606
-OBJS-$(CONFIG_MOV_DEMUXER)             += mpeg4audio.o mpegaudiodata.o timecode.o
606
+OBJS-$(CONFIG_MOV_DEMUXER)             += mpeg4audio.o mpegaudiodata.o ac3tab.o timecode.o
607 607
 OBJS-$(CONFIG_MOV_MUXER)               += mpeg4audio.o mpegaudiodata.o
608 608
 OBJS-$(CONFIG_MPEGTS_MUXER)            += mpegvideo.o mpeg4audio.o
609 609
 OBJS-$(CONFIG_MPEGTS_DEMUXER)          += mpeg4audio.o mpegaudiodata.o
... ...
@@ -624,7 +624,6 @@ OBJS-$(CONFIG_WTV_DEMUXER)             += mpeg4audio.o mpegaudiodata.o
624 624
 OBJS-$(CONFIG_LIBAACPLUS_ENCODER)         += libaacplus.o
625 625
 OBJS-$(CONFIG_LIBCELT_DECODER)            += libcelt_dec.o
626 626
 OBJS-$(CONFIG_LIBDIRAC_DECODER)           += libdiracdec.o
627
-OBJS-$(CONFIG_LIBDIRAC_ENCODER)           += libdiracenc.o libdirac_libschro.o
628 627
 OBJS-$(CONFIG_LIBFAAC_ENCODER)            += libfaac.o
629 628
 OBJS-$(CONFIG_LIBGSM_DECODER)             += libgsm.o
630 629
 OBJS-$(CONFIG_LIBGSM_ENCODER)             += libgsm.o
... ...
@@ -50,6 +50,9 @@ typedef struct A64Context {
50 50
     uint8_t *mc_colram;
51 51
     uint8_t *mc_palette;
52 52
     int mc_pal_size;
53
+
54
+    /* pts of the next packet that will be output */
55
+    int64_t next_pts;
53 56
 } A64Context;
54 57
 
55 58
 #endif /* AVCODEC_A64ENC_H */
... ...
@@ -28,6 +28,7 @@
28 28
 #include "a64colors.h"
29 29
 #include "a64tables.h"
30 30
 #include "elbg.h"
31
+#include "internal.h"
31 32
 #include "libavutil/intreadwrite.h"
32 33
 
33 34
 #define DITHERSTEPS   8
... ...
@@ -221,6 +222,8 @@ static av_cold int a64multi_init_encoder(AVCodecContext *avctx)
221 221
     if (!avctx->codec_tag)
222 222
          avctx->codec_tag = AV_RL32("a64m");
223 223
 
224
+    c->next_pts = AV_NOPTS_VALUE;
225
+
224 226
     return 0;
225 227
 }
226 228
 
... ...
@@ -239,11 +242,10 @@ static void a64_compress_colram(unsigned char *buf, int *charmap, uint8_t *colra
239 239
     }
240 240
 }
241 241
 
242
-static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
243
-                                 int buf_size, void *data)
242
+static int a64multi_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
243
+                                 const AVFrame *pict, int *got_packet)
244 244
 {
245 245
     A64Context *c = avctx->priv_data;
246
-    AVFrame *pict = data;
247 246
     AVFrame *const p = (AVFrame *) & c->picture;
248 247
 
249 248
     int frame;
... ...
@@ -251,7 +253,8 @@ static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
251 251
     int b_height;
252 252
     int b_width;
253 253
 
254
-    int req_size;
254
+    int req_size, ret;
255
+    uint8_t *buf;
255 256
 
256 257
     int *charmap     = c->mc_charmap;
257 258
     uint8_t *colram  = c->mc_colram;
... ...
@@ -274,7 +277,7 @@ static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
274 274
     }
275 275
 
276 276
     /* no data, means end encoding asap */
277
-    if (!data) {
277
+    if (!pict) {
278 278
         /* all done, end encoding */
279 279
         if (!c->mc_lifetime) return 0;
280 280
         /* no more frames in queue, prepare to flush remaining frames */
... ...
@@ -292,6 +295,8 @@ static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
292 292
             p->key_frame = 1;
293 293
             to_meta_with_crop(avctx, p, meta + 32000 * c->mc_frame_counter);
294 294
             c->mc_frame_counter++;
295
+            if (c->next_pts == AV_NOPTS_VALUE)
296
+                c->next_pts = pict->pts;
295 297
             /* lifetime is not reached so wait for next frame first */
296 298
             return 0;
297 299
         }
... ...
@@ -302,6 +307,13 @@ static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
302 302
         req_size = 0;
303 303
         /* any frames to encode? */
304 304
         if (c->mc_lifetime) {
305
+            req_size = charset_size + c->mc_lifetime*(screen_size + colram_size);
306
+            if ((ret = ff_alloc_packet(pkt, req_size)) < 0) {
307
+                av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", req_size);
308
+                return ret;
309
+            }
310
+            buf = pkt->data;
311
+
305 312
             /* calc optimal new charset + charmaps */
306 313
             ff_init_elbg(meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
307 314
             ff_do_elbg  (meta, 32, 1000 * c->mc_lifetime, best_cb, CHARSET_CHARS, 50, charmap, &c->randctx);
... ...
@@ -310,15 +322,12 @@ static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
310 310
             render_charset(avctx, charset, colram);
311 311
 
312 312
             /* copy charset to buf */
313
-            memcpy(buf,charset, charset_size);
313
+            memcpy(buf, charset, charset_size);
314 314
 
315 315
             /* advance pointers */
316 316
             buf      += charset_size;
317 317
             charset  += charset_size;
318
-            req_size += charset_size;
319 318
         }
320
-        /* no charset so clean buf */
321
-        else memset(buf, 0, charset_size);
322 319
 
323 320
         /* write x frames to buf */
324 321
         for (frame = 0; frame < c->mc_lifetime; frame++) {
... ...
@@ -351,11 +360,12 @@ static int a64multi_encode_frame(AVCodecContext *avctx, unsigned char *buf,
351 351
         /* reset counter */
352 352
         c->mc_frame_counter = 0;
353 353
 
354
-        if (req_size > buf_size) {
355
-            av_log(avctx, AV_LOG_ERROR, "buf size too small (need %d, got %d)\n", req_size, buf_size);
356
-            return -1;
357
-        }
358
-        return req_size;
354
+        pkt->pts = pkt->dts = c->next_pts;
355
+        c->next_pts         = AV_NOPTS_VALUE;
356
+
357
+        pkt->size   = req_size;
358
+        pkt->flags |= AV_PKT_FLAG_KEY;
359
+        *got_packet = !!req_size;
359 360
     }
360 361
     return 0;
361 362
 }
... ...
@@ -366,7 +376,7 @@ AVCodec ff_a64multi_encoder = {
366 366
     .id             = CODEC_ID_A64_MULTI,
367 367
     .priv_data_size = sizeof(A64Context),
368 368
     .init           = a64multi_init_encoder,
369
-    .encode         = a64multi_encode_frame,
369
+    .encode2        = a64multi_encode_frame,
370 370
     .close          = a64multi_close_encoder,
371 371
     .pix_fmts       = (const enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
372 372
     .long_name      = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64"),
... ...
@@ -379,7 +389,7 @@ AVCodec ff_a64multi5_encoder = {
379 379
     .id             = CODEC_ID_A64_MULTI5,
380 380
     .priv_data_size = sizeof(A64Context),
381 381
     .init           = a64multi_init_encoder,
382
-    .encode         = a64multi_encode_frame,
382
+    .encode2        = a64multi_encode_frame,
383 383
     .close          = a64multi_close_encoder,
384 384
     .pix_fmts       = (const enum PixelFormat[]) {PIX_FMT_GRAY8, PIX_FMT_NONE},
385 385
     .long_name      = NULL_IF_CONFIG_SMALL("Multicolor charset for Commodore 64, extended with 5th color (colram)"),
... ...
@@ -134,7 +134,7 @@ int avpriv_ac3_parse_header(GetBitContext *gbc, AC3HeaderInfo *hdr)
134 134
                         (hdr->num_blocks * 256.0));
135 135
         hdr->channels = ff_ac3_channels_tab[hdr->channel_mode] + hdr->lfe_on;
136 136
     }
137
-    hdr->channel_layout = ff_ac3_channel_layout_tab[hdr->channel_mode];
137
+    hdr->channel_layout = avpriv_ac3_channel_layout_tab[hdr->channel_mode];
138 138
     if (hdr->lfe_on)
139 139
         hdr->channel_layout |= AV_CH_LOW_FREQUENCY;
140 140
 
... ...
@@ -1378,7 +1378,7 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data,
1378 1378
                 avctx->request_channels < s->channels) {
1379 1379
             s->out_channels = avctx->request_channels;
1380 1380
             s->output_mode  = avctx->request_channels == 1 ? AC3_CHMODE_MONO : AC3_CHMODE_STEREO;
1381
-            s->channel_layout = ff_ac3_channel_layout_tab[s->output_mode];
1381
+            s->channel_layout = avpriv_ac3_channel_layout_tab[s->output_mode];
1382 1382
         }
1383 1383
         avctx->channels       = s->out_channels;
1384 1384
         avctx->channel_layout = s->channel_layout;
... ...
@@ -84,7 +84,7 @@ const uint8_t ff_ac3_channels_tab[8] = {
84 84
 /**
85 85
  * Map audio coding mode (acmod) to channel layout mask.
86 86
  */
87
-const uint16_t ff_ac3_channel_layout_tab[8] = {
87
+const uint16_t avpriv_ac3_channel_layout_tab[8] = {
88 88
     AV_CH_LAYOUT_STEREO,
89 89
     AV_CH_LAYOUT_MONO,
90 90
     AV_CH_LAYOUT_STEREO,
... ...
@@ -33,7 +33,7 @@
33 33
 
34 34
 extern const uint16_t ff_ac3_frame_size_tab[38][3];
35 35
 extern const uint8_t  ff_ac3_channels_tab[8];
36
-extern const uint16_t ff_ac3_channel_layout_tab[8];
36
+extern const uint16_t avpriv_ac3_channel_layout_tab[8];
37 37
 extern const uint8_t  ff_ac3_enc_channel_map[8][2][6];
38 38
 extern const uint8_t  ff_ac3_dec_channel_map[8][2][6];
39 39
 extern const uint16_t ff_ac3_sample_rate_tab[3];
... ...
@@ -400,7 +400,7 @@ void avcodec_register_all(void)
400 400
     /* external libraries */
401 401
     REGISTER_ENCODER (LIBAACPLUS, libaacplus);
402 402
     REGISTER_DECODER (LIBCELT, libcelt);
403
-    REGISTER_ENCDEC  (LIBDIRAC, libdirac);
403
+    REGISTER_DECODER (LIBDIRAC, libdirac);
404 404
     REGISTER_ENCODER (LIBFAAC, libfaac);
405 405
     REGISTER_ENCDEC  (LIBGSM, libgsm);
406 406
     REGISTER_ENCDEC  (LIBGSM_MS, libgsm_ms);
... ...
@@ -325,10 +325,12 @@ static inline int decode_mb(ASV1Context *a, DCTELEM block[6][64]){
325 325
     return 0;
326 326
 }
327 327
 
328
+#define MAX_MB_SIZE (30*16*16*3/2/8)
329
+
328 330
 static inline int encode_mb(ASV1Context *a, DCTELEM block[6][64]){
329 331
     int i;
330 332
 
331
-    if(a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb)>>3) < 30*16*16*3/2/8){
333
+    if (a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb)>>3) < MAX_MB_SIZE) {
332 334
         av_log(a->avctx, AV_LOG_ERROR, "encoded frame too large\n");
333 335
         return -1;
334 336
     }
... ...
@@ -461,14 +463,22 @@ static int decode_frame(AVCodecContext *avctx,
461 461
 }
462 462
 
463 463
 #if CONFIG_ASV1_ENCODER || CONFIG_ASV2_ENCODER
464
-static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
464
+static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
465
+                        const AVFrame *pict, int *got_packet)
466
+{
465 467
     ASV1Context * const a = avctx->priv_data;
466
-    AVFrame *pict = data;
467 468
     AVFrame * const p= &a->picture;
468
-    int size;
469
+    int size, ret;
469 470
     int mb_x, mb_y;
470 471
 
471
-    init_put_bits(&a->pb, buf, buf_size);
472
+    if (!pkt->data &&
473
+        (ret = av_new_packet(pkt, a->mb_height*a->mb_width*MAX_MB_SIZE +
474
+                                  FF_MIN_BUFFER_SIZE)) < 0) {
475
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
476
+        return ret;
477
+    }
478
+
479
+    init_put_bits(&a->pb, pkt->data, pkt->size);
472 480
 
473 481
     *p = *pict;
474 482
     p->pict_type= AV_PICTURE_TYPE_I;
... ...
@@ -505,14 +515,18 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
505 505
     size= put_bits_count(&a->pb)/32;
506 506
 
507 507
     if(avctx->codec_id == CODEC_ID_ASV1)
508
-        a->dsp.bswap_buf((uint32_t*)buf, (uint32_t*)buf, size);
508
+        a->dsp.bswap_buf((uint32_t*)pkt->data, (uint32_t*)pkt->data, size);
509 509
     else{
510 510
         int i;
511 511
         for(i=0; i<4*size; i++)
512
-            buf[i]= av_reverse[ buf[i] ];
512
+            pkt->data[i] = av_reverse[pkt->data[i]];
513 513
     }
514 514
 
515
-    return size*4;
515
+    pkt->size   = size*4;
516
+    pkt->flags |= AV_PKT_FLAG_KEY;
517
+    *got_packet = 1;
518
+
519
+    return 0;
516 520
 }
517 521
 #endif /* CONFIG_ASV1_ENCODER || CONFIG_ASV2_ENCODER */
518 522
 
... ...
@@ -634,7 +648,7 @@ AVCodec ff_asv1_encoder = {
634 634
     .id             = CODEC_ID_ASV1,
635 635
     .priv_data_size = sizeof(ASV1Context),
636 636
     .init           = encode_init,
637
-    .encode         = encode_frame,
637
+    .encode2        = encode_frame,
638 638
     //encode_end,
639 639
     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
640 640
     .long_name= NULL_IF_CONFIG_SMALL("ASUS V1"),
... ...
@@ -648,7 +662,7 @@ AVCodec ff_asv2_encoder = {
648 648
     .id             = CODEC_ID_ASV2,
649 649
     .priv_data_size = sizeof(ASV1Context),
650 650
     .init           = encode_init,
651
-    .encode         = encode_frame,
651
+    .encode2        = encode_frame,
652 652
     //encode_end,
653 653
     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
654 654
     .long_name= NULL_IF_CONFIG_SMALL("ASUS V2"),
... ...
@@ -1135,17 +1135,25 @@ static int encode_slice(AVCodecContext *c, void *arg){
1135 1135
     return 0;
1136 1136
 }
1137 1137
 
1138
-static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
1138
+static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1139
+                        const AVFrame *pict, int *got_packet)
1140
+{
1139 1141
     FFV1Context *f = avctx->priv_data;
1140 1142
     RangeCoder * const c= &f->slice_context[0]->c;
1141
-    AVFrame *pict = data;
1142 1143
     AVFrame * const p= &f->picture;
1143 1144
     int used_count= 0;
1144 1145
     uint8_t keystate=128;
1145 1146
     uint8_t *buf_p;
1146
-    int i;
1147
+    int i, ret;
1148
+
1149
+    if (!pkt->data &&
1150
+        (ret = av_new_packet(pkt, avctx->width*avctx->height*((8*2+1+1)*4)/8
1151
+                                  + FF_MIN_BUFFER_SIZE)) < 0) {
1152
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
1153
+        return ret;
1154
+    }
1147 1155
 
1148
-    ff_init_range_encoder(c, buf, buf_size);
1156
+    ff_init_range_encoder(c, pkt->data, pkt->size);
1149 1157
     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
1150 1158
 
1151 1159
     *p = *pict;
... ...
@@ -1165,7 +1173,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1165 1165
     if(!f->ac){
1166 1166
         used_count += ff_rac_terminate(c);
1167 1167
 //printf("pos=%d\n", used_count);
1168
-        init_put_bits(&f->slice_context[0]->pb, buf + used_count, buf_size - used_count);
1168
+        init_put_bits(&f->slice_context[0]->pb, pkt->data + used_count, pkt->size - used_count);
1169 1169
     }else if (f->ac>1){
1170 1170
         int i;
1171 1171
         for(i=1; i<256; i++){
... ...
@@ -1176,8 +1184,8 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1176 1176
 
1177 1177
     for(i=1; i<f->slice_count; i++){
1178 1178
         FFV1Context *fs= f->slice_context[i];
1179
-        uint8_t *start= buf + (buf_size-used_count)*i/f->slice_count;
1180
-        int len= buf_size/f->slice_count;
1179
+        uint8_t *start = pkt->data + (pkt->size-used_count)*i/f->slice_count;
1180
+        int len = pkt->size/f->slice_count;
1181 1181
 
1182 1182
         if(fs->ac){
1183 1183
             ff_init_range_encoder(&fs->c, start, len);
... ...
@@ -1187,7 +1195,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1187 1187
     }
1188 1188
     avctx->execute(avctx, encode_slice, &f->slice_context[0], NULL, f->slice_count, sizeof(void*));
1189 1189
 
1190
-    buf_p=buf;
1190
+    buf_p = pkt->data;
1191 1191
     for(i=0; i<f->slice_count; i++){
1192 1192
         FFV1Context *fs= f->slice_context[i];
1193 1193
         int bytes;
... ...
@@ -1202,7 +1210,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1202 1202
             used_count= 0;
1203 1203
         }
1204 1204
         if(i>0){
1205
-            av_assert0(bytes < buf_size/f->slice_count);
1205
+            av_assert0(bytes < pkt->size/f->slice_count);
1206 1206
             memmove(buf_p, fs->ac ? fs->c.bytestream_start : fs->pb.buf, bytes);
1207 1207
             av_assert0(bytes < (1<<24));
1208 1208
             AV_WB24(buf_p+bytes, bytes);
... ...
@@ -1255,7 +1263,11 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1255 1255
         avctx->stats_out[0] = '\0';
1256 1256
 
1257 1257
     f->picture_number++;
1258
-    return buf_p-buf;
1258
+    pkt->size   = buf_p - pkt->data;
1259
+    pkt->flags |= AV_PKT_FLAG_KEY*p->key_frame;
1260
+    *got_packet = 1;
1261
+
1262
+    return 0;
1259 1263
 }
1260 1264
 #endif /* CONFIG_FFV1_ENCODER */
1261 1265
 
... ...
@@ -1843,7 +1855,7 @@ AVCodec ff_ffv1_encoder = {
1843 1843
     .id             = CODEC_ID_FFV1,
1844 1844
     .priv_data_size = sizeof(FFV1Context),
1845 1845
     .init           = encode_init,
1846
-    .encode         = encode_frame,
1846
+    .encode2        = encode_frame,
1847 1847
     .close          = common_end,
1848 1848
     .capabilities = CODEC_CAP_SLICE_THREADS,
1849 1849
     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUVA420P, PIX_FMT_YUV444P, PIX_FMT_YUVA444P, PIX_FMT_YUV440P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_0RGB32, PIX_FMT_RGB32, PIX_FMT_YUV420P16, PIX_FMT_YUV422P16, PIX_FMT_YUV444P16, PIX_FMT_YUV420P9, PIX_FMT_YUV420P10, PIX_FMT_YUV422P10, PIX_FMT_GRAY16, PIX_FMT_GRAY8, PIX_FMT_NONE},
... ...
@@ -33,6 +33,7 @@
33 33
 
34 34
 #include <limits.h>
35 35
 
36
+#include "libavutil/audioconvert.h"
36 37
 #include "libavutil/crc.h"
37 38
 #include "avcodec.h"
38 39
 #include "internal.h"
... ...
@@ -62,6 +63,15 @@ typedef struct FLACContext {
62 62
     int32_t *decoded[FLAC_MAX_CHANNELS];    ///< decoded samples
63 63
 } FLACContext;
64 64
 
65
+static const int64_t flac_channel_layouts[6] = {
66
+    AV_CH_LAYOUT_MONO,
67
+    AV_CH_LAYOUT_STEREO,
68
+    AV_CH_LAYOUT_SURROUND,
69
+    AV_CH_LAYOUT_QUAD,
70
+    AV_CH_LAYOUT_5POINT0,
71
+    AV_CH_LAYOUT_5POINT1
72
+};
73
+
65 74
 static void allocate_buffers(FLACContext *s);
66 75
 
67 76
 int avpriv_flac_is_extradata_valid(AVCodecContext *avctx,
... ...
@@ -120,6 +130,9 @@ static av_cold int flac_decode_init(AVCodecContext *avctx)
120 120
     avcodec_get_frame_defaults(&s->frame);
121 121
     avctx->coded_frame = &s->frame;
122 122
 
123
+    if (avctx->channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
124
+        avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
125
+
123 126
     return 0;
124 127
 }
125 128
 
... ...
@@ -49,6 +49,7 @@
49 49
 #include <zlib.h>
50 50
 
51 51
 #include "avcodec.h"
52
+#include "internal.h"
52 53
 #include "put_bits.h"
53 54
 #include "bytestream.h"
54 55
 
... ...
@@ -194,11 +195,10 @@ static int encode_bitstream(FlashSVContext *s, AVFrame *p, uint8_t *buf,
194 194
 }
195 195
 
196 196
 
197
-static int flashsv_encode_frame(AVCodecContext *avctx, uint8_t *buf,
198
-                                int buf_size, void *data)
197
+static int flashsv_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
198
+                                const AVFrame *pict, int *got_packet)
199 199
 {
200 200
     FlashSVContext * const s = avctx->priv_data;
201
-    AVFrame *pict = data;
202 201
     AVFrame * const p = &s->frame;
203 202
     uint8_t *pfptr;
204 203
     int res;
... ...
@@ -228,15 +228,15 @@ static int flashsv_encode_frame(AVCodecContext *avctx, uint8_t *buf,
228 228
         I_frame = 1;
229 229
     }
230 230
 
231
-    if (buf_size < s->image_width * s->image_height * 3) {
231
+    if ((res = ff_alloc_packet(pkt, s->image_width * s->image_height * 3)) < 0) {
232 232
         //Conservative upper bound check for compressed data
233
-        av_log(avctx, AV_LOG_ERROR, "buf_size %d <  %d\n",
234
-               buf_size, s->image_width * s->image_height * 3);
235
-        return -1;
233
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n",
234
+               s->image_width * s->image_height * 3);
235
+        return res;
236 236
     }
237 237
 
238
-    res = encode_bitstream(s, p, buf, buf_size, opt_w * 16, opt_h * 16,
239
-                           pfptr, &I_frame);
238
+    pkt->size = encode_bitstream(s, p, pkt->data, pkt->size, opt_w * 16, opt_h * 16,
239
+                                 pfptr, &I_frame);
240 240
 
241 241
     //save the current frame
242 242
     if (p->linesize[0] > 0)
... ...
@@ -259,7 +259,11 @@ static int flashsv_encode_frame(AVCodecContext *avctx, uint8_t *buf,
259 259
 
260 260
     avctx->coded_frame = p;
261 261
 
262
-    return res;
262
+    if (p->key_frame)
263
+        pkt->flags |= AV_PKT_FLAG_KEY;
264
+    *got_packet = 1;
265
+
266
+    return 0;
263 267
 }
264 268
 
265 269
 static av_cold int flashsv_encode_end(AVCodecContext *avctx)
... ...
@@ -281,7 +285,7 @@ AVCodec ff_flashsv_encoder = {
281 281
     .id             = CODEC_ID_FLASHSV,
282 282
     .priv_data_size = sizeof(FlashSVContext),
283 283
     .init           = flashsv_encode_init,
284
-    .encode         = flashsv_encode_frame,
284
+    .encode2        = flashsv_encode_frame,
285 285
     .close          = flashsv_encode_end,
286 286
     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_NONE},
287 287
     .long_name      = NULL_IF_CONFIG_SMALL("Flash Screen Video"),
... ...
@@ -43,6 +43,7 @@
43 43
 
44 44
 #include "avcodec.h"
45 45
 #include "bytestream.h"
46
+#include "internal.h"
46 47
 #include "lzw.h"
47 48
 
48 49
 /* The GIF format uses reversed order for bitstreams... */
... ...
@@ -155,20 +156,32 @@ static av_cold int gif_encode_init(AVCodecContext *avctx)
155 155
 }
156 156
 
157 157
 /* better than nothing gif encoder */
158
-static int gif_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data)
158
+static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
159
+                            const AVFrame *pict, int *got_packet)
159 160
 {
160 161
     GIFContext *s = avctx->priv_data;
161
-    AVFrame *pict = data;
162 162
     AVFrame *const p = (AVFrame *)&s->picture;
163
-    uint8_t *outbuf_ptr = outbuf;
164
-    uint8_t *end = outbuf + buf_size;
163
+    uint8_t *outbuf_ptr, *end;
164
+    int ret;
165
+
166
+    if ((ret = ff_alloc_packet(pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0) {
167
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
168
+        return ret;
169
+    }
170
+    outbuf_ptr = pkt->data;
171
+    end        = pkt->data + pkt->size;
165 172
 
166 173
     *p = *pict;
167 174
     p->pict_type = AV_PICTURE_TYPE_I;
168 175
     p->key_frame = 1;
169 176
     gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
170 177
     gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
171
-    return outbuf_ptr - outbuf;
178
+
179
+    pkt->size   = outbuf_ptr - pkt->data;
180
+    pkt->flags |= AV_PKT_FLAG_KEY;
181
+    *got_packet = 1;
182
+
183
+    return 0;
172 184
 }
173 185
 
174 186
 static int gif_encode_close(AVCodecContext *avctx)
... ...
@@ -186,7 +199,7 @@ AVCodec ff_gif_encoder = {
186 186
     .id             = CODEC_ID_GIF,
187 187
     .priv_data_size = sizeof(GIFContext),
188 188
     .init           = gif_encode_init,
189
-    .encode         = gif_encode_frame,
189
+    .encode2        = gif_encode_frame,
190 190
     .close          = gif_encode_close,
191 191
     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8, PIX_FMT_NONE},
192 192
     .long_name= NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
... ...
@@ -28,6 +28,7 @@
28 28
 #include "avcodec.h"
29 29
 #include "get_bits.h"
30 30
 #include "golomb.h"
31
+#include "internal.h"
31 32
 #include "mathops.h"
32 33
 #include "dsputil.h"
33 34
 #include "mjpeg.h"
... ...
@@ -227,23 +228,19 @@ static void ls_store_lse(JLSState *state, PutBitContext *pb){
227 227
     put_bits(pb, 16, state->reset);
228 228
 }
229 229
 
230
-static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
230
+static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
231
+                             const AVFrame *pict, int *got_packet)
232
+{
231 233
     JpeglsContext * const s = avctx->priv_data;
232
-    AVFrame *pict = data;
233 234
     AVFrame * const p= (AVFrame*)&s->picture;
234 235
     const int near = avctx->prediction_method;
235 236
     PutBitContext pb, pb2;
236 237
     GetBitContext gb;
237 238
     uint8_t *buf2, *zero, *cur, *last;
238 239
     JLSState *state;
239
-    int i, size;
240
+    int i, size, ret;
240 241
     int comps;
241 242
 
242
-    buf2 = av_malloc(buf_size);
243
-
244
-    init_put_bits(&pb, buf, buf_size);
245
-    init_put_bits(&pb2, buf2, buf_size);
246
-
247 243
     *p = *pict;
248 244
     p->pict_type= AV_PICTURE_TYPE_I;
249 245
     p->key_frame= 1;
... ...
@@ -253,6 +250,17 @@ static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_
253 253
     else
254 254
         comps = 3;
255 255
 
256
+    if ((ret = ff_alloc_packet(pkt, avctx->width*avctx->height*comps*4 +
257
+                                    FF_MIN_BUFFER_SIZE)) < 0) {
258
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
259
+        return ret;
260
+    }
261
+
262
+    buf2 = av_malloc(pkt->size);
263
+
264
+    init_put_bits(&pb, pkt->data, pkt->size);
265
+    init_put_bits(&pb2, buf2, pkt->size);
266
+
256 267
     /* write our own JPEG header, can't use mjpeg_picture_header */
257 268
     put_marker(&pb, SOI);
258 269
     put_marker(&pb, SOF48);
... ...
@@ -366,7 +374,10 @@ static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_
366 366
 
367 367
     emms_c();
368 368
 
369
-    return put_bits_count(&pb) >> 3;
369
+    pkt->size   = put_bits_count(&pb) >> 3;
370
+    pkt->flags |= AV_PKT_FLAG_KEY;
371
+    *got_packet = 1;
372
+    return 0;
370 373
 }
371 374
 
372 375
 static av_cold int encode_init_ls(AVCodecContext *ctx) {
... ...
@@ -388,7 +399,7 @@ AVCodec ff_jpegls_encoder = { //FIXME avoid MPV_* lossless JPEG should not need
388 388
     .id             = CODEC_ID_JPEGLS,
389 389
     .priv_data_size = sizeof(JpeglsContext),
390 390
     .init           = encode_init_ls,
391
-    .encode         = encode_picture_ls,
391
+    .encode2        = encode_picture_ls,
392 392
     .pix_fmts       = (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, PIX_FMT_NONE},
393 393
     .long_name      = NULL_IF_CONFIG_SMALL("JPEG-LS"),
394 394
 };
... ...
@@ -242,9 +242,14 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPac
242 242
          * gives a file with ZLIB fourcc, but frame is really uncompressed.
243 243
          * To be sure that's true check also frame size */
244 244
         if (c->compression == COMP_ZLIB_NORMAL && c->imgtype == IMGTYPE_RGB24 &&
245
-            len == width * height * 3)
246
-            break;
247
-        if (c->flags & FLAG_MULTITHREAD) {
245
+            len == width * height * 3) {
246
+            if (c->flags & FLAG_PNGFILTER) {
247
+                memcpy(c->decomp_buf, encoded, len);
248
+                encoded = c->decomp_buf;
249
+            } else {
250
+                break;
251
+            }
252
+        } else if (c->flags & FLAG_MULTITHREAD) {
248 253
             int ret;
249 254
             mthread_inlen = AV_RL32(encoded);
250 255
             mthread_inlen = FFMIN(mthread_inlen, len - 8);
... ...
@@ -68,12 +68,20 @@ typedef struct LclEncContext {
68 68
  * Encode a frame
69 69
  *
70 70
  */
71
-static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
71
+static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
72
+                        const AVFrame *pict, int *got_packet)
73
+{
72 74
     LclEncContext *c = avctx->priv_data;
73
-    AVFrame *pict = data;
74 75
     AVFrame * const p = &c->pic;
75
-    int i;
76
+    int i, ret;
76 77
     int zret; // Zlib return code
78
+    int max_size = deflateBound(&c->zstream, avctx->width * avctx->height * 3);
79
+
80
+    if (!pkt->data &&
81
+        (ret = av_new_packet(pkt, max_size)) < 0) {
82
+            av_log(avctx, AV_LOG_ERROR, "Error allocating packet of size %d.\n", max_size);
83
+            return ret;
84
+    }
77 85
 
78 86
     *p = *pict;
79 87
     p->pict_type= AV_PICTURE_TYPE_I;
... ...
@@ -89,8 +97,8 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
89 89
         av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
90 90
         return -1;
91 91
     }
92
-    c->zstream.next_out = buf;
93
-    c->zstream.avail_out = buf_size;
92
+    c->zstream.next_out  = pkt->data;
93
+    c->zstream.avail_out = pkt->size;
94 94
 
95 95
     for(i = avctx->height - 1; i >= 0; i--) {
96 96
         c->zstream.next_in = p->data[0]+p->linesize[0]*i;
... ...
@@ -107,7 +115,11 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
107 107
         return -1;
108 108
     }
109 109
 
110
-    return c->zstream.total_out;
110
+    pkt->size   = c->zstream.total_out;
111
+    pkt->flags |= AV_PKT_FLAG_KEY;
112
+    *got_packet = 1;
113
+
114
+    return 0;
111 115
 }
112 116
 
113 117
 /*
... ...
@@ -176,7 +188,7 @@ AVCodec ff_zlib_encoder = {
176 176
     .id             = CODEC_ID_ZLIB,
177 177
     .priv_data_size = sizeof(LclEncContext),
178 178
     .init           = encode_init,
179
-    .encode         = encode_frame,
179
+    .encode2        = encode_frame,
180 180
     .close          = encode_end,
181 181
     .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_BGR24, PIX_FMT_NONE },
182 182
     .long_name = NULL_IF_CONFIG_SMALL("LCL (LossLess Codec Library) ZLIB"),
183 183
deleted file mode 100644
... ...
@@ -1,405 +0,0 @@
1
-/*
2
- * Dirac encoding support via libdirac library
3
- * Copyright (c) 2005 BBC, Andrew Kennedy <dirac at rd dot bbc dot co dot uk>
4
- * Copyright (c) 2006-2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
5
- *
6
- * This file is part of FFmpeg.
7
- *
8
- * FFmpeg is free software; you can redistribute it and/or
9
- * modify it under the terms of the GNU Lesser General Public
10
- * License as published by the Free Software Foundation; either
11
- * version 2.1 of the License, or (at your option) any later version.
12
- *
13
- * FFmpeg is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
- * Lesser General Public License for more details.
17
- *
18
- * You should have received a copy of the GNU Lesser General Public
19
- * License along with FFmpeg; if not, write to the Free Software
20
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
- */
22
-
23
-/**
24
-* @file
25
-* Dirac encoding support via libdirac library; more details about the
26
-* Dirac project can be found at http://dirac.sourceforge.net/.
27
-* The libdirac_encoder library implements Dirac specification version 2.2
28
-* (http://dirac.sourceforge.net/specification.html).
29
-*/
30
-
31
-#include "libdirac_libschro.h"
32
-#include "libdirac.h"
33
-
34
-#undef NDEBUG
35
-#include <assert.h>
36
-
37
-
38
-#include <libdirac_encoder/dirac_encoder.h>
39
-
40
-/** Dirac encoder private data */
41
-typedef struct DiracEncoderParams {
42
-    /** Dirac encoder context */
43
-    dirac_encoder_context_t enc_ctx;
44
-
45
-    /** frame being encoded */
46
-    AVFrame picture;
47
-
48
-    /** frame size */
49
-    int frame_size;
50
-
51
-    /** Dirac encoder handle */
52
-    dirac_encoder_t* p_encoder;
53
-
54
-    /** input frame buffer */
55
-    unsigned char *p_in_frame_buf;
56
-
57
-    /** buffer to store encoder output before writing it to the frame queue */
58
-    unsigned char *enc_buf;
59
-
60
-    /** size of encoder buffer */
61
-    int enc_buf_size;
62
-
63
-    /** queue storing encoded frames */
64
-    DiracSchroQueue enc_frame_queue;
65
-
66
-    /** end of sequence signalled by user, 0 - false, 1 - true */
67
-    int eos_signalled;
68
-
69
-    /** end of sequence returned by encoder, 0 - false, 1 - true */
70
-    int eos_pulled;
71
-} DiracEncoderParams;
72
-
73
-/**
74
-* Works out Dirac-compatible chroma format.
75
-*/
76
-static dirac_chroma_t GetDiracChromaFormat(enum PixelFormat ff_pix_fmt)
77
-{
78
-    int num_formats = sizeof(dirac_pixel_format_map) /
79
-                      sizeof(dirac_pixel_format_map[0]);
80
-    int idx;
81
-
82
-    for (idx = 0; idx < num_formats; ++idx)
83
-        if (dirac_pixel_format_map[idx].ff_pix_fmt == ff_pix_fmt)
84
-            return dirac_pixel_format_map[idx].dirac_pix_fmt;
85
-    return formatNK;
86
-}
87
-
88
-/**
89
-* Dirac video preset table. Ensure that this tables matches up correctly
90
-* with the ff_dirac_schro_video_format_info table in libdirac_libschro.c.
91
-*/
92
-static const VideoFormat ff_dirac_video_formats[]={
93
-    VIDEO_FORMAT_CUSTOM           ,
94
-    VIDEO_FORMAT_QSIF525          ,
95
-    VIDEO_FORMAT_QCIF             ,
96
-    VIDEO_FORMAT_SIF525           ,
97
-    VIDEO_FORMAT_CIF              ,
98
-    VIDEO_FORMAT_4SIF525          ,
99
-    VIDEO_FORMAT_4CIF             ,
100
-    VIDEO_FORMAT_SD_480I60        ,
101
-    VIDEO_FORMAT_SD_576I50        ,
102
-    VIDEO_FORMAT_HD_720P60        ,
103
-    VIDEO_FORMAT_HD_720P50        ,
104
-    VIDEO_FORMAT_HD_1080I60       ,
105
-    VIDEO_FORMAT_HD_1080I50       ,
106
-    VIDEO_FORMAT_HD_1080P60       ,
107
-    VIDEO_FORMAT_HD_1080P50       ,
108
-    VIDEO_FORMAT_DIGI_CINEMA_2K24 ,
109
-    VIDEO_FORMAT_DIGI_CINEMA_4K24 ,
110
-};
111
-
112
-/**
113
-* Returns the video format preset matching the input video dimensions and
114
-* time base.
115
-*/
116
-static VideoFormat GetDiracVideoFormatPreset(AVCodecContext *avccontext)
117
-{
118
-    unsigned int num_formats = sizeof(ff_dirac_video_formats) /
119
-                               sizeof(ff_dirac_video_formats[0]);
120
-
121
-    unsigned int idx = ff_dirac_schro_get_video_format_idx(avccontext);
122
-
123
-    return (idx < num_formats) ?
124
-                 ff_dirac_video_formats[idx] : VIDEO_FORMAT_CUSTOM;
125
-}
126
-
127
-static av_cold int libdirac_encode_init(AVCodecContext *avccontext)
128
-{
129
-
130
-    DiracEncoderParams* p_dirac_params = avccontext->priv_data;
131
-    int no_local = 1;
132
-    int verbose  = avccontext->debug;
133
-    VideoFormat preset;
134
-
135
-    /* get Dirac preset */
136
-    preset = GetDiracVideoFormatPreset(avccontext);
137
-
138
-    /* initialize the encoder context */
139
-    dirac_encoder_context_init(&p_dirac_params->enc_ctx, preset);
140
-
141
-    p_dirac_params->enc_ctx.src_params.chroma = GetDiracChromaFormat(avccontext->pix_fmt);
142
-
143
-    if (p_dirac_params->enc_ctx.src_params.chroma == formatNK) {
144
-        av_log(avccontext, AV_LOG_ERROR,
145
-               "Unsupported pixel format %d. This codec supports only "
146
-               "Planar YUV formats (yuv420p, yuv422p, yuv444p\n",
147
-               avccontext->pix_fmt);
148
-        return -1;
149
-    }
150
-
151
-    p_dirac_params->enc_ctx.src_params.frame_rate.numerator   = avccontext->time_base.den;
152
-    p_dirac_params->enc_ctx.src_params.frame_rate.denominator = avccontext->time_base.num;
153
-
154
-    p_dirac_params->enc_ctx.src_params.width  = avccontext->width;
155
-    p_dirac_params->enc_ctx.src_params.height = avccontext->height;
156
-
157
-    p_dirac_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
158
-                                                    avccontext->width,
159
-                                                    avccontext->height);
160
-
161
-    avccontext->coded_frame = &p_dirac_params->picture;
162
-
163
-    if (no_local) {
164
-        p_dirac_params->enc_ctx.decode_flag = 0;
165
-        p_dirac_params->enc_ctx.instr_flag  = 0;
166
-    } else {
167
-        p_dirac_params->enc_ctx.decode_flag = 1;
168
-        p_dirac_params->enc_ctx.instr_flag  = 1;
169
-    }
170
-
171
-    /* Intra-only sequence */
172
-    if (!avccontext->gop_size) {
173
-        p_dirac_params->enc_ctx.enc_params.num_L1 = 0;
174
-        if (avccontext->coder_type == FF_CODER_TYPE_VLC)
175
-            p_dirac_params->enc_ctx.enc_params.using_ac = 0;
176
-    } else
177
-        avccontext->has_b_frames = 1;
178
-
179
-    if (avccontext->flags & CODEC_FLAG_QSCALE) {
180
-        if (avccontext->global_quality) {
181
-            p_dirac_params->enc_ctx.enc_params.qf = avccontext->global_quality
182
-                                                    / (FF_QP2LAMBDA * 10.0);
183
-            /* if it is not default bitrate then send target rate. */
184
-            if (avccontext->bit_rate >= 1000 &&
185
-                avccontext->bit_rate != 200000)
186
-                p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate
187
-                                                           / 1000;
188
-        } else
189
-            p_dirac_params->enc_ctx.enc_params.lossless = 1;
190
-    } else if (avccontext->bit_rate >= 1000)
191
-        p_dirac_params->enc_ctx.enc_params.trate = avccontext->bit_rate / 1000;
192
-
193
-    if ((preset > VIDEO_FORMAT_QCIF || preset < VIDEO_FORMAT_QSIF525) &&
194
-         avccontext->bit_rate == 200000)
195
-        p_dirac_params->enc_ctx.enc_params.trate = 0;
196
-
197
-    if (avccontext->flags & CODEC_FLAG_INTERLACED_ME)
198
-        /* all material can be coded as interlaced or progressive
199
-         * irrespective of the type of source material */
200
-        p_dirac_params->enc_ctx.enc_params.picture_coding_mode = 1;
201
-
202
-    p_dirac_params->p_encoder = dirac_encoder_init(&p_dirac_params->enc_ctx,
203
-                                                   verbose);
204
-
205
-    if (!p_dirac_params->p_encoder) {
206
-        av_log(avccontext, AV_LOG_ERROR,
207
-               "Unrecoverable Error: dirac_encoder_init failed. ");
208
-        return EXIT_FAILURE;
209
-    }
210
-
211
-    /* allocate enough memory for the incoming data */
212
-    p_dirac_params->p_in_frame_buf = av_malloc(p_dirac_params->frame_size);
213
-
214
-    /* initialize the encoded frame queue */
215
-    ff_dirac_schro_queue_init(&p_dirac_params->enc_frame_queue);
216
-
217
-    return 0;
218
-}
219
-
220
-static void DiracFreeFrame(void *data)
221
-{
222
-    DiracSchroEncodedFrame *enc_frame = data;
223
-
224
-    av_freep(&enc_frame->p_encbuf);
225
-    av_free(enc_frame);
226
-}
227
-
228
-static int libdirac_encode_frame(AVCodecContext *avccontext,
229
-                                 unsigned char *frame,
230
-                                 int buf_size, void *data)
231
-{
232
-    int enc_size = 0;
233
-    dirac_encoder_state_t state;
234
-    DiracEncoderParams     *p_dirac_params      = avccontext->priv_data;
235
-    DiracSchroEncodedFrame *p_frame_output      = NULL;
236
-    DiracSchroEncodedFrame *p_next_output_frame = NULL;
237
-    int go = 1;
238
-    int last_frame_in_sequence = 0;
239
-
240
-    if (!data) {
241
-        /* push end of sequence if not already signalled */
242
-        if (!p_dirac_params->eos_signalled) {
243
-            dirac_encoder_end_sequence(p_dirac_params->p_encoder);
244
-            p_dirac_params->eos_signalled = 1;
245
-        }
246
-    } else {
247
-
248
-        /* Allocate frame data to Dirac input buffer.
249
-         * Input line size may differ from what the codec supports,
250
-         * especially when transcoding from one format to another.
251
-         * So use avpicture_layout to copy the frame. */
252
-        avpicture_layout((AVPicture *)data, avccontext->pix_fmt,
253
-                         avccontext->width, avccontext->height,
254
-                         p_dirac_params->p_in_frame_buf,
255
-                         p_dirac_params->frame_size);
256
-
257
-        /* load next frame */
258
-        if (dirac_encoder_load(p_dirac_params->p_encoder,
259
-                               p_dirac_params->p_in_frame_buf,
260
-                               p_dirac_params->frame_size) < 0) {
261
-            av_log(avccontext, AV_LOG_ERROR, "Unrecoverable Encoder Error."
262
-                   " dirac_encoder_load failed...\n");
263
-            return -1;
264
-        }
265
-    }
266
-
267
-    if (p_dirac_params->eos_pulled)
268
-        go = 0;
269
-
270
-    while (go) {
271
-        p_dirac_params->p_encoder->enc_buf.buffer = frame;
272
-        p_dirac_params->p_encoder->enc_buf.size   = buf_size;
273
-        /* process frame */
274
-        state = dirac_encoder_output(p_dirac_params->p_encoder);
275
-
276
-        switch (state) {
277
-        case ENC_STATE_AVAIL:
278
-        case ENC_STATE_EOS:
279
-            assert(p_dirac_params->p_encoder->enc_buf.size > 0);
280
-
281
-            /* All non-frame data is prepended to actual frame data to
282
-             * be able to set the pts correctly. So we don't write data
283
-             * to the frame output queue until we actually have a frame
284
-             */
285
-
286
-            p_dirac_params->enc_buf = av_realloc(p_dirac_params->enc_buf,
287
-                                                 p_dirac_params->enc_buf_size +
288
-                                                 p_dirac_params->p_encoder->enc_buf.size);
289
-            memcpy(p_dirac_params->enc_buf + p_dirac_params->enc_buf_size,
290
-                   p_dirac_params->p_encoder->enc_buf.buffer,
291
-                   p_dirac_params->p_encoder->enc_buf.size);
292
-
293
-            p_dirac_params->enc_buf_size += p_dirac_params->p_encoder->enc_buf.size;
294
-
295
-            if (state == ENC_STATE_EOS) {
296
-                p_dirac_params->eos_pulled = 1;
297
-                go = 0;
298
-            }
299
-
300
-            /* If non-frame data, don't output it until it we get an
301
-             * encoded frame back from the encoder. */
302
-            if (p_dirac_params->p_encoder->enc_pparams.pnum == -1)
303
-                break;
304
-
305
-            /* create output frame */
306
-            p_frame_output = av_mallocz(sizeof(DiracSchroEncodedFrame));
307
-            /* set output data */
308
-            p_frame_output->size      = p_dirac_params->enc_buf_size;
309
-            p_frame_output->p_encbuf  = p_dirac_params->enc_buf;
310
-            p_frame_output->frame_num = p_dirac_params->p_encoder->enc_pparams.pnum;
311
-
312
-            if (p_dirac_params->p_encoder->enc_pparams.ptype == INTRA_PICTURE &&
313
-                p_dirac_params->p_encoder->enc_pparams.rtype == REFERENCE_PICTURE)
314
-                p_frame_output->key_frame = 1;
315
-
316
-            ff_dirac_schro_queue_push_back(&p_dirac_params->enc_frame_queue,
317
-                                           p_frame_output);
318
-
319
-            p_dirac_params->enc_buf_size = 0;
320
-            p_dirac_params->enc_buf      = NULL;
321
-            break;
322
-
323
-        case ENC_STATE_BUFFER:
324
-            go = 0;
325
-            break;
326
-
327
-        case ENC_STATE_INVALID:
328
-            av_log(avccontext, AV_LOG_ERROR,
329
-                   "Unrecoverable Dirac Encoder Error. Quitting...\n");
330
-            return -1;
331
-
332
-        default:
333
-            av_log(avccontext, AV_LOG_ERROR, "Unknown Dirac Encoder state\n");
334
-            return -1;
335
-        }
336
-    }
337
-
338
-    /* copy 'next' frame in queue */
339
-
340
-    if (p_dirac_params->enc_frame_queue.size == 1 && p_dirac_params->eos_pulled)
341
-        last_frame_in_sequence = 1;
342
-
343
-    p_next_output_frame = ff_dirac_schro_queue_pop(&p_dirac_params->enc_frame_queue);
344
-
345
-    if (!p_next_output_frame)
346
-        return 0;
347
-
348
-    memcpy(frame, p_next_output_frame->p_encbuf, p_next_output_frame->size);
349
-    avccontext->coded_frame->key_frame = p_next_output_frame->key_frame;
350
-    /* Use the frame number of the encoded frame as the pts. It is OK to do
351
-     * so since Dirac is a constant framerate codec. It expects input to be
352
-     * of constant framerate. */
353
-    avccontext->coded_frame->pts = p_next_output_frame->frame_num;
354
-    enc_size = p_next_output_frame->size;
355
-
356
-    /* Append the end of sequence information to the last frame in the
357
-     * sequence. */
358
-    if (last_frame_in_sequence && p_dirac_params->enc_buf_size > 0) {
359
-        memcpy(frame + enc_size, p_dirac_params->enc_buf,
360
-               p_dirac_params->enc_buf_size);
361
-        enc_size += p_dirac_params->enc_buf_size;
362
-        av_freep(&p_dirac_params->enc_buf);
363
-        p_dirac_params->enc_buf_size = 0;
364
-    }
365
-
366
-    /* free frame */
367
-    DiracFreeFrame(p_next_output_frame);
368
-
369
-    return enc_size;
370
-}
371
-
372
-static av_cold int libdirac_encode_close(AVCodecContext *avccontext)
373
-{
374
-    DiracEncoderParams *p_dirac_params = avccontext->priv_data;
375
-
376
-    /* close the encoder */
377
-    dirac_encoder_close(p_dirac_params->p_encoder);
378
-
379
-    /* free data in the output frame queue */
380
-    ff_dirac_schro_queue_free(&p_dirac_params->enc_frame_queue,
381
-                              DiracFreeFrame);
382
-
383
-    /* free the encoder buffer */
384
-    if (p_dirac_params->enc_buf_size)
385
-        av_freep(&p_dirac_params->enc_buf);
386
-
387
-    /* free the input frame buffer */
388
-    av_freep(&p_dirac_params->p_in_frame_buf);
389
-
390
-    return 0;
391
-}
392
-
393
-
394
-AVCodec ff_libdirac_encoder = {
395
-    .name           = "libdirac",
396
-    .type           = AVMEDIA_TYPE_VIDEO,
397
-    .id             = CODEC_ID_DIRAC,
398
-    .priv_data_size = sizeof(DiracEncoderParams),
399
-    .init           = libdirac_encode_init,
400
-    .encode         = libdirac_encode_frame,
401
-    .close          = libdirac_encode_close,
402
-   .capabilities = CODEC_CAP_DELAY,
403
-   .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE},
404
-   .long_name = NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),
405
-};
... ...
@@ -35,6 +35,7 @@
35 35
 #include <schroedinger/schrovideoformat.h>
36 36
 
37 37
 #include "avcodec.h"
38
+#include "internal.h"
38 39
 #include "libdirac_libschro.h"
39 40
 #include "libschroedinger.h"
40 41
 #include "bytestream.h"
... ...
@@ -71,6 +72,9 @@ typedef struct SchroEncoderParams {
71 71
 
72 72
     /** end of sequence pulled */
73 73
     int eos_pulled;
74
+
75
+    /* counter for frames submitted to encoder, used as dts */
76
+    int64_t dts;
74 77
 } SchroEncoderParams;
75 78
 
76 79
 /**
... ...
@@ -175,6 +179,7 @@ static int libschroedinger_encode_init(AVCodecContext *avccontext)
175 175
         schro_encoder_setting_set_double(p_schro_params->encoder,
176 176
                                          "au_distance", avccontext->gop_size);
177 177
         avccontext->has_b_frames = 1;
178
+        p_schro_params->dts = -1;
178 179
     }
179 180
 
180 181
     /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
... ...
@@ -236,7 +241,7 @@ static int libschroedinger_encode_init(AVCodecContext *avccontext)
236 236
 }
237 237
 
238 238
 static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avccontext,
239
-                                                   void *in_data)
239
+                                                   const AVFrame *frame)
240 240
 {
241 241
     SchroEncoderParams *p_schro_params = avccontext->priv_data;
242 242
     SchroFrame *in_frame;
... ...
@@ -246,7 +251,7 @@ static SchroFrame *libschroedinger_frame_from_data(AVCodecContext *avccontext,
246 246
     in_frame = ff_create_schro_frame(avccontext, p_schro_params->frame_format);
247 247
 
248 248
     if (in_frame)
249
-        avpicture_layout((AVPicture *)in_data, avccontext->pix_fmt,
249
+        avpicture_layout((const AVPicture *)frame, avccontext->pix_fmt,
250 250
                           avccontext->width, avccontext->height,
251 251
                           in_frame->components[0].data,
252 252
                           p_schro_params->frame_size);
... ...
@@ -262,9 +267,8 @@ static void SchroedingerFreeFrame(void *data)
262 262
     av_free(enc_frame);
263 263
 }
264 264
 
265
-static int libschroedinger_encode_frame(AVCodecContext *avccontext,
266
-                                        unsigned char *frame,
267
-                                        int buf_size, void *data)
265
+static int libschroedinger_encode_frame(AVCodecContext *avccontext, AVPacket *pkt,
266
+                                        const AVFrame *frame, int *got_packet)
268 267
 {
269 268
     int enc_size = 0;
270 269
     SchroEncoderParams *p_schro_params = avccontext->priv_data;
... ...
@@ -275,8 +279,9 @@ static int libschroedinger_encode_frame(AVCodecContext *avccontext,
275 275
     int presentation_frame;
276 276
     int parse_code;
277 277
     int last_frame_in_sequence = 0;
278
+    int pkt_size, ret;
278 279
 
279
-    if (!data) {
280
+    if (!frame) {
280 281
         /* Push end of sequence if not already signalled. */
281 282
         if (!p_schro_params->eos_signalled) {
282 283
             schro_encoder_end_of_stream(encoder);
... ...
@@ -285,7 +290,7 @@ static int libschroedinger_encode_frame(AVCodecContext *avccontext,
285 285
     } else {
286 286
         /* Allocate frame data to schro input buffer. */
287 287
         SchroFrame *in_frame = libschroedinger_frame_from_data(avccontext,
288
-                                                               data);
288
+                                                               frame);
289 289
         /* Load next frame. */
290 290
         schro_encoder_push_frame(encoder, in_frame);
291 291
     }
... ...
@@ -373,28 +378,42 @@ static int libschroedinger_encode_frame(AVCodecContext *avccontext,
373 373
     if (!p_frame_output)
374 374
         return 0;
375 375
 
376
-    memcpy(frame, p_frame_output->p_encbuf, p_frame_output->size);
376
+    pkt_size = p_frame_output->size;
377
+    if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0)
378
+        pkt_size += p_schro_params->enc_buf_size;
379
+    if ((ret = ff_alloc_packet(pkt, pkt_size)) < 0) {
380
+        av_log(avccontext, AV_LOG_ERROR, "Error getting output packet of size %d.\n", pkt_size);
381
+        goto error;
382
+    }
383
+
384
+    memcpy(pkt->data, p_frame_output->p_encbuf, p_frame_output->size);
377 385
     avccontext->coded_frame->key_frame = p_frame_output->key_frame;
378 386
     /* Use the frame number of the encoded frame as the pts. It is OK to
379 387
      * do so since Dirac is a constant frame rate codec. It expects input
380 388
      * to be of constant frame rate. */
389
+    pkt->pts =
381 390
     avccontext->coded_frame->pts = p_frame_output->frame_num;
391
+    pkt->dts = p_schro_params->dts++;
382 392
     enc_size = p_frame_output->size;
383 393
 
384 394
     /* Append the end of sequence information to the last frame in the
385 395
      * sequence. */
386 396
     if (last_frame_in_sequence && p_schro_params->enc_buf_size > 0) {
387
-        memcpy(frame + enc_size, p_schro_params->enc_buf,
397
+        memcpy(pkt->data + enc_size, p_schro_params->enc_buf,
388 398
                p_schro_params->enc_buf_size);
389 399
         enc_size += p_schro_params->enc_buf_size;
390 400
         av_freep(&p_schro_params->enc_buf);
391 401
         p_schro_params->enc_buf_size = 0;
392 402
     }
393 403
 
404
+    if (p_frame_output->key_frame)
405
+        pkt->flags |= AV_PKT_FLAG_KEY;
406
+    *got_packet = 1;
407
+
408
+error:
394 409
     /* free frame */
395 410
     SchroedingerFreeFrame(p_frame_output);
396
-
397
-    return enc_size;
411
+    return ret;
398 412
 }
399 413
 
400 414
 
... ...
@@ -427,7 +446,7 @@ AVCodec ff_libschroedinger_encoder = {
427 427
     .id             = CODEC_ID_DIRAC,
428 428
     .priv_data_size = sizeof(SchroEncoderParams),
429 429
     .init           = libschroedinger_encode_init,
430
-    .encode         = libschroedinger_encode_frame,
430
+    .encode2        = libschroedinger_encode_frame,
431 431
     .close          = libschroedinger_encode_close,
432 432
    .capabilities = CODEC_CAP_DELAY,
433 433
    .pix_fmts     = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE},
... ...
@@ -35,6 +35,7 @@
35 35
 #include "libavutil/log.h"
36 36
 #include "libavutil/base64.h"
37 37
 #include "avcodec.h"
38
+#include "internal.h"
38 39
 
39 40
 /* libtheora includes */
40 41
 #include <theora/theoraenc.h>
... ...
@@ -260,14 +261,13 @@ static av_cold int encode_init(AVCodecContext* avc_context)
260 260
     return 0;
261 261
 }
262 262
 
263
-static int encode_frame(AVCodecContext* avc_context, uint8_t *outbuf,
264
-                        int buf_size, void *data)
263
+static int encode_frame(AVCodecContext* avc_context, AVPacket *pkt,
264
+                        const AVFrame *frame, int *got_packet)
265 265
 {
266 266
     th_ycbcr_buffer t_yuv_buffer;
267 267
     TheoraContext *h = avc_context->priv_data;
268
-    AVFrame *frame = data;
269 268
     ogg_packet o_packet;
270
-    int result, i;
269
+    int result, i, ret;
271 270
 
272 271
     // EOS, finish and get 1st pass stats if applicable
273 272
     if (!frame) {
... ...
@@ -328,18 +328,21 @@ static int encode_frame(AVCodecContext* avc_context, uint8_t *outbuf,
328 328
     }
329 329
 
330 330
     /* Copy ogg_packet content out to buffer */
331
-    if (buf_size < o_packet.bytes) {
332
-        av_log(avc_context, AV_LOG_ERROR, "encoded frame too large\n");
333
-        return -1;
331
+    if ((ret = ff_alloc_packet(pkt, o_packet.bytes)) < 0) {
332
+        av_log(avc_context, AV_LOG_ERROR, "Error getting output packet of size %ld.\n", o_packet.bytes);
333
+        return ret;
334 334
     }
335
-    memcpy(outbuf, o_packet.packet, o_packet.bytes);
335
+    memcpy(pkt->data, o_packet.packet, o_packet.bytes);
336 336
 
337 337
     // HACK: assumes no encoder delay, this is true until libtheora becomes
338 338
     // multithreaded (which will be disabled unless explictly requested)
339
-    avc_context->coded_frame->pts = frame->pts;
339
+    pkt->pts = pkt->dts = frame->pts;
340 340
     avc_context->coded_frame->key_frame = !(o_packet.granulepos & h->keyframe_mask);
341
+    if (avc_context->coded_frame->key_frame)
342
+        pkt->flags |= AV_PKT_FLAG_KEY;
343
+    *got_packet = 1;
341 344
 
342
-    return o_packet.bytes;
345
+    return 0;
343 346
 }
344 347
 
345 348
 static av_cold int encode_close(AVCodecContext* avc_context)
... ...
@@ -364,7 +367,7 @@ AVCodec ff_libtheora_encoder = {
364 364
     .priv_data_size = sizeof(TheoraContext),
365 365
     .init = encode_init,
366 366
     .close = encode_close,
367
-    .encode = encode_frame,
367
+    .encode2 = encode_frame,
368 368
     .capabilities = CODEC_CAP_DELAY, // needed to get the statsfile summary
369 369
     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_NONE},
370 370
     .long_name = NULL_IF_CONFIG_SMALL("libtheora Theora"),
... ...
@@ -380,33 +380,33 @@ static inline void cx_pktcpy(struct FrameListData *dst,
380 380
 }
381 381
 
382 382
 /**
383
- * Store coded frame information in format suitable for return from encode().
383
+ * Store coded frame information in format suitable for return from encode2().
384 384
  *
385
- * Write buffer information from @a cx_frame to @a buf & @a buf_size.
386
- * Timing/frame details to @a coded_frame.
387
- * @return Frame size written to @a buf on success
388
- * @return AVERROR(EINVAL) on error
385
+ * Write information from @a cx_frame to @a pkt
386
+ * @return packet data size on success
387
+ * @return a negative AVERROR on error
389 388
  */
390 389
 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
391
-                      uint8_t *buf, int buf_size, AVFrame *coded_frame)
390
+                      AVPacket *pkt, AVFrame *coded_frame)
392 391
 {
393
-    if ((int) cx_frame->sz <= buf_size) {
394
-        buf_size = cx_frame->sz;
395
-        memcpy(buf, cx_frame->buf, buf_size);
392
+    int ret = ff_alloc_packet(pkt, cx_frame->sz);
393
+    if (ret >= 0) {
394
+        memcpy(pkt->data, cx_frame->buf, pkt->size);
395
+        pkt->pts = pkt->dts    = cx_frame->pts;
396 396
         coded_frame->pts       = cx_frame->pts;
397 397
         coded_frame->key_frame = !!(cx_frame->flags & VPX_FRAME_IS_KEY);
398 398
 
399
-        if (coded_frame->key_frame)
399
+        if (coded_frame->key_frame) {
400 400
             coded_frame->pict_type = AV_PICTURE_TYPE_I;
401
-        else
401
+            pkt->flags            |= AV_PKT_FLAG_KEY;
402
+        } else
402 403
             coded_frame->pict_type = AV_PICTURE_TYPE_P;
403 404
     } else {
404 405
         av_log(avctx, AV_LOG_ERROR,
405
-               "Compressed frame larger than storage provided! (%zu/%d)\n",
406
-               cx_frame->sz, buf_size);
407
-        return AVERROR(EINVAL);
406
+               "Error getting output packet of size %zu.\n", cx_frame->sz);
407
+        return ret;
408 408
     }
409
-    return buf_size;
409
+    return pkt->size;
410 410
 }
411 411
 
412 412
 /**
... ...
@@ -417,7 +417,7 @@ static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
417 417
  * @return AVERROR(EINVAL) on output size error
418 418
  * @return AVERROR(ENOMEM) on coded frame queue data allocation error
419 419
  */
420
-static int queue_frames(AVCodecContext *avctx, uint8_t *buf, int buf_size,
420
+static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out,
421 421
                         AVFrame *coded_frame)
422 422
 {
423 423
     VP8Context *ctx = avctx->priv_data;
... ...
@@ -428,9 +428,9 @@ static int queue_frames(AVCodecContext *avctx, uint8_t *buf, int buf_size,
428 428
     if (ctx->coded_frame_list) {
429 429
         struct FrameListData *cx_frame = ctx->coded_frame_list;
430 430
         /* return the leading frame if we've already begun queueing */
431
-        size = storeframe(avctx, cx_frame, buf, buf_size, coded_frame);
431
+        size = storeframe(avctx, cx_frame, pkt_out, coded_frame);
432 432
         if (size < 0)
433
-            return AVERROR(EINVAL);
433
+            return size;
434 434
         ctx->coded_frame_list = cx_frame->next;
435 435
         free_coded_frame(cx_frame);
436 436
     }
... ...
@@ -447,9 +447,9 @@ static int queue_frames(AVCodecContext *avctx, uint8_t *buf, int buf_size,
447 447
                    provided a frame for output */
448 448
                 assert(!ctx->coded_frame_list);
449 449
                 cx_pktcpy(&cx_frame, pkt);
450
-                size = storeframe(avctx, &cx_frame, buf, buf_size, coded_frame);
450
+                size = storeframe(avctx, &cx_frame, pkt_out, coded_frame);
451 451
                 if (size < 0)
452
-                    return AVERROR(EINVAL);
452
+                    return size;
453 453
             } else {
454 454
                 struct FrameListData *cx_frame =
455 455
                     av_malloc(sizeof(struct FrameListData));
... ...
@@ -495,11 +495,10 @@ static int queue_frames(AVCodecContext *avctx, uint8_t *buf, int buf_size,
495 495
     return size;
496 496
 }
497 497
 
498
-static int vp8_encode(AVCodecContext *avctx, uint8_t *buf, int buf_size,
499
-                      void *data)
498
+static int vp8_encode(AVCodecContext *avctx, AVPacket *pkt,
499
+                      const AVFrame *frame, int *got_packet)
500 500
 {
501 501
     VP8Context *ctx = avctx->priv_data;
502
-    AVFrame *frame = data;
503 502
     struct vpx_image *rawimg = NULL;
504 503
     int64_t timestamp = 0;
505 504
     int res, coded_size;
... ...
@@ -521,7 +520,7 @@ static int vp8_encode(AVCodecContext *avctx, uint8_t *buf, int buf_size,
521 521
         log_encoder_error(avctx, "Error encoding frame");
522 522
         return AVERROR_INVALIDDATA;
523 523
     }
524
-    coded_size = queue_frames(avctx, buf, buf_size, avctx->coded_frame);
524
+    coded_size = queue_frames(avctx, pkt, avctx->coded_frame);
525 525
 
526 526
     if (!frame && avctx->flags & CODEC_FLAG_PASS1) {
527 527
         unsigned int b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
... ...
@@ -535,7 +534,9 @@ static int vp8_encode(AVCodecContext *avctx, uint8_t *buf, int buf_size,
535 535
         av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
536 536
                          ctx->twopass_stats.sz);
537 537
     }
538
-    return coded_size;
538
+
539
+    *got_packet = !!coded_size;
540
+    return 0;
539 541
 }
540 542
 
541 543
 #define OFFSET(x) offsetof(VP8Context, x)
... ...
@@ -598,7 +599,7 @@ AVCodec ff_libvpx_encoder = {
598 598
     .id             = CODEC_ID_VP8,
599 599
     .priv_data_size = sizeof(VP8Context),
600 600
     .init           = vp8_init,
601
-    .encode         = vp8_encode,
601
+    .encode2        = vp8_encode,
602 602
     .close          = vp8_free,
603 603
     .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
604 604
     .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
... ...
@@ -37,6 +37,7 @@
37 37
 #define XAVS_PART_B8X8 0x100 /* Analyze b16x8, b*/
38 38
 
39 39
 typedef struct XavsContext {
40
+    AVClass        *class;
40 41
     xavs_param_t    params;
41 42
     xavs_t         *enc;
42 43
     xavs_picture_t  pic;
... ...
@@ -53,6 +54,9 @@ typedef struct XavsContext {
53 53
     int fast_pskip;
54 54
     int mbtree;
55 55
     int mixed_refs;
56
+
57
+    int64_t *pts_buffer;
58
+    int out_frame_count;
56 59
 } XavsContext;
57 60
 
58 61
 static void XAVS_log(void *p, int level, const char *fmt, va_list args)
... ...
@@ -70,13 +74,24 @@ static void XAVS_log(void *p, int level, const char *fmt, va_list args)
70 70
     av_vlog(p, level_map[level], fmt, args);
71 71
 }
72 72
 
73
-static int encode_nals(AVCodecContext *ctx, uint8_t *buf,
74
-                       int size, xavs_nal_t *nals,
75
-                       int nnal, int skip_sei)
73
+static int encode_nals(AVCodecContext *ctx, AVPacket *pkt,
74
+                       xavs_nal_t *nals, int nnal)
76 75
 {
77 76
     XavsContext *x4 = ctx->priv_data;
78
-    uint8_t *p = buf;
79
-    int i, s;
77
+    uint8_t *p;
78
+    int i, s, ret, size = x4->sei_size + FF_MIN_BUFFER_SIZE;
79
+
80
+    if (!nnal)
81
+        return 0;
82
+
83
+    for (i = 0; i < nnal; i++)
84
+        size += nals[i].i_payload;
85
+
86
+    if ((ret = ff_alloc_packet(pkt, size)) < 0) {
87
+        av_log(ctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", size);
88
+        return ret;
89
+    }
90
+    p = pkt->data;
80 91
 
81 92
     /* Write the SEI as part of the first frame. */
82 93
     if (x4->sei_size > 0 && nnal > 0) {
... ...
@@ -86,30 +101,22 @@ static int encode_nals(AVCodecContext *ctx, uint8_t *buf,
86 86
     }
87 87
 
88 88
     for (i = 0; i < nnal; i++) {
89
-        /* Don't put the SEI in extradata. */
90
-        if (skip_sei && nals[i].i_type == NAL_SEI) {
91
-            x4->sei = av_malloc( 5 + nals[i].i_payload * 4 / 3 );
92
-            if (xavs_nal_encode(x4->sei, &x4->sei_size, 1, nals + i) < 0)
93
-                return -1;
94
-
95
-            continue;
96
-        }
97 89
         s = xavs_nal_encode(p, &size, 1, nals + i);
98 90
         if (s < 0)
99 91
             return -1;
100 92
         p += s;
101 93
     }
94
+    pkt->size = p - pkt->data;
102 95
 
103
-    return p - buf;
96
+    return 1;
104 97
 }
105 98
 
106
-static int XAVS_frame(AVCodecContext *ctx, uint8_t *buf,
107
-                      int bufsize, void *data)
99
+static int XAVS_frame(AVCodecContext *ctx, AVPacket *pkt,
100
+                      const AVFrame *frame, int *got_packet)
108 101
 {
109 102
     XavsContext *x4 = ctx->priv_data;
110
-    AVFrame *frame = data;
111 103
     xavs_nal_t *nal;
112
-    int nnal, i;
104
+    int nnal, i, ret;
113 105
     xavs_picture_t pic_out;
114 106
 
115 107
     x4->pic.img.i_csp   = XAVS_CSP_I420;
... ...
@@ -123,29 +130,44 @@ static int XAVS_frame(AVCodecContext *ctx, uint8_t *buf,
123 123
 
124 124
         x4->pic.i_pts  = frame->pts;
125 125
         x4->pic.i_type = XAVS_TYPE_AUTO;
126
+        x4->pts_buffer[ctx->frame_number % (ctx->max_b_frames+1)] = frame->pts;
126 127
     }
127 128
 
128 129
     if (xavs_encoder_encode(x4->enc, &nal, &nnal,
129 130
                             frame? &x4->pic: NULL, &pic_out) < 0)
130 131
     return -1;
131 132
 
132
-    bufsize = encode_nals(ctx, buf, bufsize, nal, nnal, 0);
133
+    ret = encode_nals(ctx, pkt, nal, nnal);
133 134
 
134
-    if (bufsize < 0)
135
+    if (ret < 0)
135 136
         return -1;
136 137
 
137
-    if (!bufsize && !frame && !(x4->end_of_stream)){
138
-        buf[bufsize]   = 0x0;
139
-        buf[bufsize+1] = 0x0;
140
-        buf[bufsize+2] = 0x01;
141
-        buf[bufsize+3] = 0xb1;
142
-        bufsize += 4;
143
-        x4->end_of_stream = END_OF_STREAM;
144
-        return bufsize;
138
+    if (!ret) {
139
+        if (!frame && !(x4->end_of_stream)) {
140
+            if ((ret = ff_alloc_packet(pkt, 4)) < 0)
141
+                return ret;
142
+
143
+            pkt->data[0] = 0x0;
144
+            pkt->data[1] = 0x0;
145
+            pkt->data[2] = 0x01;
146
+            pkt->data[3] = 0xb1;
147
+            pkt->dts = 2*x4->pts_buffer[(x4->out_frame_count-1)%(ctx->max_b_frames+1)] -
148
+                       x4->pts_buffer[(x4->out_frame_count-2)%(ctx->max_b_frames+1)];
149
+            x4->end_of_stream = END_OF_STREAM;
150
+            *got_packet = 1;
151
+        }
152
+        return 0;
145 153
     }
146
-    /* FIXME: libxavs now provides DTS */
147
-    /* but AVFrame doesn't have a field for it. */
154
+
148 155
     x4->out_pic.pts = pic_out.i_pts;
156
+    pkt->pts = pic_out.i_pts;
157
+    if (ctx->has_b_frames) {
158
+        if (!x4->out_frame_count)
159
+            pkt->dts = pkt->pts - (x4->pts_buffer[1] - x4->pts_buffer[0]);
160
+        else
161
+            pkt->dts = x4->pts_buffer[(x4->out_frame_count-1)%(ctx->max_b_frames+1)];
162
+    } else
163
+        pkt->dts = pkt->pts;
149 164
 
150 165
     switch (pic_out.i_type) {
151 166
     case XAVS_TYPE_IDR:
... ...
@@ -163,11 +185,16 @@ static int XAVS_frame(AVCodecContext *ctx, uint8_t *buf,
163 163
 
164 164
     /* There is no IDR frame in AVS JiZhun */
165 165
     /* Sequence header is used as a flag */
166
-    x4->out_pic.key_frame = pic_out.i_type == XAVS_TYPE_I;
166
+    if (pic_out.i_type == XAVS_TYPE_I) {
167
+        x4->out_pic.key_frame = 1;
168
+        pkt->flags |= AV_PKT_FLAG_KEY;
169
+    }
167 170
 
168 171
     x4->out_pic.quality   = (pic_out.i_qpplus1 - 1) * FF_QP2LAMBDA;
169 172
 
170
-    return bufsize;
173
+    x4->out_frame_count++;
174
+    *got_packet = ret;
175
+    return 0;
171 176
 }
172 177
 
173 178
 static av_cold int XAVS_close(AVCodecContext *avctx)
... ...
@@ -176,6 +203,7 @@ static av_cold int XAVS_close(AVCodecContext *avctx)
176 176
 
177 177
     av_freep(&avctx->extradata);
178 178
     av_free(x4->sei);
179
+    av_freep(&x4->pts_buffer);
179 180
 
180 181
     if (x4->enc)
181 182
         xavs_encoder_close(x4->enc);
... ...
@@ -324,17 +352,35 @@ static av_cold int XAVS_init(AVCodecContext *avctx)
324 324
     if (!x4->enc)
325 325
         return -1;
326 326
 
327
+    if (!(x4->pts_buffer = av_mallocz((avctx->max_b_frames+1) * sizeof(*x4->pts_buffer))))
328
+        return AVERROR(ENOMEM);
329
+
327 330
     avctx->coded_frame = &x4->out_pic;
328 331
     /* TAG: Do we have GLOBAL HEADER in AVS */
329 332
     /* We Have PPS and SPS in AVS */
330 333
     if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
331 334
         xavs_nal_t *nal;
332
-        int nnal, s;
335
+        int nnal, s, i, size;
336
+        uint8_t *p;
333 337
 
334 338
         s = xavs_encoder_headers(x4->enc, &nal, &nnal);
335 339
 
336
-        avctx->extradata      = av_malloc(s);
337
-        avctx->extradata_size = encode_nals(avctx, avctx->extradata, s, nal, nnal, 1);
340
+        avctx->extradata = p = av_malloc(s);
341
+        for (i = 0; i < nnal; i++) {
342
+            /* Don't put the SEI in extradata. */
343
+            if (nal[i].i_type == NAL_SEI) {
344
+                x4->sei = av_malloc( 5 + nal[i].i_payload * 4 / 3 );
345
+                if (xavs_nal_encode(x4->sei, &x4->sei_size, 1, nal + i) < 0)
346
+                    return -1;
347
+
348
+                continue;
349
+            }
350
+            size = xavs_nal_encode(p, &s, 1, nal + i);
351
+            if (size < 0)
352
+                return -1;
353
+            p += size;
354
+        }
355
+        avctx->extradata_size = p - avctx->extradata;
338 356
     }
339 357
     return 0;
340 358
 }
... ...
@@ -376,7 +422,7 @@ AVCodec ff_libxavs_encoder = {
376 376
     .id             = CODEC_ID_CAVS,
377 377
     .priv_data_size = sizeof(XavsContext),
378 378
     .init           = XAVS_init,
379
-    .encode         = XAVS_frame,
379
+    .encode2        = XAVS_frame,
380 380
     .close          = XAVS_close,
381 381
     .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
382 382
     .pix_fmts       = (const enum PixelFormat[]) { PIX_FMT_YUV420P, PIX_FMT_NONE },
... ...
@@ -33,6 +33,7 @@
33 33
 #include "libavutil/intreadwrite.h"
34 34
 #include "libavutil/mathematics.h"
35 35
 #include "libxvid_internal.h"
36
+#include "mpegvideo.h"
36 37
 
37 38
 /**
38 39
  * Buffer management macros.
... ...
@@ -71,7 +72,7 @@ struct xvid_ff_pass1 {
71 71
 };
72 72
 
73 73
 /* Prototypes - See function implementation for details */
74
-int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len);
74
+int xvid_strip_vol_header(AVCodecContext *avctx, AVPacket *pkt, unsigned int header_len, unsigned int frame_len);
75 75
 int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2);
76 76
 void xvid_correct_framerate(AVCodecContext *avctx);
77 77
 
... ...
@@ -370,17 +371,25 @@ static av_cold int xvid_encode_init(AVCodecContext *avctx)  {
370 370
  * @param data Pointer to AVFrame of unencoded frame
371 371
  * @return Returns 0 on success, -1 on failure
372 372
  */
373
-static int xvid_encode_frame(AVCodecContext *avctx,
374
-                         unsigned char *frame, int buf_size, void *data) {
375
-    int xerr, i;
373
+static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
374
+                             const AVFrame *picture, int *got_packet)
375
+{
376
+    int xerr, i, ret, user_packet = !!pkt->data;
376 377
     char *tmp;
377 378
     struct xvid_context *x = avctx->priv_data;
378
-    AVFrame *picture = data;
379 379
     AVFrame *p = &x->encoded_picture;
380
+    int mb_width   = (avctx->width  + 15) / 16;
381
+    int mb_height  = (avctx->height + 15) / 16;
380 382
 
381 383
     xvid_enc_frame_t xvid_enc_frame;
382 384
     xvid_enc_stats_t xvid_enc_stats;
383 385
 
386
+    if (!user_packet &&
387
+        (ret = av_new_packet(pkt, mb_width*mb_height*MAX_MB_BYTES + FF_MIN_BUFFER_SIZE)) < 0) {
388
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
389
+        return ret;
390
+    }
391
+
384 392
     /* Start setting up the frame */
385 393
     memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
386 394
     xvid_enc_frame.version = XVID_VERSION;
... ...
@@ -389,8 +398,8 @@ static int xvid_encode_frame(AVCodecContext *avctx,
389 389
     *p = *picture;
390 390
 
391 391
     /* Let Xvid know where to put the frame. */
392
-    xvid_enc_frame.bitstream = frame;
393
-    xvid_enc_frame.length = buf_size;
392
+    xvid_enc_frame.bitstream = pkt->data;
393
+    xvid_enc_frame.length    = pkt->size;
394 394
 
395 395
     /* Initialize input image fields */
396 396
     if( avctx->pix_fmt != PIX_FMT_YUV420P ) {
... ...
@@ -450,7 +459,9 @@ static int xvid_encode_frame(AVCodecContext *avctx,
450 450
         }
451 451
     }
452 452
 
453
-    if( 0 <= xerr ) {
453
+    if (xerr > 0) {
454
+        *got_packet = 1;
455
+
454 456
         p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;
455 457
         if( xvid_enc_stats.type == XVID_TYPE_PVOP )
456 458
             p->pict_type = AV_PICTURE_TYPE_P;
... ...
@@ -462,14 +473,21 @@ static int xvid_encode_frame(AVCodecContext *avctx,
462 462
             p->pict_type = AV_PICTURE_TYPE_I;
463 463
         if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) {
464 464
             p->key_frame = 1;
465
+            pkt->flags |= AV_PKT_FLAG_KEY;
465 466
             if( x->quicktime_format )
466
-                return xvid_strip_vol_header(avctx, frame,
467
+                return xvid_strip_vol_header(avctx, pkt,
467 468
                     xvid_enc_stats.hlength, xerr);
468 469
          } else
469 470
             p->key_frame = 0;
470 471
 
471
-        return xerr;
472
+        pkt->size = xerr;
473
+
474
+        return 0;
472 475
     } else {
476
+        if (!user_packet)
477
+            av_free_packet(pkt);
478
+        if (!xerr)
479
+            return 0;
473 480
         av_log(avctx, AV_LOG_ERROR, "Xvid: Encoding Error Occurred: %i\n", xerr);
474 481
         return -1;
475 482
     }
... ...
@@ -514,16 +532,16 @@ static av_cold int xvid_encode_close(AVCodecContext *avctx) {
514 514
  * @return Returns new length of frame data
515 515
  */
516 516
 int xvid_strip_vol_header(AVCodecContext *avctx,
517
-                  unsigned char *frame,
517
+                  AVPacket *pkt,
518 518
                   unsigned int header_len,
519 519
                   unsigned int frame_len) {
520 520
     int vo_len = 0, i;
521 521
 
522 522
     for( i = 0; i < header_len - 3; i++ ) {
523
-        if( frame[i] == 0x00 &&
524
-            frame[i+1] == 0x00 &&
525
-            frame[i+2] == 0x01 &&
526
-            frame[i+3] == 0xB6 ) {
523
+        if( pkt->data[i] == 0x00 &&
524
+            pkt->data[i+1] == 0x00 &&
525
+            pkt->data[i+2] == 0x01 &&
526
+            pkt->data[i+3] == 0xB6 ) {
527 527
             vo_len = i;
528 528
             break;
529 529
         }
... ...
@@ -533,15 +551,15 @@ int xvid_strip_vol_header(AVCodecContext *avctx,
533 533
         /* We need to store the header, so extract it */
534 534
         if( avctx->extradata == NULL ) {
535 535
             avctx->extradata = av_malloc(vo_len);
536
-            memcpy(avctx->extradata, frame, vo_len);
536
+            memcpy(avctx->extradata, pkt->data, vo_len);
537 537
             avctx->extradata_size = vo_len;
538 538
         }
539 539
         /* Less dangerous now, memmove properly copies the two
540 540
            chunks of overlapping data */
541
-        memmove(frame, &frame[vo_len], frame_len - vo_len);
542
-        return frame_len - vo_len;
543
-    } else
544
-        return frame_len;
541
+        memmove(pkt->data, &pkt->data[vo_len], frame_len - vo_len);
542
+        pkt->size = frame_len - vo_len;
543
+    }
544
+    return 0;
545 545
 }
546 546
 
547 547
 /**
... ...
@@ -777,7 +795,7 @@ AVCodec ff_libxvid_encoder = {
777 777
     .id             = CODEC_ID_MPEG4,
778 778
     .priv_data_size = sizeof(struct xvid_context),
779 779
     .init           = xvid_encode_init,
780
-    .encode         = xvid_encode_frame,
780
+    .encode2        = xvid_encode_frame,
781 781
     .close          = xvid_encode_close,
782 782
     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
783 783
     .long_name= NULL_IF_CONFIG_SMALL("libxvidcore MPEG-4 part 2"),
... ...
@@ -32,21 +32,37 @@
32 32
 
33 33
 #include "avcodec.h"
34 34
 #include "dsputil.h"
35
+#include "internal.h"
35 36
 #include "mpegvideo.h"
36 37
 #include "mjpeg.h"
37 38
 #include "mjpegenc.h"
38 39
 
39 40
 
40
-static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
41
+static int encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt,
42
+                                   const AVFrame *pict, int *got_packet)
43
+{
41 44
     MpegEncContext * const s = avctx->priv_data;
42 45
     MJpegContext * const m = s->mjpeg_ctx;
43
-    AVFrame *pict = data;
44 46
     const int width= s->width;
45 47
     const int height= s->height;
46 48
     AVFrame * const p= (AVFrame*)&s->current_picture;
47 49
     const int predictor= avctx->prediction_method+1;
50
+    const int mb_width  = (width  + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
51
+    const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
52
+    int ret, max_pkt_size = FF_MIN_BUFFER_SIZE;
53
+
54
+    if (avctx->pix_fmt == PIX_FMT_BGRA)
55
+        max_pkt_size += width * height * 3 * 4;
56
+    else {
57
+        max_pkt_size += mb_width * mb_height * 3 * 4
58
+                        * s->mjpeg_hsample[0] * s->mjpeg_vsample[0];
59
+    }
60
+    if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
61
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
62
+        return ret;
63
+    }
48 64
 
49
-    init_put_bits(&s->pb, buf, buf_size);
65
+    init_put_bits(&s->pb, pkt->data, pkt->size);
50 66
 
51 67
     *p = *pict;
52 68
     p->pict_type= AV_PICTURE_TYPE_I;
... ...
@@ -112,8 +128,6 @@ static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, in
112 112
         }
113 113
     }else{
114 114
         int mb_x, mb_y, i;
115
-        const int mb_width  = (width  + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
116
-        const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
117 115
 
118 116
         for(mb_y = 0; mb_y < mb_height; mb_y++) {
119 117
             if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < mb_width * 4 * 3 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0]){
... ...
@@ -189,7 +203,11 @@ static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, in
189 189
     s->picture_number++;
190 190
 
191 191
     flush_put_bits(&s->pb);
192
-    return put_bits_ptr(&s->pb) - s->pb.buf;
192
+    pkt->size   = put_bits_ptr(&s->pb) - s->pb.buf;
193
+    pkt->flags |= AV_PKT_FLAG_KEY;
194
+    *got_packet = 1;
195
+
196
+    return 0;
193 197
 //    return (put_bits_count(&f->pb)+7)/8;
194 198
 }
195 199
 
... ...
@@ -200,7 +218,7 @@ AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need t
200 200
     .id             = CODEC_ID_LJPEG,
201 201
     .priv_data_size = sizeof(MpegEncContext),
202 202
     .init           = ff_MPV_encode_init,
203
-    .encode         = encode_picture_lossless,
203
+    .encode2        = encode_picture_lossless,
204 204
     .close          = ff_MPV_encode_end,
205 205
     .long_name      = NULL_IF_CONFIG_SMALL("Lossless JPEG"),
206 206
 };
... ...
@@ -25,6 +25,7 @@
25 25
 #include "libavutil/imgutils.h"
26 26
 #include "avcodec.h"
27 27
 #include "bytestream.h"
28
+#include "internal.h"
28 29
 
29 30
 /** Maximum RLE code for bulk copy */
30 31
 #define MAX_RLE_BULK   127
... ...
@@ -102,7 +103,7 @@ static av_cold int qtrle_encode_init(AVCodecContext *avctx)
102 102
         return -1;
103 103
     }
104 104
 
105
-    s->max_buf_size = s->logical_width*s->avctx->height*s->pixel_size /* image base material */
105
+    s->max_buf_size = s->logical_width*s->avctx->height*s->pixel_size*2 /* image base material */
106 106
                       + 15                                            /* header + footer */
107 107
                       + s->avctx->height*2                            /* skip code+rle end */
108 108
                       + s->logical_width/MAX_RLE_BULK + 1             /* rle codes */;
... ...
@@ -113,7 +114,7 @@ static av_cold int qtrle_encode_init(AVCodecContext *avctx)
113 113
 /**
114 114
  * Compute the best RLE sequence for a line
115 115
  */
116
-static void qtrle_encode_line(QtrleEncContext *s, AVFrame *p, int line, uint8_t **buf)
116
+static void qtrle_encode_line(QtrleEncContext *s, const AVFrame *p, int line, uint8_t **buf)
117 117
 {
118 118
     int width=s->logical_width;
119 119
     int i;
... ...
@@ -259,7 +260,7 @@ static void qtrle_encode_line(QtrleEncContext *s, AVFrame *p, int line, uint8_t
259 259
 }
260 260
 
261 261
 /** Encode frame including header */
262
-static int encode_frame(QtrleEncContext *s, AVFrame *p, uint8_t *buf)
262
+static int encode_frame(QtrleEncContext *s, const AVFrame *p, uint8_t *buf)
263 263
 {
264 264
     int i;
265 265
     int start_line = 0;
... ...
@@ -300,19 +301,19 @@ static int encode_frame(QtrleEncContext *s, AVFrame *p, uint8_t *buf)
300 300
     return buf - orig_buf;
301 301
 }
302 302
 
303
-static int qtrle_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
303
+static int qtrle_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
304
+                              const AVFrame *pict, int *got_packet)
304 305
 {
305 306
     QtrleEncContext * const s = avctx->priv_data;
306
-    AVFrame *pict = data;
307 307
     AVFrame * const p = &s->frame;
308
-    int chunksize;
308
+    int ret;
309 309
 
310 310
     *p = *pict;
311 311
 
312
-    if (buf_size < s->max_buf_size) {
312
+    if ((ret = ff_alloc_packet(pkt, s->max_buf_size)) < 0) {
313 313
         /* Upper bound check for compressed data */
314
-        av_log(avctx, AV_LOG_ERROR, "buf_size %d <  %d\n", buf_size, s->max_buf_size);
315
-        return -1;
314
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", s->max_buf_size);
315
+        return ret;
316 316
     }
317 317
 
318 318
     if (avctx->gop_size == 0 || (s->avctx->frame_number % avctx->gop_size) == 0) {
... ...
@@ -325,11 +326,16 @@ static int qtrle_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size,
325 325
         p->key_frame = 0;
326 326
     }
327 327
 
328
-    chunksize = encode_frame(s, pict, buf);
328
+    pkt->size = encode_frame(s, pict, pkt->data);
329 329
 
330 330
     /* save the current frame */
331 331
     av_picture_copy(&s->previous_frame, (AVPicture *)p, avctx->pix_fmt, avctx->width, avctx->height);
332
-    return chunksize;
332
+
333
+    if (p->key_frame)
334
+        pkt->flags |= AV_PKT_FLAG_KEY;
335
+    *got_packet = 1;
336
+
337
+    return 0;
333 338
 }
334 339
 
335 340
 static av_cold int qtrle_encode_end(AVCodecContext *avctx)
... ...
@@ -349,7 +355,7 @@ AVCodec ff_qtrle_encoder = {
349 349
     .id             = CODEC_ID_QTRLE,
350 350
     .priv_data_size = sizeof(QtrleEncContext),
351 351
     .init           = qtrle_encode_init,
352
-    .encode         = qtrle_encode_frame,
352
+    .encode2        = qtrle_encode_frame,
353 353
     .close          = qtrle_encode_end,
354 354
     .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGB555BE, PIX_FMT_ARGB, PIX_FMT_GRAY8, PIX_FMT_NONE},
355 355
     .long_name = NULL_IF_CONFIG_SMALL("QuickTime Animation (RLE) video"),
... ...
@@ -1600,17 +1600,25 @@ static void calculate_visual_weight(SnowContext *s, Plane *p){
1600 1600
     }
1601 1601
 }
1602 1602
 
1603
-static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
1603
+static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
1604
+                        const AVFrame *pict, int *got_packet)
1605
+{
1604 1606
     SnowContext *s = avctx->priv_data;
1605 1607
     RangeCoder * const c= &s->c;
1606
-    AVFrame *pict = data;
1608
+    AVFrame *pic = &s->new_picture;
1607 1609
     const int width= s->avctx->width;
1608 1610
     const int height= s->avctx->height;
1609
-    int level, orientation, plane_index, i, y;
1611
+    int level, orientation, plane_index, i, y, ret;
1610 1612
     uint8_t rc_header_bak[sizeof(s->header_state)];
1611 1613
     uint8_t rc_block_bak[sizeof(s->block_state)];
1612 1614
 
1613
-    ff_init_range_encoder(c, buf, buf_size);
1615
+    if (!pkt->data &&
1616
+        (ret = av_new_packet(pkt, s->b_width*s->b_height*MB_SIZE*MB_SIZE*3 + FF_MIN_BUFFER_SIZE)) < 0) {
1617
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
1618
+        return ret;
1619
+    }
1620
+
1621
+    ff_init_range_encoder(c, pkt->data, pkt->size);
1614 1622
     ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
1615 1623
 
1616 1624
     for(i=0; i<3; i++){
... ...
@@ -1624,27 +1632,25 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1624 1624
 
1625 1625
     s->m.picture_number= avctx->frame_number;
1626 1626
     if(avctx->flags&CODEC_FLAG_PASS2){
1627
-        s->m.pict_type =
1628
-        pict->pict_type= s->m.rc_context.entry[avctx->frame_number].new_pict_type;
1629
-        s->keyframe= pict->pict_type==AV_PICTURE_TYPE_I;
1627
+        s->m.pict_type = pic->pict_type = s->m.rc_context.entry[avctx->frame_number].new_pict_type;
1628
+        s->keyframe = pic->pict_type == AV_PICTURE_TYPE_I;
1630 1629
         if(!(avctx->flags&CODEC_FLAG_QSCALE)) {
1631
-            pict->quality= ff_rate_estimate_qscale(&s->m, 0);
1632
-            if (pict->quality < 0)
1630
+            pic->quality = ff_rate_estimate_qscale(&s->m, 0);
1631
+            if (pic->quality < 0)
1633 1632
                 return -1;
1634 1633
         }
1635 1634
     }else{
1636 1635
         s->keyframe= avctx->gop_size==0 || avctx->frame_number % avctx->gop_size == 0;
1637
-        s->m.pict_type=
1638
-        pict->pict_type= s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1636
+        s->m.pict_type = pic->pict_type = s->keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
1639 1637
     }
1640 1638
 
1641 1639
     if(s->pass1_rc && avctx->frame_number == 0)
1642
-        pict->quality= 2*FF_QP2LAMBDA;
1643
-    if(pict->quality){
1644
-        s->qlog= qscale2qlog(pict->quality);
1645
-        s->lambda = pict->quality * 3/2;
1640
+        pic->quality = 2*FF_QP2LAMBDA;
1641
+    if (pic->quality) {
1642
+        s->qlog   = qscale2qlog(pic->quality);
1643
+        s->lambda = pic->quality * 3/2;
1646 1644
     }
1647
-    if(s->qlog < 0 || (!pict->quality && (avctx->flags & CODEC_FLAG_QSCALE))){
1645
+    if (s->qlog < 0 || (!pic->quality && (avctx->flags & CODEC_FLAG_QSCALE))) {
1648 1646
         s->qlog= LOSSLESS_QLOG;
1649 1647
         s->lambda = 0;
1650 1648
     }//else keep previous frame's qlog until after motion estimation
... ...
@@ -1654,7 +1660,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1654 1654
     s->m.current_picture_ptr= &s->m.current_picture;
1655 1655
     s->m.last_picture.f.pts = s->m.current_picture.f.pts;
1656 1656
     s->m.current_picture.f.pts = pict->pts;
1657
-    if(pict->pict_type == AV_PICTURE_TYPE_P){
1657
+    if(pic->pict_type == AV_PICTURE_TYPE_P){
1658 1658
         int block_width = (width +15)>>4;
1659 1659
         int block_height= (height+15)>>4;
1660 1660
         int stride= s->current_picture.linesize[0];
... ...
@@ -1679,7 +1685,7 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1679 1679
         s->m.mb_stride=   s->m.mb_width+1;
1680 1680
         s->m.b8_stride= 2*s->m.mb_width+1;
1681 1681
         s->m.f_code=1;
1682
-        s->m.pict_type= pict->pict_type;
1682
+        s->m.pict_type = pic->pict_type;
1683 1683
         s->m.me_method= s->avctx->me_method;
1684 1684
         s->m.me.scene_change_score=0;
1685 1685
         s->m.flags= s->avctx->flags;
... ...
@@ -1703,13 +1709,13 @@ static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size,
1703 1703
 
1704 1704
 redo_frame:
1705 1705
 
1706
-    if(pict->pict_type == AV_PICTURE_TYPE_I)
1706
+    if (pic->pict_type == AV_PICTURE_TYPE_I)
1707 1707
         s->spatial_decomposition_count= 5;
1708 1708
     else
1709 1709
         s->spatial_decomposition_count= 5;
1710 1710
 
1711
-    s->m.pict_type = pict->pict_type;
1712
-    s->qbias= pict->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
1711
+    s->m.pict_type = pic->pict_type;
1712
+    s->qbias = pic->pict_type == AV_PICTURE_TYPE_P ? 2 : 0;
1713 1713
 
1714 1714
     ff_snow_common_init_after_header(avctx);
1715 1715
 
... ...
@@ -1742,12 +1748,12 @@ redo_frame:
1742 1742
             predict_plane(s, s->spatial_idwt_buffer, plane_index, 0);
1743 1743
 
1744 1744
             if(   plane_index==0
1745
-               && pict->pict_type == AV_PICTURE_TYPE_P
1745
+               && pic->pict_type == AV_PICTURE_TYPE_P
1746 1746
                && !(avctx->flags&CODEC_FLAG_PASS2)
1747 1747
                && s->m.me.scene_change_score > s->avctx->scenechange_threshold){
1748
-                ff_init_range_encoder(c, buf, buf_size);
1748
+                ff_init_range_encoder(c, pkt->data, pkt->size);
1749 1749
                 ff_build_rac_states(c, 0.05*(1LL<<32), 256-8);
1750
-                pict->pict_type= AV_PICTURE_TYPE_I;
1750
+                pic->pict_type= AV_PICTURE_TYPE_I;
1751 1751
                 s->keyframe=1;
1752 1752
                 s->current_picture.key_frame=1;
1753 1753
                 goto redo_frame;
... ...
@@ -1773,12 +1779,12 @@ redo_frame:
1773 1773
                 ff_spatial_dwt(s->spatial_dwt_buffer, w, h, w, s->spatial_decomposition_type, s->spatial_decomposition_count);
1774 1774
 
1775 1775
             if(s->pass1_rc && plane_index==0){
1776
-                int delta_qlog = ratecontrol_1pass(s, pict);
1776
+                int delta_qlog = ratecontrol_1pass(s, pic);
1777 1777
                 if (delta_qlog <= INT_MIN)
1778 1778
                     return -1;
1779 1779
                 if(delta_qlog){
1780 1780
                     //reordering qlog in the bitstream would eliminate this reset
1781
-                    ff_init_range_encoder(c, buf, buf_size);
1781
+                    ff_init_range_encoder(c, pkt->data, pkt->size);
1782 1782
                     memcpy(s->header_state, rc_header_bak, sizeof(s->header_state));
1783 1783
                     memcpy(s->block_state, rc_block_bak, sizeof(s->block_state));
1784 1784
                     encode_header(s);
... ...
@@ -1793,7 +1799,7 @@ redo_frame:
1793 1793
                     if(!QUANTIZE2)
1794 1794
                         quantize(s, b, b->ibuf, b->buf, b->stride, s->qbias);
1795 1795
                     if(orientation==0)
1796
-                        decorrelate(s, b, b->ibuf, b->stride, pict->pict_type == AV_PICTURE_TYPE_P, 0);
1796
+                        decorrelate(s, b, b->ibuf, b->stride, pic->pict_type == AV_PICTURE_TYPE_P, 0);
1797 1797
                     encode_subband(s, b, b->ibuf, b->parent ? b->parent->ibuf : NULL, b->stride, orientation);
1798 1798
                     assert(b->parent==NULL || b->parent->stride == b->stride*2);
1799 1799
                     if(orientation==0)
... ...
@@ -1820,7 +1826,7 @@ redo_frame:
1820 1820
             predict_plane(s, s->spatial_idwt_buffer, plane_index, 1);
1821 1821
         }else{
1822 1822
             //ME/MC only
1823
-            if(pict->pict_type == AV_PICTURE_TYPE_I){
1823
+            if(pic->pict_type == AV_PICTURE_TYPE_I){
1824 1824
                 for(y=0; y<h; y++){
1825 1825
                     for(x=0; x<w; x++){
1826 1826
                         s->current_picture.data[plane_index][y*s->current_picture.linesize[plane_index] + x]=
... ...
@@ -1859,7 +1865,7 @@ redo_frame:
1859 1859
     s->m.p_tex_bits = s->m.frame_bits - s->m.misc_bits - s->m.mv_bits;
1860 1860
     s->m.current_picture.f.display_picture_number =
1861 1861
     s->m.current_picture.f.coded_picture_number   = avctx->frame_number;
1862
-    s->m.current_picture.f.quality                = pict->quality;
1862
+    s->m.current_picture.f.quality                = pic->quality;
1863 1863
     s->m.total_bits += 8*(s->c.bytestream - s->c.bytestream_start);
1864 1864
     if(s->pass1_rc)
1865 1865
         if (ff_rate_estimate_qscale(&s->m, 0) < 0)
... ...
@@ -1874,7 +1880,12 @@ redo_frame:
1874 1874
 
1875 1875
     emms_c();
1876 1876
 
1877
-    return ff_rac_terminate(c);
1877
+    pkt->size = ff_rac_terminate(c);
1878
+    if (avctx->coded_frame->key_frame)
1879
+        pkt->flags |= AV_PKT_FLAG_KEY;
1880
+    *got_packet = 1;
1881
+
1882
+    return 0;
1878 1883
 }
1879 1884
 
1880 1885
 static av_cold int encode_end(AVCodecContext *avctx)
... ...
@@ -1909,7 +1920,7 @@ AVCodec ff_snow_encoder = {
1909 1909
     .id             = CODEC_ID_SNOW,
1910 1910
     .priv_data_size = sizeof(SnowContext),
1911 1911
     .init           = encode_init,
1912
-    .encode         = encode_frame,
1912
+    .encode2        = encode_frame,
1913 1913
     .close          = encode_end,
1914 1914
     .long_name = NULL_IF_CONFIG_SMALL("Snow"),
1915 1915
     .priv_class     = &snowenc_class,
... ...
@@ -202,15 +202,13 @@ static void pack_yuv(TiffEncoderContext * s, uint8_t * dst, int lnum)
202 202
     }
203 203
 }
204 204
 
205
-static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
206
-                        int buf_size, void *data)
205
+static int encode_frame(AVCodecContext * avctx, AVPacket *pkt,
206
+                        const AVFrame *pict, int *got_packet)
207 207
 {
208 208
     TiffEncoderContext *s = avctx->priv_data;
209
-    AVFrame *pict = data;
210 209
     AVFrame *const p = (AVFrame *) & s->picture;
211 210
     int i;
212
-    int n;
213
-    uint8_t *ptr = buf;
211
+    uint8_t *ptr;
214 212
     uint8_t *offset;
215 213
     uint32_t strips;
216 214
     uint32_t *strip_sizes = NULL;
... ...
@@ -224,9 +222,6 @@ static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
224 224
     int shift_h, shift_v;
225 225
 
226 226
     s->avctx = avctx;
227
-    s->buf_start = buf;
228
-    s->buf = &ptr;
229
-    s->buf_size = buf_size;
230 227
 
231 228
     *p = *pict;
232 229
     p->pict_type = AV_PICTURE_TYPE_I;
... ...
@@ -308,6 +303,17 @@ static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
308 308
 
309 309
     strips = (s->height - 1) / s->rps + 1;
310 310
 
311
+    if (!pkt->data &&
312
+        (ret = av_new_packet(pkt, avctx->width * avctx->height * s->bpp * 2 +
313
+                                  avctx->height * 4 + FF_MIN_BUFFER_SIZE)) < 0) {
314
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
315
+        return ret;
316
+    }
317
+    ptr          = pkt->data;
318
+    s->buf_start = pkt->data;
319
+    s->buf       = &ptr;
320
+    s->buf_size  = pkt->size;
321
+
311 322
     if (check_size(s, 8))
312 323
         goto fail;
313 324
 
... ...
@@ -339,7 +345,7 @@ static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
339 339
 
340 340
         zlen = bytes_per_row * s->rps;
341 341
         zbuf = av_malloc(zlen);
342
-        strip_offsets[0] = ptr - buf;
342
+        strip_offsets[0] = ptr - pkt->data;
343 343
         zn = 0;
344 344
         for (j = 0; j < s->rps; j++) {
345 345
             if (is_yuv){
... ...
@@ -352,14 +358,14 @@ static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
352 352
                        p->data[0] + j * p->linesize[0], bytes_per_row);
353 353
             zn += bytes_per_row;
354 354
         }
355
-        n = encode_strip(s, zbuf, ptr, zn, s->compr);
355
+        ret = encode_strip(s, zbuf, ptr, zn, s->compr);
356 356
         av_free(zbuf);
357
-        if (n<0) {
357
+        if (ret < 0) {
358 358
             av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
359 359
             goto fail;
360 360
         }
361
-        ptr += n;
362
-        strip_sizes[0] = ptr - buf - strip_offsets[0];
361
+        ptr += ret;
362
+        strip_sizes[0] = ptr - pkt->data - strip_offsets[0];
363 363
     } else
364 364
 #endif
365 365
     {
... ...
@@ -371,24 +377,23 @@ static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
371 371
                     ff_lzw_encode_init(s->lzws, ptr, s->buf_size - (*s->buf - s->buf_start),
372 372
                                        12, FF_LZW_TIFF, put_bits);
373 373
                 }
374
-                strip_offsets[i / s->rps] = ptr - buf;
374
+                strip_offsets[i / s->rps] = ptr - pkt->data;
375 375
             }
376 376
             if (is_yuv){
377 377
                  pack_yuv(s, yuv_line, i);
378
-                 n = encode_strip(s, yuv_line, ptr, bytes_per_row, s->compr);
378
+                 ret = encode_strip(s, yuv_line, ptr, bytes_per_row, s->compr);
379 379
                  i += s->subsampling[1] - 1;
380 380
             }
381 381
             else
382
-                n = encode_strip(s, p->data[0] + i * p->linesize[0],
382
+                ret = encode_strip(s, p->data[0] + i * p->linesize[0],
383 383
                         ptr, bytes_per_row, s->compr);
384
-            if (n < 0) {
384
+            if (ret < 0) {
385 385
                 av_log(s->avctx, AV_LOG_ERROR, "Encode strip failed\n");
386 386
                 goto fail;
387 387
             }
388
-            strip_sizes[i / s->rps] += n;
389
-            ptr += n;
388
+            strip_sizes[i / s->rps] += ret;
389
+            ptr += ret;
390 390
             if(s->compr == TIFF_LZW && (i==s->height-1 || i%s->rps == s->rps-1)){
391
-                int ret;
392 391
                 ret = ff_lzw_encode_flush(s->lzws, flush_put_bits);
393 392
                 strip_sizes[(i / s->rps )] += ret ;
394 393
                 ptr += ret;
... ...
@@ -440,15 +445,19 @@ static int encode_frame(AVCodecContext * avctx, unsigned char *buf,
440 440
         add_entry(s, TIFF_YCBCR_SUBSAMPLING, TIFF_SHORT,    2, s->subsampling);
441 441
         add_entry(s, TIFF_REFERENCE_BW,      TIFF_RATIONAL, 6, refbw);
442 442
     }
443
-    bytestream_put_le32(&offset, ptr - buf);    // write offset to dir
443
+    bytestream_put_le32(&offset, ptr - pkt->data);    // write offset to dir
444 444
 
445
-    if (check_size(s, 6 + s->num_entries * 12))
445
+    if (check_size(s, 6 + s->num_entries * 12)) {
446
+        ret = AVERROR(EINVAL);
446 447
         goto fail;
448
+    }
447 449
     bytestream_put_le16(&ptr, s->num_entries);  // write tag count
448 450
     bytestream_put_buffer(&ptr, s->entries, s->num_entries * 12);
449 451
     bytestream_put_le32(&ptr, 0);
450 452
 
451
-    ret = ptr - buf;
453
+    pkt->size   = ptr - pkt->data;
454
+    pkt->flags |= AV_PKT_FLAG_KEY;
455
+    *got_packet = 1;
452 456
 
453 457
 fail:
454 458
     av_free(strip_sizes);
... ...
@@ -483,7 +492,7 @@ AVCodec ff_tiff_encoder = {
483 483
     .type           = AVMEDIA_TYPE_VIDEO,
484 484
     .id             = CODEC_ID_TIFF,
485 485
     .priv_data_size = sizeof(TiffEncoderContext),
486
-    .encode         = encode_frame,
486
+    .encode2        = encode_frame,
487 487
     .pix_fmts =
488 488
         (const enum PixelFormat[]) {PIX_FMT_RGB24, PIX_FMT_PAL8, PIX_FMT_GRAY8,
489 489
                               PIX_FMT_MONOBLACK, PIX_FMT_MONOWHITE,
... ...
@@ -135,7 +135,7 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
135 135
                huff.val_bits, huff.max_bits);
136 136
         return -1;
137 137
     }
138
-    if((huff.nodes < 0) || (huff.nodes > 0x10000)) {
138
+    if((huff.nodes <= 0) || (huff.nodes > 0x10000)) {
139 139
         av_log(ctx->avctx, AV_LOG_ERROR, "Incorrect number of Huffman tree nodes: %i\n", huff.nodes);
140 140
         return -1;
141 141
     }
... ...
@@ -1188,9 +1188,6 @@ int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf
1188 1188
 }
1189 1189
 #endif
1190 1190
 
1191
-#define MAX_CODED_FRAME_SIZE(width, height)\
1192
-    (9*(width)*(height) + FF_MIN_BUFFER_SIZE)
1193
-
1194 1191
 int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1195 1192
                                               AVPacket *avpkt,
1196 1193
                                               const AVFrame *frame,
... ...
@@ -1209,44 +1206,15 @@ int attribute_align_arg avcodec_encode_video2(AVCodecContext *avctx,
1209 1209
     if (av_image_check_size(avctx->width, avctx->height, 0, avctx))
1210 1210
         return AVERROR(EINVAL);
1211 1211
 
1212
-    if (avctx->codec->encode2) {
1213
-        *got_packet_ptr = 0;
1214
-        ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1215
-        if (!ret) {
1216
-            if (!*got_packet_ptr)
1217
-                avpkt->size = 0;
1218
-            else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1219
-                avpkt->pts = avpkt->dts = frame->pts;
1220
-        }
1221
-    } else {
1222
-        /* for compatibility with encoders not supporting encode2(), we need to
1223
-           allocate a packet buffer if the user has not provided one or check
1224
-           the size otherwise */
1225
-        int buf_size = avpkt->size;
1226
-
1227
-        if (!user_packet)
1228
-            buf_size = MAX_CODED_FRAME_SIZE(avctx->width, avctx->height);
1229
-
1230
-        if ((ret = ff_alloc_packet(avpkt, buf_size)))
1231
-            return ret;
1232
-
1233
-        /* encode the frame */
1234
-        ret = avctx->codec->encode(avctx, avpkt->data, avpkt->size, frame);
1235
-        if (ret >= 0) {
1236
-            if (!ret) {
1237
-                /* no output. if the packet data was allocated by libavcodec,
1238
-                   free it */
1239
-                if (!user_packet)
1240
-                    av_freep(&avpkt->data);
1241
-            } else if (avctx->coded_frame) {
1242
-                avpkt->pts    = avctx->coded_frame->pts;
1243
-                avpkt->flags |= AV_PKT_FLAG_KEY*!!avctx->coded_frame->key_frame;
1244
-            }
1212
+    av_assert0(avctx->codec->encode2);
1245 1213
 
1246
-            avpkt->size     = ret;
1247
-            *got_packet_ptr = (ret > 0);
1248
-            ret             = 0;
1249
-        }
1214
+    *got_packet_ptr = 0;
1215
+    ret = avctx->codec->encode2(avctx, avpkt, frame, got_packet_ptr);
1216
+    if (!ret) {
1217
+        if (!*got_packet_ptr)
1218
+            avpkt->size = 0;
1219
+        else if (!(avctx->codec->capabilities & CODEC_CAP_DELAY))
1220
+            avpkt->pts = avpkt->dts = frame->pts;
1250 1221
     }
1251 1222
 
1252 1223
     if (!ret)
... ...
@@ -813,8 +813,8 @@ static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
813 813
     s->hybrid         =   s->frame_flags & WV_HYBRID_MODE;
814 814
     s->hybrid_bitrate =   s->frame_flags & WV_HYBRID_BITRATE;
815 815
     s->post_shift     = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
816
-    s->hybrid_maxclip = (( 1LL << (orig_bpp - 1)) - 1) >> s->post_shift;
817
-    s->hybrid_minclip = ((-1LL << (orig_bpp - 1)))     >> s->post_shift;
816
+    s->hybrid_maxclip = (( 1LL << (orig_bpp - 1)) - 1);
817
+    s->hybrid_minclip = ((-1LL << (orig_bpp - 1)));
818 818
     s->CRC            = AV_RL32(buf); buf += 4;
819 819
     if (wc->mkv_mode)
820 820
         buf += 4; //skip block size;
... ...
@@ -258,6 +258,7 @@ OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
258 258
                                             rtpenc_latm.o    \
259 259
                                             rtpenc_amr.o     \
260 260
                                             rtpenc_h263.o    \
261
+                                            rtpenc_h263_rfc2190.o \
261 262
                                             rtpenc_mpv.o     \
262 263
                                             rtpenc.o      \
263 264
                                             rtpenc_h264.o \
... ...
@@ -25,6 +25,7 @@
25 25
 //#define DEBUG
26 26
 //#define MOV_EXPORT_ALL_METADATA
27 27
 
28
+#include "libavutil/audioconvert.h"
28 29
 #include "libavutil/intreadwrite.h"
29 30
 #include "libavutil/intfloat.h"
30 31
 #include "libavutil/mathematics.h"
... ...
@@ -32,6 +33,7 @@
32 32
 #include "libavutil/dict.h"
33 33
 #include "libavutil/opt.h"
34 34
 #include "libavutil/timecode.h"
35
+#include "libavcodec/ac3tab.h"
35 36
 #include "avformat.h"
36 37
 #include "internal.h"
37 38
 #include "avio_internal.h"
... ...
@@ -570,6 +572,9 @@ static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
570 570
     acmod = (ac3info >> 11) & 0x7;
571 571
     lfeon = (ac3info >> 10) & 0x1;
572 572
     st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
573
+    st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
574
+    if (lfeon)
575
+        st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
573 576
     st->codec->audio_service_type = bsmod;
574 577
     if (st->codec->channels > 1 && bsmod == 0x7)
575 578
         st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
... ...
@@ -52,7 +52,7 @@ static const AVOption options[] = {
52 52
     { "separate_moof", "Write separate moof/mdat atoms for each track", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_SEPARATE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
53 53
     { "frag_custom", "Flush fragments on caller requests", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_FRAG_CUSTOM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
54 54
     { "isml", "Create a live smooth streaming feed (for pushing to a publishing point)", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_ISML}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
55
-    FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
55
+    FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags)
56 56
     { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
57 57
     { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
58 58
     { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
... ...
@@ -106,7 +106,9 @@ int ff_rtp_get_payload_type(AVFormatContext *fmt, AVCodecContext *codec)
106 106
     /* static payload type */
107 107
     for (i = 0; AVRtpPayloadTypes[i].pt >= 0; ++i)
108 108
         if (AVRtpPayloadTypes[i].codec_id == codec->codec_id) {
109
-            if (codec->codec_id == CODEC_ID_H263)
109
+            if (codec->codec_id == CODEC_ID_H263 && (!fmt ||
110
+                !fmt->oformat->priv_class ||
111
+                !av_opt_flag_is_set(fmt->priv_data, "rtpflags", "rfc2190")))
110 112
                 continue;
111 113
             if (codec->codec_id == CODEC_ID_PCM_S16BE)
112 114
                 if (codec->channels != AVRtpPayloadTypes[i].audio_channels)
... ...
@@ -31,8 +31,9 @@
31 31
 //#define DEBUG
32 32
 
33 33
 static const AVOption options[] = {
34
-    FF_RTP_FLAG_OPTS(RTPMuxContext, flags),
34
+    FF_RTP_FLAG_OPTS(RTPMuxContext, flags)
35 35
     { "payload_type", "Specify RTP payload type", offsetof(RTPMuxContext, payload_type), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, 127, AV_OPT_FLAG_ENCODING_PARAM },
36
+    { "max_packet_size", "Max packet size", offsetof(RTPMuxContext, max_packet_size), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
36 37
     { NULL },
37 38
 };
38 39
 
... ...
@@ -82,11 +83,13 @@ static int is_supported(enum CodecID id)
82 82
 static int rtp_write_header(AVFormatContext *s1)
83 83
 {
84 84
     RTPMuxContext *s = s1->priv_data;
85
-    int max_packet_size, n;
85
+    int n;
86 86
     AVStream *st;
87 87
 
88
-    if (s1->nb_streams != 1)
89
-        return -1;
88
+    if (s1->nb_streams != 1) {
89
+        av_log(s1, AV_LOG_ERROR, "Only one stream supported in the RTP muxer\n");
90
+        return AVERROR(EINVAL);
91
+    }
90 92
     st = s1->streams[0];
91 93
     if (!is_supported(st->codec->codec_id)) {
92 94
         av_log(s1, AV_LOG_ERROR, "Unsupported codec %s\n", avcodec_get_name(st->codec->codec_id));
... ...
@@ -107,16 +110,21 @@ static int rtp_write_header(AVFormatContext *s1)
107 107
         s->first_rtcp_ntp_time = (s1->start_time_realtime / 1000) * 1000 +
108 108
                                  NTP_OFFSET_US;
109 109
 
110
-    max_packet_size = s1->pb->max_packet_size;
111
-    if (max_packet_size <= 12) {
112
-        av_log(s1, AV_LOG_ERROR, "Max packet size %d too low\n", max_packet_size);
110
+    if (s->max_packet_size) {
111
+        if (s1->pb->max_packet_size)
112
+            s->max_packet_size = FFMIN(s->max_payload_size,
113
+                                       s1->pb->max_packet_size);
114
+    } else
115
+        s->max_packet_size = s1->pb->max_packet_size;
116
+    if (s->max_packet_size <= 12) {
117
+        av_log(s1, AV_LOG_ERROR, "Max packet size %d too low\n", s->max_packet_size);
113 118
         return AVERROR(EIO);
114 119
     }
115
-    s->buf = av_malloc(max_packet_size);
120
+    s->buf = av_malloc(s->max_packet_size);
116 121
     if (s->buf == NULL) {
117 122
         return AVERROR(ENOMEM);
118 123
     }
119
-    s->max_payload_size = max_packet_size - 12;
124
+    s->max_payload_size = s->max_packet_size - 12;
120 125
 
121 126
     s->max_frames_per_packet = 0;
122 127
     if (s1->max_delay) {
... ...
@@ -386,8 +394,9 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
386 386
 
387 387
     rtcp_bytes = ((s->octet_count - s->last_octet_count) * RTCP_TX_RATIO_NUM) /
388 388
         RTCP_TX_RATIO_DEN;
389
-    if (s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
390
-                           (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) {
389
+    if ((s->first_packet || ((rtcp_bytes >= RTCP_SR_SIZE) &&
390
+                            (ff_ntp_time() - s->last_rtcp_ntp_time > 5000000))) &&
391
+        !(s->flags & FF_RTP_FLAG_SKIP_RTCP)) {
391 392
         rtcp_send_sr(s1, ff_ntp_time());
392 393
         s->last_octet_count = s->octet_count;
393 394
         s->first_packet = 0;
... ...
@@ -443,6 +452,11 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
443 443
         ff_rtp_send_h264(s1, pkt->data, size);
444 444
         break;
445 445
     case CODEC_ID_H263:
446
+        if (s->flags & FF_RTP_FLAG_RFC2190) {
447
+            ff_rtp_send_h263_rfc2190(s1, pkt->data, size);
448
+            break;
449
+        }
450
+        /* Fallthrough */
446 451
     case CODEC_ID_H263P:
447 452
         ff_rtp_send_h263(s1, pkt->data, size);
448 453
         break;
... ...
@@ -34,6 +34,7 @@ struct RTPMuxContext {
34 34
     uint32_t timestamp;
35 35
     uint32_t base_timestamp;
36 36
     uint32_t cur_timestamp;
37
+    int max_packet_size;
37 38
     int max_payload_size;
38 39
     int num_frames;
39 40
 
... ...
@@ -64,15 +65,20 @@ struct RTPMuxContext {
64 64
 typedef struct RTPMuxContext RTPMuxContext;
65 65
 
66 66
 #define FF_RTP_FLAG_MP4A_LATM 1
67
+#define FF_RTP_FLAG_RFC2190   2
68
+#define FF_RTP_FLAG_SKIP_RTCP 4
67 69
 
68 70
 #define FF_RTP_FLAG_OPTS(ctx, fieldname) \
69 71
     { "rtpflags", "RTP muxer flags", offsetof(ctx, fieldname), AV_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "rtpflags" }, \
70
-    { "latm", "Use MP4A-LATM packetization instead of MPEG4-GENERIC for AAC", 0, AV_OPT_TYPE_CONST, {.dbl = FF_RTP_FLAG_MP4A_LATM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "rtpflags" } \
72
+    { "latm", "Use MP4A-LATM packetization instead of MPEG4-GENERIC for AAC", 0, AV_OPT_TYPE_CONST, {.dbl = FF_RTP_FLAG_MP4A_LATM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "rtpflags" }, \
73
+    { "rfc2190", "Use RFC 2190 packetization instead of RFC 4629 for H.263", 0, AV_OPT_TYPE_CONST, {.dbl = FF_RTP_FLAG_RFC2190}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "rtpflags" }, \
74
+    { "skip_rtcp", "Don't send RTCP sender reports", 0, AV_OPT_TYPE_CONST, {.dbl = FF_RTP_FLAG_SKIP_RTCP}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "rtpflags" }, \
71 75
 
72 76
 void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m);
73 77
 
74 78
 void ff_rtp_send_h264(AVFormatContext *s1, const uint8_t *buf1, int size);
75 79
 void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size);
80
+void ff_rtp_send_h263_rfc2190(AVFormatContext *s1, const uint8_t *buf1, int size);
76 81
 void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size);
77 82
 void ff_rtp_send_latm(AVFormatContext *s1, const uint8_t *buff, int size);
78 83
 void ff_rtp_send_amr(AVFormatContext *s1, const uint8_t *buff, int size);
... ...
@@ -80,4 +86,7 @@ void ff_rtp_send_mpegvideo(AVFormatContext *s1, const uint8_t *buf1, int size);
80 80
 void ff_rtp_send_xiph(AVFormatContext *s1, const uint8_t *buff, int size);
81 81
 void ff_rtp_send_vp8(AVFormatContext *s1, const uint8_t *buff, int size);
82 82
 
83
+const uint8_t *ff_h263_find_resync_marker_reverse(const uint8_t *restrict start,
84
+                                                  const uint8_t *restrict end);
85
+
83 86
 #endif /* AVFORMAT_RTPENC_H */
... ...
@@ -23,8 +23,8 @@
23 23
 #include "avformat.h"
24 24
 #include "rtpenc.h"
25 25
 
26
-static const uint8_t *find_resync_marker_reverse(const uint8_t *restrict start,
27
-                                                 const uint8_t *restrict end)
26
+const uint8_t *ff_h263_find_resync_marker_reverse(const uint8_t *restrict start,
27
+                                                  const uint8_t *restrict end)
28 28
 {
29 29
     const uint8_t *p = end - 1;
30 30
     start += 1; /* Make sure we never return the original start. */
... ...
@@ -63,7 +63,8 @@ void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size)
63 63
 
64 64
         /* Look for a better place to split the frame into packets. */
65 65
         if (len < size) {
66
-            const uint8_t *end = find_resync_marker_reverse(buf1, buf1 + len);
66
+            const uint8_t *end = ff_h263_find_resync_marker_reverse(buf1,
67
+                                                                    buf1 + len);
67 68
             len = end - buf1;
68 69
         }
69 70
 
70 71
new file mode 100644
... ...
@@ -0,0 +1,104 @@
0
+/*
1
+ * RTP packetization for H.263 video
2
+ * Copyright (c) 2012 Martin Storsjo
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * Libav is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "rtpenc.h"
23
+#include "libavcodec/put_bits.h"
24
+#include "libavcodec/get_bits.h"
25
+
26
+struct H263Info {
27
+    int src;
28
+    int i;
29
+    int u;
30
+    int s;
31
+    int a;
32
+    int pb;
33
+    int tr;
34
+};
35
+
36
+static void send_mode_a(AVFormatContext *s1, const struct H263Info *info,
37
+                        const uint8_t *buf, int len, int m)
38
+{
39
+    RTPMuxContext *s = s1->priv_data;
40
+    PutBitContext pb;
41
+
42
+    init_put_bits(&pb, s->buf, 32);
43
+    put_bits(&pb, 1, 0); /* F - 0, mode A */
44
+    put_bits(&pb, 1, 0); /* P - 0, normal I/P */
45
+    put_bits(&pb, 3, 0); /* SBIT - 0 bits */
46
+    put_bits(&pb, 3, 0); /* EBIT - 0 bits */
47
+    put_bits(&pb, 3, info->src); /* SRC - source format */
48
+    put_bits(&pb, 1, info->i); /* I - inter/intra */
49
+    put_bits(&pb, 1, info->u); /* U - unrestricted motion vector */
50
+    put_bits(&pb, 1, info->s); /* S - syntax-baesd arithmetic coding */
51
+    put_bits(&pb, 1, info->a); /* A - advanced prediction */
52
+    put_bits(&pb, 4, 0); /* R - reserved */
53
+    put_bits(&pb, 2, 0); /* DBQ - 0 */
54
+    put_bits(&pb, 3, 0); /* TRB - 0 */
55
+    put_bits(&pb, 8, info->tr); /* TR */
56
+    flush_put_bits(&pb);
57
+    memcpy(s->buf + 4, buf, len);
58
+
59
+    ff_rtp_send_data(s1, s->buf, len + 4, m);
60
+}
61
+
62
+void ff_rtp_send_h263_rfc2190(AVFormatContext *s1, const uint8_t *buf, int size)
63
+{
64
+    RTPMuxContext *s = s1->priv_data;
65
+    int len;
66
+    GetBitContext gb;
67
+    struct H263Info info = { 0 };
68
+
69
+    s->timestamp = s->cur_timestamp;
70
+
71
+    init_get_bits(&gb, buf, size*8);
72
+    if (get_bits(&gb, 22) == 0x20) { /* Picture Start Code */
73
+        info.tr  = get_bits(&gb, 8);
74
+        skip_bits(&gb, 2); /* PTYPE start, H261 disambiguation */
75
+        skip_bits(&gb, 3); /* Split screen, document camera, freeze picture release */
76
+        info.src = get_bits(&gb, 3);
77
+        info.i   = get_bits(&gb, 1);
78
+        info.u   = get_bits(&gb, 1);
79
+        info.s   = get_bits(&gb, 1);
80
+        info.a   = get_bits(&gb, 1);
81
+        info.pb  = get_bits(&gb, 1);
82
+    }
83
+
84
+    while (size > 0) {
85
+        len = FFMIN(s->max_payload_size - 4, size);
86
+
87
+        /* Look for a better place to split the frame into packets. */
88
+        if (len < size) {
89
+            const uint8_t *end = ff_h263_find_resync_marker_reverse(buf,
90
+                                                                    buf + len);
91
+            len = end - buf;
92
+            if (len == s->max_payload_size - 4)
93
+                av_log(s1, AV_LOG_WARNING,
94
+                       "No GOB boundary found within MTU size, splitting at "
95
+                       "a random boundary\n");
96
+        }
97
+
98
+        send_mode_a(s1, &info, buf, len, len == size);
99
+
100
+        buf  += len;
101
+        size -= len;
102
+    }
103
+}
... ...
@@ -73,7 +73,7 @@
73 73
 
74 74
 const AVOption ff_rtsp_options[] = {
75 75
     { "initial_pause",  "Don't start playing the stream immediately", OFFSET(initial_pause), AV_OPT_TYPE_INT, {0}, 0, 1, DEC },
76
-    FF_RTP_FLAG_OPTS(RTSPState, rtp_muxer_flags),
76
+    FF_RTP_FLAG_OPTS(RTSPState, rtp_muxer_flags)
77 77
     { "rtsp_transport", "RTSP transport protocols", OFFSET(lower_transport_mask), AV_OPT_TYPE_FLAGS, {0}, INT_MIN, INT_MAX, DEC|ENC, "rtsp_transport" }, \
78 78
     { "udp", "UDP", 0, AV_OPT_TYPE_CONST, {1 << RTSP_LOWER_TRANSPORT_UDP}, 0, 0, DEC|ENC, "rtsp_transport" }, \
79 79
     { "tcp", "TCP", 0, AV_OPT_TYPE_CONST, {1 << RTSP_LOWER_TRANSPORT_TCP}, 0, 0, DEC|ENC, "rtsp_transport" }, \
... ...
@@ -404,6 +404,9 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c,
404 404
              * actually specifies the maximum video size, but we only know
405 405
              * the current size. This is required for playback on Android
406 406
              * stagefright and on Samsung bada. */
407
+            if (!fmt || !fmt->oformat->priv_class ||
408
+                !av_opt_flag_is_set(fmt->priv_data, "rtpflags", "rfc2190") ||
409
+                c->codec_id == CODEC_ID_H263P)
407 410
             av_strlcatf(buff, size, "a=rtpmap:%d H263-2000/90000\r\n"
408 411
                                     "a=framesize:%d %d-%d\r\n",
409 412
                                     payload_type,
... ...
@@ -84,7 +84,7 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
84 84
     SWFContext *swf = s->priv_data;
85 85
     AVIOContext *pb = s->pb;
86 86
     AVStream *vst = NULL, *ast = NULL, *st = 0;
87
-    int tag, len, i, frame, v;
87
+    int tag, len, i, frame, v, res;
88 88
 
89 89
     for(;;) {
90 90
         uint64_t pos = avio_tell(pb);
... ...
@@ -147,7 +147,8 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
147 147
                 st = s->streams[i];
148 148
                 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
149 149
                     frame = avio_rl16(pb);
150
-                    av_get_packet(pb, pkt, len-2);
150
+                    if ((res = av_get_packet(pb, pkt, len-2)) < 0)
151
+                        return res;
151 152
                     pkt->pos = pos;
152 153
                     pkt->pts = frame;
153 154
                     pkt->stream_index = st->index;
... ...
@@ -160,9 +161,11 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
160 160
                 if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
161 161
             if (st->codec->codec_id == CODEC_ID_MP3) {
162 162
                 avio_skip(pb, 4);
163
-                av_get_packet(pb, pkt, len-4);
163
+                if ((res = av_get_packet(pb, pkt, len-4)) < 0)
164
+                    return res;
164 165
             } else { // ADPCM, PCM
165
-                av_get_packet(pb, pkt, len);
166
+                if ((res = av_get_packet(pb, pkt, len)) < 0)
167
+                    return res;
166 168
             }
167 169
             pkt->pos = pos;
168 170
             pkt->stream_index = st->index;
... ...
@@ -187,7 +190,8 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
187 187
                 st = vst;
188 188
             }
189 189
             avio_rl16(pb); /* BITMAP_ID */
190
-            av_new_packet(pkt, len-2);
190
+            if ((res = av_new_packet(pkt, len-2)) < 0)
191
+                return res;
191 192
             avio_read(pb, pkt->data, 4);
192 193
             if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
193 194
                 AV_RB32(pkt->data) == 0xffd9ffd8) {
... ...
@@ -433,12 +433,14 @@ static int swScale(SwsContext *c, const uint8_t* src[],
433 433
         };
434 434
         int use_mmx_vfilter= c->use_mmx_vfilter;
435 435
 
436
-        const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
437
-        const int firstLumSrcY2= vLumFilterPos[FFMIN(dstY | ((1<<c->chrDstVSubSample) - 1), dstH-1)];
438
-        const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
439
-        int lastLumSrcY= firstLumSrcY + vLumFilterSize -1; // Last line needed as input
440
-        int lastLumSrcY2=firstLumSrcY2+ vLumFilterSize -1; // Last line needed as input
441
-        int lastChrSrcY= firstChrSrcY + vChrFilterSize -1; // Last line needed as input
436
+        const int firstLumSrcY= FFMAX(1 - vLumFilterSize, vLumFilterPos[dstY]); //First line needed as input
437
+        const int firstLumSrcY2= FFMAX(1 - vLumFilterSize, vLumFilterPos[FFMIN(dstY | ((1<<c->chrDstVSubSample) - 1), dstH-1)]);
438
+        const int firstChrSrcY= FFMAX(1 - vChrFilterSize, vChrFilterPos[chrDstY]); //First line needed as input
439
+
440
+        // Last line needed as input
441
+        int lastLumSrcY  = FFMIN(c->srcH,    firstLumSrcY  + vLumFilterSize) - 1;
442
+        int lastLumSrcY2 = FFMIN(c->srcH,    firstLumSrcY2 + vLumFilterSize) - 1;
443
+        int lastChrSrcY  = FFMIN(c->chrSrcH, firstChrSrcY  + vChrFilterSize) - 1;
442 444
         int enough_lines;
443 445
 
444 446
         //handle holes (FAST_BILINEAR & weird filters)
... ...
@@ -275,7 +275,8 @@ static int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSi
275 275
         if (xInc <= 1<<16)      filterSize= 1 + sizeFactor; // upscale
276 276
         else                    filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
277 277
 
278
-        filterSize = av_clip(filterSize, 1, srcW - 2);
278
+        filterSize = FFMIN(filterSize, srcW - 2);
279
+        filterSize = FFMAX(filterSize, 1);
279 280
 
280 281
         FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
281 282
 
... ...
@@ -841,8 +842,8 @@ int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SwsFilter *dstFilter)
841 841
     if (!dstFilter) dstFilter= &dummyFilter;
842 842
     if (!srcFilter) srcFilter= &dummyFilter;
843 843
 
844
-    c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
845
-    c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
844
+    c->lumXInc= (((int64_t)srcW<<16) + (dstW>>1))/dstW;
845
+    c->lumYInc= (((int64_t)srcH<<16) + (dstH>>1))/dstH;
846 846
     c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
847 847
     c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
848 848
     c->vRounder= 4* 0x0001000100010001ULL;
... ...
@@ -921,8 +922,8 @@ int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SwsFilter *dstFilter)
921 921
     else
922 922
         c->canMMX2BeUsed=0;
923 923
 
924
-    c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
925
-    c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
924
+    c->chrXInc= (((int64_t)c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
925
+    c->chrYInc= (((int64_t)c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
926 926
 
927 927
     // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
928 928
     // but only for the FAST_BILINEAR mode otherwise do correct scaling
... ...
@@ -937,8 +938,8 @@ int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SwsFilter *dstFilter)
937 937
         }
938 938
         //we don't use the x86 asm scaler if MMX is available
939 939
         else if (HAVE_MMX && cpu_flags & AV_CPU_FLAG_MMX && c->dstBpc <= 10) {
940
-            c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
941
-            c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
940
+            c->lumXInc = ((int64_t)(srcW-2)<<16)/(dstW-2) - 20;
941
+            c->chrXInc = ((int64_t)(c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
942 942
         }
943 943
     }
944 944
 
... ...
@@ -118,6 +118,44 @@ void updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufI
118 118
         const int16_t **chrUSrcPtr= (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
119 119
         const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
120 120
         int i;
121
+
122
+        if (firstLumSrcY < 0 || firstLumSrcY + vLumFilterSize > c->srcH) {
123
+            const int16_t **tmpY = (const int16_t **) lumPixBuf + 2 * vLumBufSize;
124
+            int neg = -firstLumSrcY, i, end = FFMIN(c->srcH - firstLumSrcY, vLumFilterSize);
125
+            for (i = 0; i < neg;            i++)
126
+                tmpY[i] = lumSrcPtr[neg];
127
+            for (     ; i < end;            i++)
128
+                tmpY[i] = lumSrcPtr[i];
129
+            for (     ; i < vLumFilterSize; i++)
130
+                tmpY[i] = tmpY[i-1];
131
+            lumSrcPtr = tmpY;
132
+
133
+            if (alpSrcPtr) {
134
+                const int16_t **tmpA = (const int16_t **) alpPixBuf + 2 * vLumBufSize;
135
+                for (i = 0; i < neg;            i++)
136
+                    tmpA[i] = alpSrcPtr[neg];
137
+                for (     ; i < end;            i++)
138
+                    tmpA[i] = alpSrcPtr[i];
139
+                for (     ; i < vLumFilterSize; i++)
140
+                    tmpA[i] = tmpA[i - 1];
141
+                alpSrcPtr = tmpA;
142
+            }
143
+        }
144
+        if (firstChrSrcY < 0 || firstChrSrcY + vChrFilterSize > c->chrSrcH) {
145
+            const int16_t **tmpU = (const int16_t **) chrUPixBuf + 2 * vChrBufSize;
146
+            int neg = -firstChrSrcY, i, end = FFMIN(c->chrSrcH - firstChrSrcY, vChrFilterSize);
147
+            for (i = 0; i < neg;            i++) {
148
+                tmpU[i] = chrUSrcPtr[neg];
149
+            }
150
+            for (     ; i < end;            i++) {
151
+                tmpU[i] = chrUSrcPtr[i];
152
+            }
153
+            for (     ; i < vChrFilterSize; i++) {
154
+                tmpU[i] = tmpU[i - 1];
155
+            }
156
+            chrUSrcPtr = tmpU;
157
+        }
158
+
121 159
         if (flags & SWS_ACCURATE_RND) {
122 160
             int s= APCK_SIZE / 8;
123 161
             for (i=0; i<vLumFilterSize; i+=2) {