Merge commit 'ce5870a3a8f2b10668ee4f04c2ae0287f66f31b2'
authorMark Thompson <sw@jkqxz.net>
Wed, 21 Feb 2018 22:22:54 +0000 (22:22 +0000)
committerMark Thompson <sw@jkqxz.net>
Wed, 21 Feb 2018 22:22:54 +0000 (22:22 +0000)
* commit 'ce5870a3a8f2b10668ee4f04c2ae0287f66f31b2':
  cbs: Refcount all the things!

Some changes for bitstream API.

Merged-by: Mark Thompson <sw@jkqxz.net>
15 files changed:
1  2 
libavcodec/cbs.c
libavcodec/cbs.h
libavcodec/cbs_h264.h
libavcodec/cbs_h2645.c
libavcodec/cbs_h265.h
libavcodec/cbs_internal.h
libavcodec/cbs_mpeg2.c
libavcodec/cbs_mpeg2.h
libavcodec/cbs_mpeg2_syntax_template.c
libavcodec/h264_metadata_bsf.c
libavcodec/h265_metadata_bsf.c
libavcodec/mpeg2_metadata_bsf.c
libavcodec/vaapi_encode_h264.c
libavcodec/vaapi_encode_h265.c
libavcodec/vaapi_encode_mpeg2.c

Simple merge
Simple merge
Simple merge
@@@ -288,12 -288,13 +288,13 @@@ static int cbs_h2645_read_more_rbsp_dat
  
  #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
  
 -#define byte_alignment(rw) (bitstream_tell(rw) % 8)
 +#define byte_alignment(rw) (get_bits_count(rw) % 8)
  
  #define allocate(name, size) do { \
-         name = av_mallocz(size); \
-         if (!name) \
+         name ## _ref = av_buffer_allocz(size); \
+         if (!name ## _ref) \
              return AVERROR(ENOMEM); \
+         name = name ## _ref->data; \
      } while (0)
  
  #define FUNC(name) FUNC_H264(READWRITE, name)
@@@ -494,7 -481,7 +481,7 @@@ static int cbs_h2645_fragment_add_nals(
          memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  
          err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
-                                       data, size);
 -                                      data, nal->size, NULL);
++                                      data, size, NULL);
          if (err < 0) {
              av_freep(&data);
              return err;
@@@ -705,35 -692,32 +692,32 @@@ static int cbs_h264_read_nal_unit(Coded
          {
              H264RawSPS *sps;
  
-             sps = av_mallocz(sizeof(*sps));
-             if (!sps)
-                 return AVERROR(ENOMEM);
+             err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps), NULL);
+             if (err < 0)
+                 return err;
+             sps = unit->content;
 -            err = cbs_h264_read_sps(ctx, &bc, sps);
 +            err = cbs_h264_read_sps(ctx, &gbc, sps);
-             if (err >= 0)
-                 err = cbs_h264_replace_sps(ctx, sps);
-             if (err < 0) {
-                 av_free(sps);
+             if (err < 0)
                  return err;
-             }
  
-             unit->content = sps;
+             err = cbs_h264_replace_sps(ctx, sps);
+             if (err < 0)
+                 return err;
          }
          break;
  
      case H264_NAL_SPS_EXT:
          {
-             H264RawSPSExtension *sps_ext;
-             sps_ext = av_mallocz(sizeof(*sps_ext));
-             if (!sps_ext)
-                 return AVERROR(ENOMEM);
-             err = cbs_h264_read_sps_extension(ctx, &gbc, sps_ext);
-             if (err < 0) {
-                 av_free(sps_ext);
+             err = ff_cbs_alloc_unit_content(ctx, unit,
+                                             sizeof(H264RawSPSExtension),
+                                             NULL);
+             if (err < 0)
                  return err;
-             }
  
-             unit->content = sps_ext;
 -            err = cbs_h264_read_sps_extension(ctx, &bc, unit->content);
++            err = cbs_h264_read_sps_extension(ctx, &gbc, unit->content);
+             if (err < 0)
+                 return err;
          }
          break;
  
          {
              H264RawPPS *pps;
  
-             pps = av_mallocz(sizeof(*pps));
-             if (!pps)
-                 return AVERROR(ENOMEM);
+             err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps), NULL);
+             if (err < 0)
+                 return err;
+             pps = unit->content;
 -            err = cbs_h264_read_pps(ctx, &bc, pps);
 +            err = cbs_h264_read_pps(ctx, &gbc, pps);
