Merge commit '1b3439b3055b083df51d7f7838ecc6b3f708b15c'
authorMichael Niedermayer <michaelni@gmx.at>
Wed, 19 Sep 2012 12:53:53 +0000 (14:53 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Wed, 19 Sep 2012 13:13:53 +0000 (15:13 +0200)
* commit '1b3439b3055b083df51d7f7838ecc6b3f708b15c':
  mpegvideo: move frame size dependent memory management to separate functions
  configure: add --toolchain option
  configure: Make the smoothstreaming muxer enable the ismv muxer
  smoothstreaming: Export the mp4 codec tags
  mov: check for EOF in long lasting loops
  avcodec: cleanup utils.c
  binkaudio: remove unneeded GET_BITS_SAFE macro
  binkaudio: use float sample format
  binkaudio: use a different value for the coefficient scale for the DCT codec

Conflicts:
configure
libavcodec/mpegvideo.c
libavcodec/utils.c
libavformat/Makefile

Merged-by: Michael Niedermayer <michaelni@gmx.at>
1  2 
configure
libavcodec/binkaudio.c
libavcodec/mpegvideo.c
libavcodec/utils.c
libavformat/Makefile
libavformat/mov.c
libavformat/smoothstreamingenc.c

diff --cc configure
+++ b/configure
@@@ -232,14 -205,13 +232,15 @@@ Advanced options (experts only)
    --target-os=OS           compiler targets OS [$target_os]
    --target-exec=CMD        command to run executables on target
    --target-path=DIR        path to view of build directory on target
 -  --nm=NM                  use nm tool
+   --toolchain=NAME         set tool defaults according to NAME
 +  --nm=NM                  use nm tool NM [$nm_default]
    --ar=AR                  use archive tool AR [$ar_default]
    --as=AS                  use assembler AS [$as_default]
 +  --yasmexe=EXE            use yasm-compatible assembler EXE [$yasmexe_default]
    --cc=CC                  use C compiler CC [$cc_default]
 +  --cxx=CXX                use C compiler CXX [$cxx_default]
    --dep-cc=DEPCC           use dependency generator DEPCC [$cc_default]
 -  --ld=LD                  use linker LD
 +  --ld=LD                  use linker LD [$ld_default]
    --host-cc=HOSTCC         use host C compiler HOSTCC
    --host-cflags=HCFLAGS    use HCFLAGS when compiling for host
    --host-ld=HOSTLD         use host linker HOSTLD
@@@ -1485,8 -1328,7 +1486,9 @@@ CMDLINE_SET=
      target_exec
      target_os
      target_path
+     toolchain
 +    valgrind
 +    yasmexe
  "
  
  CMDLINE_APPEND="
@@@ -2308,7 -2040,18 +2311,18 @@@ strip_default="${cross_prefix}${strip_d
  
  sysinclude_default="${sysroot}/usr/include"
  
 -set_default cc pkg_config sysinclude
+ case "$toolchain" in
+     msvc)
+         cc_default="c99wrap cl"
+         ld_default="c99wrap link"
+         nm_default="dumpbin -symbols"
+     ;;
+     ?*)
+         die "Unknown toolchain $toolchain"
+     ;;
+ esac
 +set_default cc cxx pkg_config strip sysinclude yasmexe
  enabled cross_compile || host_cc_default=$cc
  set_default host_cc
  
Simple merge
@@@ -657,6 -654,161 +657,131 @@@ void ff_MPV_decode_defaults(MpegEncCont
  }
  
  /**
 -    FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
 -                      fail); // error ressilience code looks cleaner with this
+  * Initialize and allocates MpegEncContext fields dependent on the resolution.
+  */
+ static int init_context_frame(MpegEncContext *s)
+ {
+     int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
+     s->mb_width   = (s->width + 15) / 16;
+     s->mb_stride  = s->mb_width + 1;
+     s->b8_stride  = s->mb_width * 2 + 1;
+     s->b4_stride  = s->mb_width * 4 + 1;
+     mb_array_size = s->mb_height * s->mb_stride;
+     mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
+     /* set default edge pos, will be overriden
+      * in decode_header if needed */
+     s->h_edge_pos = s->mb_width * 16;
+     s->v_edge_pos = s->mb_height * 16;
+     s->mb_num     = s->mb_width * s->mb_height;
+     s->block_wrap[0] =
+     s->block_wrap[1] =
+     s->block_wrap[2] =
+     s->block_wrap[3] = s->b8_stride;
+     s->block_wrap[4] =
+     s->block_wrap[5] = s->mb_stride;
+     y_size  = s->b8_stride * (2 * s->mb_height + 1);
+     c_size  = s->mb_stride * (s->mb_height + 1);
+     yc_size = y_size + 2   * c_size;
 -    s->mb_index2xy[s->mb_height * s->mb_width] =
 -        (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
++    FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
+     for (y = 0; y < s->mb_height; y++)
+         for (x = 0; x < s->mb_width; x++)
+             s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
 -        FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,
 -                          mv_table_size * 2 * sizeof(int16_t), fail);
 -        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,
 -                          mv_table_size * 2 * sizeof(int16_t), fail);
 -        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,
 -                          mv_table_size * 2 * sizeof(int16_t), fail);
 -        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,
 -                          mv_table_size * 2 * sizeof(int16_t), fail);
 -        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,
 -                          mv_table_size * 2 * sizeof(int16_t), fail);
 -        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,
 -                          mv_table_size * 2 * sizeof(int16_t), fail);
++    s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
+     if (s->encoding) {
+         /* Allocate MV tables */
 -        s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base +
 -                                   s->mb_stride + 1;
 -        s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base +
 -                                   s->mb_stride + 1;
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base,                 mv_table_size * 2 * sizeof(int16_t), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base,            mv_table_size * 2 * sizeof(int16_t), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base,            mv_table_size * 2 * sizeof(int16_t), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base,      mv_table_size * 2 * sizeof(int16_t), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base,      mv_table_size * 2 * sizeof(int16_t), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base,          mv_table_size * 2 * sizeof(int16_t), fail)
+         s->p_mv_table            = s->p_mv_table_base + s->mb_stride + 1;
+         s->b_forw_mv_table       = s->b_forw_mv_table_base + s->mb_stride + 1;
+         s->b_back_mv_table       = s->b_back_mv_table_base + s->mb_stride + 1;
 -        FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
 -                          sizeof(uint16_t), fail); // needed for encoding