-             if (err >= 0)
-                 err = cbs_h264_replace_pps(ctx, pps);
-             if (err < 0) {
-                 av_free(pps);
+             if (err < 0)
                  return err;
-             }
  
-             unit->content = pps;
+             err = cbs_h264_replace_pps(ctx, pps);
+             if (err < 0)
+                 return err;
          }
          break;
  
              H264RawSlice *slice;
              int pos, len;
  
-             slice = av_mallocz(sizeof(*slice));
-             if (!slice)
-                 return AVERROR(ENOMEM);
+             err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
+                                             &cbs_h264_free_slice);
+             if (err < 0)
+                 return err;
+             slice = unit->content;
 -            err = cbs_h264_read_slice_header(ctx, &bc, &slice->header);
 +            err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
-             if (err < 0) {
-                 av_free(slice);
+             if (err < 0)
                  return err;
-             }
  
 -            pos = bitstream_tell(&bc);
 +            pos = get_bits_count(&gbc);
              len = unit->data_size;
              if (!unit->data[len - 1]) {
                  int z;
  
      case H264_NAL_AUD:
          {
-             H264RawAUD *aud;
-             aud = av_mallocz(sizeof(*aud));
-             if (!aud)
-                 return AVERROR(ENOMEM);
-             err = cbs_h264_read_aud(ctx, &gbc, aud);
-             if (err < 0) {
-                 av_free(aud);
+             err = ff_cbs_alloc_unit_content(ctx, unit,
+                                             sizeof(H264RawAUD), NULL);
+             if (err < 0)
                  return err;
-             }
  
-             unit->content = aud;
 -            err = cbs_h264_read_aud(ctx, &bc, unit->content);
++            err = cbs_h264_read_aud(ctx, &gbc, unit->content);
+             if (err < 0)
+                 return err;
          }
          break;
  
      case H264_NAL_SEI:
          {
-             H264RawSEI *sei;
-             sei = av_mallocz(sizeof(*sei));
-             if (!sei)
-                 return AVERROR(ENOMEM);
-             err = cbs_h264_read_sei(ctx, &gbc, sei);
-             if (err < 0) {
-                 cbs_h264_free_sei(sei);
-                 av_free(sei);
+             err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(H264RawSEI),
+                                             &cbs_h264_free_sei);
+             if (err < 0)
                  return err;
-             }
  
-             unit->content = sei;
 -            err = cbs_h264_read_sei(ctx, &bc, unit->content);
++            err = cbs_h264_read_sei(ctx, &gbc, unit->content);
+             if (err < 0)
+                 return err;
          }
          break;
  
@@@ -856,36 -829,38 +829,38 @@@ static int cbs_h265_read_nal_unit(Coded
          {
              H265RawVPS *vps;
  
-             vps = av_mallocz(sizeof(*vps));
-             if (!vps)
-                 return AVERROR(ENOMEM);
+             err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*vps),
+                                             &cbs_h265_free_vps);
+             if (err < 0)
+                 return err;
+             vps = unit->content;
 -            err = cbs_h265_read_vps(ctx, &bc, vps);
 +            err = cbs_h265_read_vps(ctx, &gbc, vps);
-             if (err >= 0)
-                 err = cbs_h265_replace_vps(ctx, vps);
-             if (err < 0) {
-                 av_free(vps);
+             if (err < 0)
                  return err;
-             }
  
-             unit->content = vps;
+             err = cbs_h265_replace_vps(ctx, vps);
+             if (err < 0)
+                 return err;
          }
          break;
      case HEVC_NAL_SPS:
          {
              H265RawSPS *sps;
  
-             sps = av_mallocz(sizeof(*sps));
-             if (!sps)
-                 return AVERROR(ENOMEM);
+             err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*sps),
+                                             &cbs_h265_free_sps);
+             if (err < 0)
+                 return err;
+             sps = unit->content;
 -            err = cbs_h265_read_sps(ctx, &bc, sps);
 +            err = cbs_h265_read_sps(ctx, &gbc, sps);
-             if (err >= 0)
-                 err = cbs_h265_replace_sps(ctx, sps);
-             if (err < 0) {
-                 av_free(sps);
+             if (err < 0)
                  return err;
-             }
  
-             unit->content = sps;
+             err = cbs_h265_replace_sps(ctx, sps);
+             if (err < 0)
+                 return err;
          }
          break;
  
          {
              H265RawPPS *pps;
  
-             pps = av_mallocz(sizeof(*pps));
-             if (!pps)
-                 return AVERROR(ENOMEM);
+             err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*pps),
+                                             &cbs_h265_free_pps);
+             if (err < 0)
+                 return err;
+             pps = unit->content;
 -            err = cbs_h265_read_pps(ctx, &bc, pps);
 +            err = cbs_h265_read_pps(ctx, &gbc, pps);
-             if (err >= 0)
-                 err = cbs_h265_replace_pps(ctx, pps);
-             if (err < 0) {
-                 av_free(pps);
+             if (err < 0)
                  return err;
-             }
  
-             unit->content = pps;
+             err = cbs_h265_replace_pps(ctx, pps);
+             if (err < 0)
+                 return err;
          }
          break;
  
              H265RawSlice *slice;
              int pos, len;
  
-             slice = av_mallocz(sizeof(*slice));
-             if (!slice)
-                 return AVERROR(ENOMEM);
+             err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
+                                             &cbs_h265_free_slice);
+             if (err < 0)
+                 return err;
+             slice = unit->content;
 -            err = cbs_h265_read_slice_segment_header(ctx, &bc, &slice->header);
 +            err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
-             if (err < 0) {
-                 av_free(slice);
+             if (err < 0)
                  return err;
-             }
  
 -            pos = bitstream_tell(&bc);
 +            pos = get_bits_count(&gbc);
              len = unit->data_size;
              if (!unit->data[len - 1]) {
                  int z;
  
      case HEVC_NAL_AUD:
          {
-             H265RawAUD *aud;
-             aud = av_mallocz(sizeof(*aud));
-             if (!aud)
-                 return AVERROR(ENOMEM);
-             err = cbs_h265_read_aud(ctx, &gbc, aud);
-             if (err < 0) {
-                 av_free(aud);
+             err = ff_cbs_alloc_unit_content(ctx, unit,
+                                             sizeof(H265RawAUD), NULL);
+             if (err < 0)
                  return err;
-             }
  
-             unit->content = aud;
 -            err = cbs_h265_read_aud(ctx, &bc, unit->content);
++            err = cbs_h265_read_aud(ctx, &gbc, unit->content);
+             if (err < 0)
+                 return err;
          }
          break;
  
Simple merge
Simple merge
@@@ -168,16 -183,17 +183,17 @@@ static int cbs_mpeg2_read_unit(CodedBit
          MPEG2RawSlice *slice;
          int pos, len;
  
-         slice = av_mallocz(sizeof(*slice));
-         if (!slice)
-             return AVERROR(ENOMEM);
+         err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
+                                         &cbs_mpeg2_free_slice);
+         if (err < 0)
+             return err;
+         slice = unit->content;
 -        err = cbs_mpeg2_read_slice_header(ctx, &bc, &slice->header);
 +        err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
-         if (err < 0) {
-             av_free(slice);
+         if (err < 0)
              return err;
-         }
  
 -        pos = bitstream_tell(&bc);
 +        pos = get_bits_count(&gbc);
          len = unit->data_size;
  
          slice->data_size = len - pos / 8;
          case start_code: \
              { \
                  type *header; \
-                 header = av_mallocz(sizeof(*header)); \
-                 if (!header) \
-                     return AVERROR(ENOMEM); \
-                 err = cbs_mpeg2_read_ ## func(ctx, &gbc, header); \
-                 if (err < 0) { \
-                     av_free(header); \
+                 err = ff_cbs_alloc_unit_content(ctx, unit, \
+                                                 sizeof(*header), free_func); \
+                 if (err < 0) \
+                     return err; \
+                 header = unit->content; \
 -                err = cbs_mpeg2_read_ ## read_func(ctx, &bc, header); \
++                err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
+                 if (err < 0) \
                      return err; \
-                 } \
-                 unit->content = header; \
              } \
              break;
-             START(0x00, MPEG2RawPictureHeader,  picture_header);
-             START(0xb2, MPEG2RawUserData,       user_data);
-             START(0xb3, MPEG2RawSequenceHeader, sequence_header);
-             START(0xb5, MPEG2RawExtensionData,  extension_data);
-             START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
+             START(0x00, MPEG2RawPictureHeader,  picture_header,  NULL);
+             START(0xb2, MPEG2RawUserData,       user_data,
+                                             &cbs_mpeg2_free_user_data);
+             START(0xb3, MPEG2RawSequenceHeader, sequence_header, NULL);
+             START(0xb5, MPEG2RawExtensionData,  extension_data,  NULL);
+             START(0xb8, MPEG2RawGroupOfPicturesHeader,
+                                        group_of_pictures_header, NULL);
  #undef START
          default:
              av_log(ctx->log_ctx, AV_LOG_ERROR, "Unknown start code %02"PRIx32".\n",
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge
Simple merge