++        s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
++        s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1;
+         s->b_direct_mv_table     = s->b_direct_mv_table_base + s->mb_stride + 1;
+         /* Allocate MB type table */
 -        FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
 -                          sizeof(int), fail);
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
 -    if (s->codec_id == AV_CODEC_ID_MPEG4 ||
 -        (s->flags & CODEC_FLAG_INTERLACED_ME)) {
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
+         FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
+                          mb_array_size * sizeof(float), fail);
+         FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
+                          mb_array_size * sizeof(float), fail);
+     }
+     FF_ALLOC_OR_GOTO(s->avctx, s->er_temp_buffer,
+                      mb_array_size * sizeof(uint8_t), fail);
+     FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table,
+                       mb_array_size * sizeof(uint8_t), fail);
 -                FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j],
 -                                  mb_array_size * 2 * sizeof(uint8_t), fail);
 -                FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j],
 -                                  mv_table_size * 2 * sizeof(int16_t), fail);
 -                s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
 -                                            + s->mb_stride + 1;
++    if (s->codec_id == AV_CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)) {
+         /* interlaced direct mode decoding tables */
+         for (i = 0; i < 2; i++) {
+             int j, k;
+             for (j = 0; j < 2; j++) {
+                 for (k = 0; k < 2; k++) {
+                     FF_ALLOCZ_OR_GOTO(s->avctx,
+                                       s->b_field_mv_table_base[i][j][k],
+                                       mv_table_size * 2 * sizeof(int16_t),
+                                       fail);
+                     s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
+                                                    s->mb_stride + 1;
+                 }
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i],
 -                              mb_array_size * 2 * sizeof(uint8_t), fail);
++                FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
++                FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
++                s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
+             }
 -        FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table,
 -                          mb_array_size * sizeof(uint8_t), fail);
 -        FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table,
 -                          mb_array_size * sizeof(uint8_t), fail);
++            FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
+         }
+     }
+     if (s->out_format == FMT_H263) {
+         /* cbp values */
+         FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
+         s->coded_block = s->coded_block_base + s->b8_stride + 1;
+         /* cbp, ac_pred, pred_dir */
 -        FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base,
 -                          yc_size * sizeof(int16_t), fail);
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table     , mb_array_size * sizeof(uint8_t), fail);
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
+     }
+     if (s->h263_pred || s->h263_plus || !s->encoding) {
+         /* dc values */
+         // MN: we need these for  error resilience of intra-frames
 -    if ((s->avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
 -        s->avctx->debug_mv) {
 -        s->visualization_buffer[0] = av_malloc((s->mb_width * 16 +
 -                    2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
 -        s->visualization_buffer[1] = av_malloc((s->mb_width * 16 +
 -                    2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
 -        s->visualization_buffer[2] = av_malloc((s->mb_width * 16 +
 -                    2 * EDGE_WIDTH) * s->mb_height * 16 + 2 * EDGE_WIDTH);
 -    }
 -
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
+         s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
+         s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
+         s->dc_val[2] = s->dc_val[1] + c_size;
+         for (i = 0; i < yc_size; i++)
+             s->dc_val_base[i] = 1024;
+     }
+     /* which mb is a intra block */
+     FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
+     memset(s->mbintra_table, 1, mb_array_size);
+     /* init macroblock skip table */
+     FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
+     // Note the + 1 is for  a quicker mpeg4 slice_end detection
+     return 0;
+ fail:
+     return AVERROR(ENOMEM);
+ }
+ /**
   * init common structure for both encoder and decoder.
   * this assumes that some variables like width/height are already set
   */
@@@ -701,89 -853,44 +826,36 @@@ av_cold int ff_MPV_common_init(MpegEncC
      s->flags  = s->avctx->flags;
      s->flags2 = s->avctx->flags2;
  
-     s->mb_width   = (s->width + 15) / 16;
-     s->mb_stride  = s->mb_width + 1;
-     s->b8_stride  = s->mb_width * 2 + 1;
-     s->b4_stride  = s->mb_width * 4 + 1;
-     mb_array_size = s->mb_height * s->mb_stride;
-     mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
 -    if (s->width && s->height) {
          /* set chroma shifts */
          avcodec_get_chroma_sub_sample(s->avctx->pix_fmt, &s->chroma_x_shift,
                                        &s->chroma_y_shift);
  
-     /* set default edge pos, will be overridden in decode_header if needed */
-     s->h_edge_pos = s->mb_width * 16;
-     s->v_edge_pos = s->mb_height * 16;
-     s->mb_num = s->mb_width * s->mb_height;
-     s->block_wrap[0] =
-     s->block_wrap[1] =
-     s->block_wrap[2] =
-     s->block_wrap[3] = s->b8_stride;
-     s->block_wrap[4] =
-     s->block_wrap[5] = s->mb_stride;
-     y_size = s->b8_stride * (2 * s->mb_height + 1);
-     c_size = s->mb_stride * (s->mb_height + 1);
-     yc_size = y_size + 2 * c_size;
 -        /* convert fourcc to upper case */
 -        s->codec_tag          = avpriv_toupper4(s->avctx->codec_tag);
 +    /* convert fourcc to upper case */
 +    s->codec_tag        = avpriv_toupper4(s->avctx->codec_tag);
 +    s->stream_codec_tag = avpriv_toupper4(s->avctx->stream_codec_tag);
  
 -        s->stream_codec_tag   = avpriv_toupper4(s->avctx->stream_codec_tag);
 +    s->avctx->coded_frame = &s->current_picture.f;
  
-     FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
-     for (y = 0; y < s->mb_height; y++)
-         for (x = 0; x < s->mb_width; x++)
-             s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
-     s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
 -        s->avctx->coded_frame = &s->current_picture.f;
 +    if (s->encoding) {
-         /* Allocate MV tables */
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base            , mv_table_size * 2 * sizeof(int16_t), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base       , mv_table_size * 2 * sizeof(int16_t), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base       , mv_table_size * 2 * sizeof(int16_t), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base , mv_table_size * 2 * sizeof(int16_t), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base     , mv_table_size * 2 * sizeof(int16_t), fail)
-         s->p_mv_table           = s->p_mv_table_base            + s->mb_stride + 1;
-         s->b_forw_mv_table      = s->b_forw_mv_table_base       + s->mb_stride + 1;
-         s->b_back_mv_table      = s->b_back_mv_table_base       + s->mb_stride + 1;
-         s->b_bidir_forw_mv_table= s->b_bidir_forw_mv_table_base + s->mb_stride + 1;
-         s->b_bidir_back_mv_table= s->b_bidir_back_mv_table_base + s->mb_stride + 1;
-         s->b_direct_mv_table    = s->b_direct_mv_table_base     + s->mb_stride + 1;
-         if(s->msmpeg4_version){
-             FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats, 2*2*(MAX_LEVEL+1)*(MAX_RUN+1)*2*sizeof(int), fail);
++        if (s->msmpeg4_version) {
++            FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
++                                2 * 2 * (MAX_LEVEL + 1) *
++                                (MAX_RUN + 1) * 2 * sizeof(int), fail);
 +        }
 +        FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
  
-         /* Allocate MB type table */
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type  , mb_array_size * sizeof(uint16_t), fail) //needed for encoding
 -        if (s->encoding) {
 -            if (s->msmpeg4_version) {
 -                FF_ALLOCZ_OR_GOTO(s->avctx, s->ac_stats,
 -                                  2 * 2 * (MAX_LEVEL + 1) *
 -                                  (MAX_RUN + 1) * 2 * sizeof(int), fail);
 -            }
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->avctx->stats_out, 256, fail);
--
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,
 -                              64 * 32   * sizeof(int), fail);
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,
 -                              64 * 32   * sizeof(int), fail);
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,
 -                              64 * 32 * 2 * sizeof(uint16_t), fail);
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,
 -                              64 * 32 * 2 * sizeof(uint16_t), fail);
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,
 -                              MAX_PICTURE_COUNT * sizeof(Picture *), fail);
 -            FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture,
 -                              MAX_PICTURE_COUNT * sizeof(Picture *), fail);
 -
 -            if (s->avctx->noise_reduction) {
 -                FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset,
 -                                  2 * 64 * sizeof(uint16_t), fail);
 -            }
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix,          64 * 32   * sizeof(int), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix,   64 * 32   * sizeof(int), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix,          64 * 32   * sizeof(int), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16,        64 * 32 * 2 * sizeof(uint16_t), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64 * 32 * 2 * sizeof(uint16_t), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16,        64 * 32 * 2 * sizeof(uint16_t), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture,           MAX_PICTURE_COUNT * sizeof(Picture *), fail)
++        FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture *), fail)
 +
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix         , 64*32   * sizeof(int), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix  , 64*32   * sizeof(int), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix         , 64*32   * sizeof(int), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->q_intra_matrix16       , 64*32*2 * sizeof(uint16_t), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->q_chroma_intra_matrix16, 64*32*2 * sizeof(uint16_t), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->q_inter_matrix16       , 64*32*2 * sizeof(uint16_t), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->reordered_input_picture, MAX_PICTURE_COUNT * sizeof(Picture*), fail)
-         if(s->avctx->noise_reduction){
-             FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail)
++        if (s->avctx->noise_reduction) {
++            FF_ALLOCZ_OR_GOTO(s->avctx, s->dct_offset, 2 * 64 * sizeof(uint16_t), fail);
          }
-             FF_ALLOC_OR_GOTO(s->avctx, s->cplx_tab,
-                              mb_array_size * sizeof(float), fail);
-             FF_ALLOC_OR_GOTO(s->avctx, s->bits_tab,
-                              mb_array_size * sizeof(float), fail);
      }
  
      s->picture_count = MAX_PICTURE_COUNT * FFMAX(1, s->avctx->thread_count);
          avcodec_get_frame_defaults(&s->picture[i].f);
      }
  
-         FF_ALLOC_OR_GOTO(s->avctx, s->er_temp_buffer,
-                          mb_array_size * sizeof(uint8_t), fail);
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->error_status_table,
-                           mb_array_size * sizeof(uint8_t), fail);
-         if(s->codec_id==AV_CODEC_ID_MPEG4 || (s->flags & CODEC_FLAG_INTERLACED_ME)){
-             /* interlaced direct mode decoding tables */
-             for (i = 0; i < 2; i++) {
-                 int j, k;
-                 for (j = 0; j < 2; j++) {
-                     for (k = 0; k < 2; k++) {
-                         FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_mv_table_base[i][j][k],  mv_table_size * 2 * sizeof(int16_t), fail)
-                         s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] + s->mb_stride + 1;
-                     }
-                     FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
-                     FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
-                     s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
-                 }
-                 FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
-             }
-         }
-         if (s->out_format == FMT_H263) {
-             /* cbp values */
-             FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
-             s->coded_block = s->coded_block_base + s->b8_stride + 1;
-             /* cbp, ac_pred, pred_dir */
-             FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table     , mb_array_size * sizeof(uint8_t), fail);
-             FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, mb_array_size * sizeof(uint8_t), fail);
-         }
-         if (s->h263_pred || s->h263_plus || !s->encoding) {
-             /* dc values */
-             // MN: we need these for  error resilience of intra-frames
-             FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
-             s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
-             s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
-             s->dc_val[2] = s->dc_val[1] + c_size;
-             for (i = 0; i < yc_size; i++)
-                 s->dc_val_base[i] = 1024;
-         }
-         /* which mb is a intra block */
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
-         memset(s->mbintra_table, 1, mb_array_size);
-         /* init macroblock skip table */
-         FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
-         // Note the + 1 is for  a quicker mpeg4 slice_end detection
 -    if (s->width && s->height) {
+         if ((err = init_context_frame(s)))
+             goto fail;
  
          s->parse_context.state = -1;
 -    }
  
 -    s->context_initialized = 1;
 -    s->thread_context[0]   = s;
 +        s->context_initialized = 1;
 +        s->thread_context[0]   = s;
  
 -    if (s->width && s->height) {
 +//     if (s->width && s->height) {
          if (nb_slices > 1) {
              for (i = 1; i < nb_slices; i++) {
                  s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
@@@ -938,10 -987,44 +950,49 @@@ static int free_context_frame(MpegEncCo
      av_freep(&s->er_temp_buffer);
      av_freep(&s->mb_index2xy);
      av_freep(&s->lambda_table);
++
+     av_freep(&s->cplx_tab);
+     av_freep(&s->bits_tab);
+     s->linesize = s->uvlinesize = 0;
+     for (i = 0; i < 3; i++)
+         av_freep(&s->visualization_buffer[i]);
+     if (!(s->avctx->active_thread_type & FF_THREAD_FRAME))
+         avcodec_default_free_buffers(s->avctx);
+     return 0;
+ }
+ /* init common structure for both encoder and decoder */
+ void ff_MPV_common_end(MpegEncContext *s)
+ {
+     int i;
+     if (s->slice_context_count > 1) {
+         for (i = 0; i < s->slice_context_count; i++) {
+             free_duplicate_context(s->thread_context[i]);
+         }
+         for (i = 1; i < s->slice_context_count; i++) {
+             av_freep(&s->thread_context[i]);
+         }
+         s->slice_context_count = 1;
+     } else free_duplicate_context(s);
+     av_freep(&s->parse_context.buffer);
+     s->parse_context.buffer_size = 0;
+     av_freep(&s->bitstream_buffer);
+     s->allocated_bitstream_buffer_size = 0;
+     av_freep(&s->avctx->stats_out);
+     av_freep(&s->ac_stats);
 +    if(s->q_chroma_intra_matrix   != s->q_intra_matrix  ) av_freep(&s->q_chroma_intra_matrix);
 +    if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16);
 +    s->q_chroma_intra_matrix=   NULL;
 +    s->q_chroma_intra_matrix16= NULL;
      av_freep(&s->q_intra_matrix);
      av_freep(&s->q_inter_matrix);
      av_freep(&s->q_intra_matrix16);
      av_freep(&s->input_picture);
      av_freep(&s->reordered_input_picture);
      av_freep(&s->dct_offset);
-     av_freep(&s->cplx_tab);
-     av_freep(&s->bits_tab);
  
 -    free_context_frame(s);
 -
      if (s->picture && !s->avctx->internal->is_copy) {
          for (i = 0; i < s->picture_count; i++) {
              free_picture(s, &s->picture[i]);
          }
      }
      av_freep(&s->picture);
++
++    free_context_frame(s);
++
      s->context_initialized      = 0;
      s->last_picture_ptr         =
      s->next_picture_ptr         =
@@@ -74,18 -75,13 +77,19 @@@ static inline int ff_fast_malloc(void *
  {
      void **p = ptr;
      if (min_size < *size)
 -        return;
 +        return 0;
-     min_size= FFMAX(17*min_size/16 + 32, min_size);
+     min_size = FFMAX(17 * min_size / 16 + 32, min_size);
      av_free(*p);
 -    *p = av_malloc(min_size);
 +    *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
-     if (!*p) min_size = 0;
-     *size= min_size;
+     if (!*p)
+         min_size = 0;
+     *size = min_size;
 +    return 1;
 +}
 +
 +void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
 +{
 +    ff_fast_malloc(ptr, size, min_size, 0);
  }
  
  void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
@@@ -160,14 -148,15 +167,15 @@@ unsigned avcodec_get_edge_width(void
      return EDGE_WIDTH;
  }
  
- void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
-     s->coded_width = width;
-     s->coded_height= height;
-     s->width = -((-width )>>s->lowres);
-     s->height= -((-height)>>s->lowres);
+ void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
+ {
+     s->coded_width  = width;
+     s->coded_height = height;
 -    s->width        = width;
 -    s->height       = height;
++    s->width        = -((-width ) >> s->lowres);
++    s->height       = -((-height) >> s->lowres);
  }
  
- #define INTERNAL_BUFFER_SIZE (32+1)
+ #define INTERNAL_BUFFER_SIZE (32 + 1)
  
  void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height,
                                 int linesize_align[AV_NUM_DATA_POINTERS])
          break;
      }
  
-     if(s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1){
-         w_align= FFMAX(w_align, 8);
++    if (s->codec_id == AV_CODEC_ID_IFF_ILBM || s->codec_id == AV_CODEC_ID_IFF_BYTERUN1) {
++        w_align = FFMAX(w_align, 8);
 +    }
 +
-     *width = FFALIGN(*width , w_align);
-     *height= FFALIGN(*height, h_align);
-     if(s->codec_id == AV_CODEC_ID_H264 || s->lowres)
-         *height+=2; // some of the optimized chroma MC reads one line too much
-                     // which is also done in mpeg decoders with lowres > 0
+     *width  = FFALIGN(*width, w_align);
+     *height = FFALIGN(*height, h_align);
 -    if (s->codec_id == AV_CODEC_ID_H264)
++    if (s->codec_id == AV_CODEC_ID_H264 || s->lowres)
+         // some of the optimized chroma MC reads one line too much
++        // which is also done in mpeg decoders with lowres > 0
+         *height += 2;
  
      for (i = 0; i < 4; i++)
          linesize_align[i] = STRIDE_ALIGN;
@@@ -282,43 -251,15 +293,44 @@@ void avcodec_align_dimensions(AVCodecCo
      int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
      int linesize_align[AV_NUM_DATA_POINTERS];
      int align;
      avcodec_align_dimensions2(s, width, height, linesize_align);
-     align = FFMAX(linesize_align[0], linesize_align[3]);
+     align               = FFMAX(linesize_align[0], linesize_align[3]);
      linesize_align[1] <<= chroma_shift;
      linesize_align[2] <<= chroma_shift;
-     align = FFMAX3(align, linesize_align[1], linesize_align[2]);
-     *width=FFALIGN(*width, align);
+     align               = FFMAX3(align, linesize_align[1], linesize_align[2]);
+     *width              = FFALIGN(*width, align);
  }
  
 +void ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
 +{
 +    if (s->pkt) {
 +        frame->pkt_pts = s->pkt->pts;
 +        frame->pkt_pos = s->pkt->pos;
 +        frame->pkt_duration = s->pkt->duration;
 +    } else {
 +        frame->pkt_pts = AV_NOPTS_VALUE;
 +        frame->pkt_pos = -1;
 +        frame->pkt_duration = 0;
 +    }
 +    frame->reordered_opaque = s->reordered_opaque;
 +
 +    switch (s->codec->type) {
 +    case AVMEDIA_TYPE_VIDEO:
 +        frame->width               = s->width;
 +        frame->height              = s->height;
 +        frame->format              = s->pix_fmt;
 +        frame->sample_aspect_ratio = s->sample_aspect_ratio;
 +        break;
 +    case AVMEDIA_TYPE_AUDIO:
 +        frame->sample_rate    = s->sample_rate;
 +        frame->format         = s->sample_fmt;
 +        frame->channel_layout = s->channel_layout;
 +        frame->channels       = s->channels;
 +        break;
 +    }
 +}
 +
  int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels,
                               enum AVSampleFormat sample_fmt, const uint8_t *buf,
                               int buf_size, int align)
@@@ -382,12 -323,12 +394,12 @@@ static int audio_get_buffer(AVCodecCont
          if (buf->extended_data[0] && buf_size > buf->audio_data_size) {
              av_free(buf->extended_data[0]);
              if (buf->extended_data != buf->data)
 -                av_free(&buf->extended_data);
 +                av_freep(&buf->extended_data);
              buf->extended_data = NULL;
-             buf->data[0] = NULL;
+             buf->data[0]       = NULL;
          }
          /* if number of channels has changed, reset and/or free extended data
-            pointers but leave data buffer in buf->data[0] for reuse */
+          * pointers but leave data buffer in buf->data[0] for reuse */
          if (buf->nb_channels != avctx->channels) {
              if (buf->extended_data != buf->data)
                  av_free(buf->extended_data);
          memcpy(frame->data, buf->data, sizeof(frame->data));
      }
  
-     frame->type          = FF_BUFFER_TYPE_INTERNAL;
+     frame->type = FF_BUFFER_TYPE_INTERNAL;
 -
 -    if (avctx->pkt)
 -        frame->pkt_pts = avctx->pkt->pts;
 -    else
 -        frame->pkt_pts = AV_NOPTS_VALUE;
 -    frame->reordered_opaque = avctx->reordered_opaque;
 -
 -    frame->sample_rate    = avctx->sample_rate;
 -    frame->format         = avctx->sample_fmt;
 -    frame->channel_layout = avctx->channel_layout;
 +    ff_init_buffer_info(avctx, frame);
  
      if (avctx->debug & FF_DEBUG_BUFFERS)
          av_log(avctx, AV_LOG_DEBUG, "default_get_buffer called on frame %p, "
@@@ -449,13 -399,11 +461,13 @@@ static int video_get_buffer(AVCodecCont
          return -1;
      }
  
-     if(av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
 -    if (av_image_check_size(w, h, 0, s))
++    if (av_image_check_size(w, h, 0, s) || s->pix_fmt<0) {
 +        av_log(s, AV_LOG_ERROR, "video_get_buffer: image parameters invalid\n");
          return -1;
 +    }
  
      if (!avci->buffer) {
-         avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE+1) *
+         avci->buffer = av_mallocz((INTERNAL_BUFFER_SIZE + 1) *
                                    sizeof(InternalBuffer));
      }
  
          memset(buf->base, 0, sizeof(buf->base));
          memset(buf->data, 0, sizeof(buf->data));
  
-         for(i=0; i<4 && size[i]; i++){
-             const int h_shift= i==0 ? 0 : h_chroma_shift;
-             const int v_shift= i==0 ? 0 : v_chroma_shift;
+         for (i = 0; i < 4 && size[i]; i++) {
+             const int h_shift = i == 0 ? 0 : h_chroma_shift;
+             const int v_shift = i == 0 ? 0 : v_chroma_shift;
  
-             buf->linesize[i]= picture.linesize[i];
+             buf->linesize[i] = picture.linesize[i];
  
-             buf->base[i]= av_malloc(size[i]+16); //FIXME 16
-             if(buf->base[i]==NULL)
+             buf->base[i] = av_malloc(size[i] + 16); //FIXME 16
+             if (buf->base[i] == NULL)
 -                return -1;
 +                return AVERROR(ENOMEM);
              memset(buf->base[i], 128, size[i]);
  
              // no edge if EDGE EMU or not planar YUV
      }
      pic->extended_data = pic->data;
      avci->buffer_count++;
-     pic->width  = buf->width;
-     pic->height = buf->height;
-     pic->format = buf->pix_fmt;
+     pic->width               = buf->width;
+     pic->height              = buf->height;
+     pic->format              = buf->pix_fmt;
 -    pic->sample_aspect_ratio = s->sample_aspect_ratio;
  
 -    if (s->pkt)
 -        pic->pkt_pts = s->pkt->pts;
 -    else
 -        pic->pkt_pts = AV_NOPTS_VALUE;
 -    pic->reordered_opaque = s->reordered_opaque;
 +    ff_init_buffer_info(s, pic);
  
-     if(s->debug&FF_DEBUG_BUFFERS)
+     if (s->debug & FF_DEBUG_BUFFERS)
          av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d "
-                "buffers used\n", pic, avci->buffer_count);
+                                 "buffers used\n", pic, avci->buffer_count);
  
      return 0;
  }
@@@ -576,9 -529,9 +588,9 @@@ void avcodec_default_release_buffer(AVC
      InternalBuffer *buf, *last;
      AVCodecInternal *avci = s->internal;
  
 -    assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
 +    av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
  
-     assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
+     assert(pic->type == FF_BUFFER_TYPE_INTERNAL);
      assert(avci->buffer_count);
  
      if (avci->buffer) {
@@@ -611,18 -564,10 +623,18 @@@ int avcodec_default_reget_buffer(AVCode
      AVFrame temp_pic;
      int i;
  
 -    assert(s->codec_type == AVMEDIA_TYPE_VIDEO);
 +    av_assert0(s->codec_type == AVMEDIA_TYPE_VIDEO);
 +
 +    if (pic->data[0] && (pic->width != s->width || pic->height != s->height || pic->format != s->pix_fmt)) {
 +        av_log(s, AV_LOG_WARNING, "Picture changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s in reget buffer()\n",
 +               pic->width, pic->height, av_get_pix_fmt_name(pic->format), s->width, s->height, av_get_pix_fmt_name(s->pix_fmt));
 +        s->release_buffer(s, pic);
 +    }
 +
 +    ff_init_buffer_info(s, pic);
  
      /* If no picture return a new buffer */
-     if(pic->data[0] == NULL) {
+     if (pic->data[0] == NULL) {
          /* We will copy from buffer, so must be readable */
          pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
          return s->get_buffer(s, pic);
      assert(s->pix_fmt == pic->format);
  
      /* If internal buffer type return the same buffer */
-     if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
+     if (pic->type == FF_BUFFER_TYPE_INTERNAL) {
 -        if (s->pkt)
 -            pic->pkt_pts = s->pkt->pts;
 -        else
 -            pic->pkt_pts = AV_NOPTS_VALUE;
 -        pic->reordered_opaque = s->reordered_opaque;
          return 0;
      }
  
@@@ -678,79 -633,28 +695,85 @@@ enum PixelFormat avcodec_default_get_fo
      return fmt[0];
  }
  
- void avcodec_get_frame_defaults(AVFrame *pic){
+ void avcodec_get_frame_defaults(AVFrame *pic)
+ {
      memset(pic, 0, sizeof(AVFrame));
  
-     pic->pts = pic->pkt_dts = pic->pkt_pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
-     pic->pkt_duration = 0;
-     pic->pkt_pos = -1;
-     pic->key_frame= 1;
-     pic->sample_aspect_ratio = (AVRational){0, 1};
-     pic->format = -1;           /* unknown */
 -    pic->pts                 = AV_NOPTS_VALUE;
++    pic->pts                   =
++    pic->pkt_dts               =
++    pic->pkt_pts               =
++    pic->best_effort_timestamp = AV_NOPTS_VALUE;
++    pic->pkt_duration        = 0;
++    pic->pkt_pos             = -1;
+     pic->key_frame           = 1;
+     pic->sample_aspect_ratio = (AVRational) {0, 1 };
+     pic->format              = -1; /* unknown */
  }
  
- AVFrame *avcodec_alloc_frame(void){
-     AVFrame *pic= av_malloc(sizeof(AVFrame));
+ AVFrame *avcodec_alloc_frame(void)
+ {
+     AVFrame *pic = av_malloc(sizeof(AVFrame));
  
-     if(pic==NULL) return NULL;
+     if (pic == NULL)
+         return NULL;
  
      avcodec_get_frame_defaults(pic);
  
      return pic;
  }
  
-     switch(ctx->codec_type) {
 +#define MAKE_ACCESSORS(str, name, type, field) \
 +    type av_##name##_get_##field(const str *s) { return s->field; } \
 +    void av_##name##_set_##field(str *s, type v) { s->field = v; }
 +
 +MAKE_ACCESSORS(AVFrame, frame, int64_t, best_effort_timestamp)
 +MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_duration)
 +MAKE_ACCESSORS(AVFrame, frame, int64_t, pkt_pos)
 +MAKE_ACCESSORS(AVFrame, frame, int64_t, channel_layout)
 +MAKE_ACCESSORS(AVFrame, frame, int,     channels)
 +MAKE_ACCESSORS(AVFrame, frame, int,     sample_rate)
 +MAKE_ACCESSORS(AVFrame, frame, AVDictionary *, metadata)
 +MAKE_ACCESSORS(AVFrame, frame, int,     decode_error_flags)
 +
 +MAKE_ACCESSORS(AVCodecContext, codec, AVRational, pkt_timebase)
 +MAKE_ACCESSORS(AVCodecContext, codec, const AVCodecDescriptor *, codec_descriptor)
 +
 +static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
 +{
 +    memset(sub, 0, sizeof(*sub));
 +    sub->pts = AV_NOPTS_VALUE;
 +}
 +
 +static int get_bit_rate(AVCodecContext *ctx)
 +{
 +    int bit_rate;
 +    int bits_per_sample;
 +
++    switch (ctx->codec_type) {
 +    case AVMEDIA_TYPE_VIDEO:
 +    case AVMEDIA_TYPE_DATA:
 +    case AVMEDIA_TYPE_SUBTITLE:
 +    case AVMEDIA_TYPE_ATTACHMENT:
 +        bit_rate = ctx->bit_rate;
 +        break;
 +    case AVMEDIA_TYPE_AUDIO:
 +        bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
 +        bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
 +        break;
 +    default:
 +        bit_rate = 0;
 +        break;
 +    }
 +    return bit_rate;
 +}
 +
 +#if FF_API_AVCODEC_OPEN
 +int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
 +{
 +    return avcodec_open2(avctx, codec, NULL);
 +}
 +#endif
 +
  int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
  {
      int ret = 0;
      if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
          goto free_and_end;
  
-     if(!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
-     if(avctx->coded_width && avctx->coded_height)
 +    if (codec->capabilities & CODEC_CAP_EXPERIMENTAL)
 +        if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
 +            av_log(avctx, AV_LOG_ERROR, "Codec is experimental but experimental codecs are not enabled, try -strict -2\n");
 +            ret = -1;
 +            goto free_and_end;
 +        }
 +
 +    //We only call avcodec_set_dimensions() for non h264 codecs so as not to overwrite previously setup dimensions
++    if (!( avctx->coded_width && avctx->coded_height && avctx->width && avctx->height && avctx->codec_id == AV_CODEC_ID_H264)){
++
+     if (avctx->coded_width && avctx->coded_height)
          avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
-     else if(avctx->width && avctx->height)
+     else if (avctx->width && avctx->height)
          avcodec_set_dimensions(avctx, avctx->width, avctx->height);
 +    }
  
      if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
          && (  av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
          }
      }
  
 -    if (avctx->codec->init && !(avctx->active_thread_type & FF_THREAD_FRAME)) {
 +    avctx->pts_correction_num_faulty_pts =
 +    avctx->pts_correction_num_faulty_dts = 0;
 +    avctx->pts_correction_last_pts =
 +    avctx->pts_correction_last_dts = INT64_MIN;
 +
-     if(avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME) || avctx->internal->frame_thread_encoder)){
++    if (   avctx->codec->init && (!(avctx->active_thread_type&FF_THREAD_FRAME)
++        || avctx->internal->frame_thread_encoder)) {
          ret = avctx->codec->init(avctx);
          if (ret < 0) {
              goto free_and_end;
@@@ -1029,13 -878,10 +1054,13 @@@ int ff_alloc_packet2(AVCodecContext *av
  
          av_init_packet(avpkt);
          avpkt->destruct = destruct;
-         avpkt->size = size;
+         avpkt->size     = size;
          return 0;
      } else {
 -        return av_new_packet(avpkt, size);
 +        int ret = av_new_packet(avpkt, size);
 +        if (ret < 0)
 +            av_log(avctx, AV_LOG_ERROR, "Failed to allocate packet of size %d\n", size);
 +        return ret;
      }
  }
  
@@@ -1264,13 -1080,10 +1289,13 @@@ int attribute_align_arg avcodec_encode_
              return ret;
  
          /* fabricate frame pts from sample count.
-            this is needed because the avcodec_encode_audio() API does not have
-            a way for the user to provide pts */
-         if(avctx->sample_rate && avctx->time_base.num)
+          * this is needed because the avcodec_encode_audio() API does not have
+          * a way for the user to provide pts */
 -        frame->pts = ff_samples_to_time_base(avctx,
 -                                             avctx->internal->sample_count);
++        if (avctx->sample_rate && avctx->time_base.num)
 +            frame->pts = ff_samples_to_time_base(avctx,
-                                                 avctx->internal->sample_count);
++                                                 avctx->internal->sample_count);
 +        else
 +            frame->pts = AV_NOPTS_VALUE;
          avctx->internal->sample_count += frame->nb_samples;
      } else {
          frame = NULL;
@@@ -1487,65 -1251,38 +1514,61 @@@ static void apply_param_change(AVCodecC
  }
  
  int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
-                          int *got_picture_ptr,
-                          const AVPacket *avpkt)
+                                               int *got_picture_ptr,
 -                                              AVPacket *avpkt)
++                                              const AVPacket *avpkt)
  {
      int ret;
 +    // copy to ensure we do not change avpkt
 +    AVPacket tmp = *avpkt;
 +
 +    if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) {
 +        av_log(avctx, AV_LOG_ERROR, "Invalid media type for video\n");
 +        return AVERROR(EINVAL);
 +    }
  
-     *got_picture_ptr= 0;
-     if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
+     *got_picture_ptr = 0;
+     if ((avctx->coded_width || avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
 -        return -1;
 -
 -    avctx->pkt = avpkt;
 -    apply_param_change(avctx, avpkt);
 +        return AVERROR(EINVAL);
  
-     if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
+     if ((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) {
 +        int did_split = av_packet_split_side_data(&tmp);
 +        apply_param_change(avctx, &tmp);
 +        avctx->pkt = &tmp;
-         if (HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
-              ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
-                                           &tmp);
+         if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
+             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
 -                                         avpkt);
++                                         &tmp);
          else {
              ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
-                               &tmp);
-             picture->pkt_dts= avpkt->dts;
 -                                       avpkt);
++                                       &tmp);
+             picture->pkt_dts             = avpkt->dts;
 -            picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
 -            picture->width               = avctx->width;
 -            picture->height              = avctx->height;
 -            picture->format              = avctx->pix_fmt;
 +
 +            if(!avctx->has_b_frames){
-             picture->pkt_pos= avpkt->pos;
++                picture->pkt_pos         = avpkt->pos;
 +            }
 +            //FIXME these should be under if(!avctx->has_b_frames)
-             if (!picture->sample_aspect_ratio.num)
-                 picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
-             if (!picture->width)
-                 picture->width = avctx->width;
-             if (!picture->height)
-                 picture->height = avctx->height;
-             if (picture->format == PIX_FMT_NONE)
-                 picture->format = avctx->pix_fmt;
++            if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
++            if (!picture->width)                   picture->width               = avctx->width;
++            if (!picture->height)                  picture->height              = avctx->height;
++            if (picture->format == PIX_FMT_NONE)   picture->format              = avctx->pix_fmt;
          }
  
          emms_c(); //needed to avoid an emms_c() call before every return;
  
 -        if (*got_picture_ptr)
 +        avctx->pkt = NULL;
 +        if (did_split) {
 +            ff_packet_free_side_data(&tmp);
 +            if(ret == tmp.size)
 +                ret = avpkt->size;
 +        }
 +
 +        if (*got_picture_ptr){
              avctx->frame_number++;
-     }else
-         ret= 0;
 +            picture->best_effort_timestamp = guess_correct_pts(avctx,
 +                                                            picture->pkt_pts,
 +                                                            picture->pkt_dts);
 +        }
+     } else
+         ret = 0;
  
      return ret;
  }
@@@ -1560,11 -1297,10 +1583,11 @@@ int attribute_align_arg avcodec_decode_
  
      if (avctx->get_buffer != avcodec_default_get_buffer) {
          av_log(avctx, AV_LOG_ERROR, "Custom get_buffer() for use with"
-                "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
+                                     "avcodec_decode_audio3() detected. Overriding with avcodec_default_get_buffer\n");
          av_log(avctx, AV_LOG_ERROR, "Please port your application to "
-                "avcodec_decode_audio4()\n");
+                                     "avcodec_decode_audio4()\n");
          avctx->get_buffer = avcodec_default_get_buffer;
 +        avctx->release_buffer = avcodec_default_release_buffer;
      }
  
      ret = avcodec_decode_audio4(avctx, &frame, &got_frame, avpkt);
@@@ -1779,21 -1440,10 +1802,21 @@@ av_cold int avcodec_close(AVCodecContex
      return 0;
  }
  
 +static enum AVCodecID remap_deprecated_codec_id(enum AVCodecID id)
 +{
 +    switch(id){
 +        //This is for future deprecatec codec ids, its empty since
 +        //last major bump but will fill up again over time, please don't remove it
 +//         case AV_CODEC_ID_UTVIDEO_DEPRECATED: return AV_CODEC_ID_UTVIDEO;
 +        default                         : return id;
 +    }
 +}
 +
  AVCodec *avcodec_find_encoder(enum AVCodecID id)
  {
-     AVCodec *p, *experimental=NULL;
+     AVCodec *p, *experimental = NULL;
      p = first_avcodec;
 +    id= remap_deprecated_codec_id(id);
      while (p) {
          if (av_codec_is_encoder(p) && p->id == id) {
              if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
@@@ -1875,18 -1523,13 +1898,18 @@@ size_t av_get_codec_tag_string(char *bu
  {
      int i, len, ret = 0;
  
 +#define IS_PRINT(x)                                               \
 +    (((x) >= '0' && (x) <= '9') ||                                \
 +     ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z') ||  \
 +     ((x) == '.' || (x) == ' ' || (x) == '-'))
 +
      for (i = 0; i < 4; i++) {
          len = snprintf(buf, buf_size,
 -                       isprint(codec_tag & 0xFF) ? "%c" : "[%d]", codec_tag & 0xFF);
 +                       IS_PRINT(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
-         buf      += len;
-         buf_size  = buf_size > len ? buf_size - len : 0;
-         ret      += len;
-         codec_tag>>=8;
+         buf        += len;
+         buf_size    = buf_size > len ? buf_size - len : 0;
+         ret        += len;
+         codec_tag >>= 8;
      }
      return ret;
  }
@@@ -1900,33 -1542,39 +1923,34 @@@ void avcodec_string(char *buf, int buf_
      int bitrate;
      AVRational display_aspect_ratio;
  
 -    if (enc->codec)
 -        p = enc->codec;
 -    else if (encode)
 -        p = avcodec_find_encoder(enc->codec_id);
 -    else
 -        p = avcodec_find_decoder(enc->codec_id);
 -
 -    if (p) {
 -        codec_name = p->name;
 -        profile = av_get_profile_name(p, enc->profile);
 -    } else if (enc->codec_id == AV_CODEC_ID_MPEG2TS) {
 -        /* fake mpeg2 transport stream codec (currently not
 -         * registered) */
 -        codec_name = "mpeg2ts";
 -    } else if (enc->codec_name[0] != '\0') {
 -        codec_name = enc->codec_name;
 -    } else {
 -        /* output avi tags */
 +    if (!buf || buf_size <= 0)
 +        return;
 +    codec_type = av_get_media_type_string(enc->codec_type);
 +    codec_name = avcodec_get_name(enc->codec_id);
 +    if (enc->profile != FF_PROFILE_UNKNOWN) {
 +        if (enc->codec)
 +            p = enc->codec;
 +        else
 +            p = encode ? avcodec_find_encoder(enc->codec_id) :
 +                        avcodec_find_decoder(enc->codec_id);
 +        if (p)
 +            profile = av_get_profile_name(p, enc->profile);
 +    }
 +
 +    snprintf(buf, buf_size, "%s: %s%s", codec_type ? codec_type : "unknown",
 +             codec_name, enc->mb_decision ? " (hq)" : "");
 +    buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
 +    if (profile)
 +        snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
 +    if (enc->codec_tag) {
          char tag_buf[32];
          av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
 -        snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
 -        codec_name = buf1;
 +        snprintf(buf + strlen(buf), buf_size - strlen(buf),
 +                 " (%s / 0x%04X)", tag_buf, enc->codec_tag);
      }
-     switch(enc->codec_type) {
+     switch (enc->codec_type) {
      case AVMEDIA_TYPE_VIDEO:
 -        snprintf(buf, buf_size,
 -                 "Video: %s%s",
 -                 codec_name, enc->mb_decision ? " (hq)" : "");
 -        if (profile)
 -            snprintf(buf + strlen(buf), buf_size - strlen(buf),
 -                     " (%s)", profile);
          if (enc->pix_fmt != PIX_FMT_NONE) {
              snprintf(buf + strlen(buf), buf_size - strlen(buf),
                       ", %s",
                       enc->width, enc->height);
              if (enc->sample_aspect_ratio.num) {
                  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
-                           enc->width*enc->sample_aspect_ratio.num,
-                           enc->height*enc->sample_aspect_ratio.den,
-                           1024*1024);
+                           enc->width * enc->sample_aspect_ratio.num,
+                           enc->height * enc->sample_aspect_ratio.den,
+                           1024 * 1024);
                  snprintf(buf + strlen(buf), buf_size - strlen(buf),
 -                         " [PAR %d:%d DAR %d:%d]",
 +                         " [SAR %d:%d DAR %d:%d]",
                           enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
                           display_aspect_ratio.num, display_aspect_ratio.den);
              }
@@@ -2001,16 -1665,9 +2025,16 @@@ const char *av_get_profile_name(const A
      return NULL;
  }
  
- unsigned avcodec_version( void )
+ unsigned avcodec_version(void)
  {
-   return LIBAVCODEC_VERSION_INT;
 +//    av_assert0(AV_CODEC_ID_V410==164);
 +    av_assert0(AV_CODEC_ID_PCM_S8_PLANAR==65563);
 +    av_assert0(AV_CODEC_ID_ADPCM_G722==69660);
 +//     av_assert0(AV_CODEC_ID_BMV_AUDIO==86071);
 +    av_assert0(AV_CODEC_ID_SRT==94216);
 +    av_assert0(LIBAVCODEC_VERSION_MICRO >= 100);
 +
+     return LIBAVCODEC_VERSION_INT;
  }
  
  const char *avcodec_configuration(void)
@@@ -2026,13 -1683,10 +2050,13 @@@ const char *avcodec_license(void
  
  void avcodec_flush_buffers(AVCodecContext *avctx)
  {
-     if(HAVE_THREADS && avctx->active_thread_type&FF_THREAD_FRAME)
+     if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME)
          ff_thread_flush(avctx);
-     else if(avctx->codec->flush)
+     else if (avctx->codec->flush)
          avctx->codec->flush(avctx);
 +
 +    avctx->pts_correction_last_pts =
 +    avctx->pts_correction_last_dts = INT64_MIN;
  }
  
  static void video_free_buffers(AVCodecContext *s)
@@@ -318,8 -292,7 +318,8 @@@ OBJS-$(CONFIG_SIFF_DEMUXER
  OBJS-$(CONFIG_SMACKER_DEMUXER)           += smacker.o
  OBJS-$(CONFIG_SMJPEG_DEMUXER)            += smjpegdec.o smjpeg.o
  OBJS-$(CONFIG_SMJPEG_MUXER)              += smjpegenc.o smjpeg.o
- OBJS-$(CONFIG_SMOOTHSTREAMING_MUXER)     += smoothstreamingenc.o
+ OBJS-$(CONFIG_SMOOTHSTREAMING_MUXER)     += smoothstreamingenc.o isom.o
 +OBJS-$(CONFIG_SMUSH_DEMUXER)             += smush.o
  OBJS-$(CONFIG_SOL_DEMUXER)               += sol.o pcm.o
  OBJS-$(CONFIG_SOX_DEMUXER)               += soxdec.o pcm.o
  OBJS-$(CONFIG_SOX_MUXER)                 += soxenc.o
Simple merge
Simple merge