LCOV - code coverage report
Current view: top level - libavcodec - cbs_h2645.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 432 657 65.8 %
Date: 2017-12-12 03:56:30 Functions: 24 24 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include "libavutil/attributes.h"
      20             : #include "libavutil/avassert.h"
      21             : 
      22             : #include "bytestream.h"
      23             : #include "cbs.h"
      24             : #include "cbs_internal.h"
      25             : #include "cbs_h264.h"
      26             : #include "cbs_h265.h"
      27             : #include "golomb.h"
      28             : #include "h264.h"
      29             : #include "h264_sei.h"
      30             : #include "h2645_parse.h"
      31             : #include "hevc.h"
      32             : 
      33             : 
      34       68238 : static int cbs_read_ue_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
      35             :                               const char *name, uint32_t *write_to,
      36             :                               uint32_t range_min, uint32_t range_max)
      37             : {
      38             :     uint32_t value;
      39             :     int position, i, j;
      40             :     unsigned int k;
      41             :     char bits[65];
      42             : 
      43       68238 :     position = get_bits_count(gbc);
      44             : 
      45      115711 :     for (i = 0; i < 32; i++) {
      46      115711 :         if (get_bits_left(gbc) < i + 1) {
      47           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
      48             :                    "%s: bitstream ended.\n", name);
      49           0 :             return AVERROR_INVALIDDATA;
      50             :         }
      51      115711 :         k = get_bits1(gbc);
      52      115711 :         bits[i] = k ? '1' : '0';
      53      115711 :         if (k)
      54       68238 :             break;
      55             :     }
      56       68238 :     if (i >= 32) {
      57           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid ue-golomb code at "
      58             :                "%s: more than 31 zeroes.\n", name);
      59           0 :         return AVERROR_INVALIDDATA;
      60             :     }
      61       68238 :     value = 1;
      62      115711 :     for (j = 0; j < i; j++) {
      63       47473 :         k = get_bits1(gbc);
      64       47473 :         bits[i + j + 1] = k ? '1' : '0';
      65       47473 :         value = value << 1 | k;
      66             :     }
      67       68238 :     bits[i + j + 1] = 0;
      68       68238 :     --value;
      69             : 
      70       68238 :     if (ctx->trace_enable)
      71           0 :         ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
      72             : 
      73       68238 :     if (value < range_min || value > range_max) {
      74           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
      75             :                "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
      76             :                name, value, range_min, range_max);
      77           0 :         return AVERROR_INVALIDDATA;
      78             :     }
      79             : 
      80       68238 :     *write_to = value;
      81       68238 :     return 0;
      82             : }
      83             : 
      84       75597 : static int cbs_read_se_golomb(CodedBitstreamContext *ctx, GetBitContext *gbc,
      85             :                               const char *name, int32_t *write_to,
      86             :                               int32_t range_min, int32_t range_max)
      87             : {
      88             :     int32_t value;
      89             :     int position, i, j;
      90             :     unsigned int k;
      91             :     uint32_t v;
      92             :     char bits[65];
      93             : 
      94       75597 :     position = get_bits_count(gbc);
      95             : 
      96      267582 :     for (i = 0; i < 32; i++) {
      97      267582 :         if (get_bits_left(gbc) < i + 1) {
      98           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
      99             :                    "%s: bitstream ended.\n", name);
     100           0 :             return AVERROR_INVALIDDATA;
     101             :         }
     102      267582 :         k = get_bits1(gbc);
     103      267582 :         bits[i] = k ? '1' : '0';
     104      267582 :         if (k)
     105       75597 :             break;
     106             :     }
     107       75597 :     if (i >= 32) {
     108           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid se-golomb code at "
     109             :                "%s: more than 31 zeroes.\n", name);
     110           0 :         return AVERROR_INVALIDDATA;
     111             :     }
     112       75597 :     v = 1;
     113      267582 :     for (j = 0; j < i; j++) {
     114      191985 :         k = get_bits1(gbc);
     115      191985 :         bits[i + j + 1] = k ? '1' : '0';
     116      191985 :         v = v << 1 | k;
     117             :     }
     118       75597 :     bits[i + j + 1] = 0;
     119       75597 :     if (v & 1)
     120       43722 :         value = -(int32_t)(v / 2);
     121             :     else
     122       31875 :         value = v / 2;
     123             : 
     124       75597 :     if (ctx->trace_enable)
     125           0 :         ff_cbs_trace_syntax_element(ctx, position, name, bits, value);
     126             : 
     127       75597 :     if (value < range_min || value > range_max) {
     128           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
     129             :                "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
     130             :                name, value, range_min, range_max);
     131           0 :         return AVERROR_INVALIDDATA;
     132             :     }
     133             : 
     134       75597 :     *write_to = value;
     135       75597 :     return 0;
     136             : }
     137             : 
     138       68238 : static int cbs_write_ue_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
     139             :                                const char *name, uint32_t value,
     140             :                                uint32_t range_min, uint32_t range_max)
     141             : {
     142             :     int len;
     143             : 
     144       68238 :     if (value < range_min || value > range_max) {
     145           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
     146             :                "%"PRIu32", but must be in [%"PRIu32",%"PRIu32"].\n",
     147             :                name, value, range_min, range_max);
     148           0 :         return AVERROR_INVALIDDATA;
     149             :     }
     150       68238 :     av_assert0(value != UINT32_MAX);
     151             : 
     152       68238 :     len = av_log2(value + 1);
     153       68238 :     if (put_bits_left(pbc) < 2 * len + 1)
     154           0 :         return AVERROR(ENOSPC);
     155             : 
     156       68238 :     if (ctx->trace_enable) {
     157             :         char bits[65];
     158             :         int i;
     159             : 
     160           0 :         for (i = 0; i < len; i++)
     161           0 :             bits[i] = '0';
     162           0 :         bits[len] = '1';
     163           0 :         for (i = 0; i < len; i++)
     164           0 :             bits[len + i + 1] = (value + 1) >> (len - i - 1) & 1 ? '1' : '0';
     165           0 :         bits[len + len + 1] = 0;
     166             : 
     167           0 :         ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
     168             :     }
     169             : 
     170       68238 :     put_bits(pbc, len, 0);
     171       68238 :     if (len + 1 < 32)
     172       68238 :         put_bits(pbc, len + 1, value + 1);
     173             :     else
     174           0 :         put_bits32(pbc, value + 1);
     175             : 
     176       68238 :     return 0;
     177             : }
     178             : 
     179       75597 : static int cbs_write_se_golomb(CodedBitstreamContext *ctx, PutBitContext *pbc,
     180             :                                const char *name, int32_t value,
     181             :                                int32_t range_min, int32_t range_max)
     182             : {
     183             :     int len;
     184             :     uint32_t uvalue;
     185             : 
     186       75597 :     if (value < range_min || value > range_max) {
     187           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "%s out of range: "
     188             :                "%"PRId32", but must be in [%"PRId32",%"PRId32"].\n",
     189             :                name, value, range_min, range_max);
     190           0 :         return AVERROR_INVALIDDATA;
     191             :     }
     192       75597 :     av_assert0(value != INT32_MIN);
     193             : 
     194       75597 :     if (value == 0)
     195       17114 :         uvalue = 0;
     196       58483 :     else if (value > 0)
     197       31875 :         uvalue = 2 * (uint32_t)value - 1;
     198             :     else
     199       26608 :         uvalue = 2 * (uint32_t)-value;
     200             : 
     201       75597 :     len = av_log2(uvalue + 1);
     202       75597 :     if (put_bits_left(pbc) < 2 * len + 1)
     203           0 :         return AVERROR(ENOSPC);
     204             : 
     205       75597 :     if (ctx->trace_enable) {
     206             :         char bits[65];
     207             :         int i;
     208             : 
     209           0 :         for (i = 0; i < len; i++)
     210           0 :             bits[i] = '0';
     211           0 :         bits[len] = '1';
     212           0 :         for (i = 0; i < len; i++)
     213           0 :             bits[len + i + 1] = (uvalue + 1) >> (len - i - 1) & 1 ? '1' : '0';
     214           0 :         bits[len + len + 1] = 0;
     215             : 
     216           0 :         ff_cbs_trace_syntax_element(ctx, put_bits_count(pbc), name, bits, value);
     217             :     }
     218             : 
     219       75597 :     put_bits(pbc, len, 0);
     220       75597 :     if (len + 1 < 32)
     221       75597 :         put_bits(pbc, len + 1, uvalue + 1);
     222             :     else
     223           0 :         put_bits32(pbc, uvalue + 1);
     224             : 
     225       75597 :     return 0;
     226             : }
     227             : 
     228             : #define HEADER(name) do { \
     229             :         ff_cbs_trace_header(ctx, name); \
     230             :     } while (0)
     231             : 
     232             : #define CHECK(call) do { \
     233             :         err = (call); \
     234             :         if (err < 0) \
     235             :             return err; \
     236             :     } while (0)
     237             : 
     238             : #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
     239             : #define FUNC_H264(rw, name) FUNC_NAME(rw, h264, name)
     240             : #define FUNC_H265(rw, name) FUNC_NAME(rw, h265, name)
     241             : 
     242             : 
     243             : #define READ
     244             : #define READWRITE read
     245             : #define RWContext GetBitContext
     246             : 
     247             : #define xu(width, name, var, range_min, range_max) do { \
     248             :         uint32_t value = range_min; \
     249             :         CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
     250             :                                    &value, range_min, range_max)); \
     251             :         var = value; \
     252             :     } while (0)
     253             : #define xue(name, var, range_min, range_max) do { \
     254             :         uint32_t value = range_min; \
     255             :         CHECK(cbs_read_ue_golomb(ctx, rw, #name, \
     256             :                                  &value, range_min, range_max)); \
     257             :         var = value; \
     258             :     } while (0)
     259             : #define xse(name, var, range_min, range_max) do { \
     260             :         int32_t value = range_min; \
     261             :         CHECK(cbs_read_se_golomb(ctx, rw, #name, \
     262             :                                  &value, range_min, range_max)); \
     263             :         var = value; \
     264             :     } while (0)
     265             : 
     266             : 
     267             : #define u(width, name, range_min, range_max) \
     268             :         xu(width, name, current->name, range_min, range_max)
     269             : #define flag(name) u(1, name, 0, 1)
     270             : #define ue(name, range_min, range_max) \
     271             :         xue(name, current->name, range_min, range_max)
     272             : #define se(name, range_min, range_max) \
     273             :         xse(name, current->name, range_min, range_max)
     274             : 
     275             : #define infer(name, value) do { \
     276             :         current->name = value; \
     277             :     } while (0)
     278             : 
     279        1459 : static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc)
     280             : {
     281        1459 :     int bits_left = get_bits_left(gbc);
     282        1459 :     if (bits_left > 8)
     283           1 :         return 1;
     284        1458 :     if (show_bits(gbc, bits_left) == 1 << (bits_left - 1))
     285        1458 :         return 0;
     286           0 :     return 1;
     287             : }
     288             : 
     289             : #define more_rbsp_data(var) ((var) = cbs_h2645_read_more_rbsp_data(rw))
     290             : 
     291             : #define byte_alignment(rw) (get_bits_count(rw) % 8)
     292             : 
     293             : #define allocate(name, size) do { \
     294             :         name = av_mallocz(size); \
     295             :         if (!name) \
     296             :             return AVERROR(ENOMEM); \
     297             :     } while (0)
     298             : 
     299             : #define FUNC(name) FUNC_H264(READWRITE, name)
     300             : #include "cbs_h264_syntax_template.c"
     301             : #undef FUNC
     302             : 
     303             : #define FUNC(name) FUNC_H265(READWRITE, name)
     304             : #include "cbs_h265_syntax_template.c"
     305             : #undef FUNC
     306             : 
     307             : #undef READ
     308             : #undef READWRITE
     309             : #undef RWContext
     310             : #undef xu
     311             : #undef xue
     312             : #undef xse
     313             : #undef u
     314             : #undef flag
     315             : #undef ue
     316             : #undef se
     317             : #undef infer
     318             : #undef more_rbsp_data
     319             : #undef byte_alignment
     320             : #undef allocate
     321             : 
     322             : 
     323             : #define WRITE
     324             : #define READWRITE write
     325             : #define RWContext PutBitContext
     326             : 
     327             : #define xu(width, name, var, range_min, range_max) do { \
     328             :         uint32_t value = var; \
     329             :         CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
     330             :                                     value, range_min, range_max)); \
     331             :     } while (0)
     332             : #define xue(name, var, range_min, range_max) do { \
     333             :         uint32_t value = var; \
     334             :         CHECK(cbs_write_ue_golomb(ctx, rw, #name, \
     335             :                                   value, range_min, range_max)); \
     336             :     } while (0)
     337             : #define xse(name, var, range_min, range_max) do { \
     338             :         int32_t value = var; \
     339             :         CHECK(cbs_write_se_golomb(ctx, rw, #name, \
     340             :                                   value, range_min, range_max)); \
     341             :     } while (0)
     342             : 
     343             : #define u(width, name, range_min, range_max) \
     344             :         xu(width, name, current->name, range_min, range_max)
     345             : #define flag(name) u(1, name, 0, 1)
     346             : #define ue(name, range_min, range_max) \
     347             :         xue(name, current->name, range_min, range_max)
     348             : #define se(name, range_min, range_max) \
     349             :         xse(name, current->name, range_min, range_max)
     350             : 
     351             : #define infer(name, value) do { \
     352             :         if (current->name != (value)) { \
     353             :             av_log(ctx->log_ctx, AV_LOG_WARNING, "Warning: " \
     354             :                    "%s does not match inferred value: " \
     355             :                    "%"PRId64", but should be %"PRId64".\n", \
     356             :                    #name, (int64_t)current->name, (int64_t)(value)); \
     357             :         } \
     358             :     } while (0)
     359             : 
     360             : #define more_rbsp_data(var) (var)
     361             : 
     362             : #define byte_alignment(rw) (put_bits_count(rw) % 8)
     363             : 
     364             : #define allocate(name, size) do { \
     365             :         if (!name) { \
     366             :             av_log(ctx->log_ctx, AV_LOG_ERROR, "%s must be set " \
     367             :                    "for writing.\n", #name); \
     368             :             return AVERROR_INVALIDDATA; \
     369             :         } \
     370             :     } while (0)
     371             : 
     372             : #define FUNC(name) FUNC_H264(READWRITE, name)
     373             : #include "cbs_h264_syntax_template.c"
     374             : #undef FUNC
     375             : 
     376             : #define FUNC(name) FUNC_H265(READWRITE, name)
     377             : #include "cbs_h265_syntax_template.c"
     378             : #undef FUNC
     379             : 
     380             : #undef WRITE
     381             : #undef READWRITE
     382             : #undef RWContext
     383             : #undef xu
     384             : #undef xue
     385             : #undef xse
     386             : #undef u
     387             : #undef flag
     388             : #undef ue
     389             : #undef se
     390             : #undef infer
     391             : #undef more_rbsp_data
     392             : #undef byte_alignment
     393             : #undef allocate
     394             : 
     395             : 
     396         278 : static void cbs_h264_free_sei(H264RawSEI *sei)
     397             : {
     398             :     int i;
     399         557 :     for (i = 0; i < sei->payload_count; i++) {
     400         279 :         H264RawSEIPayload *payload = &sei->payload[i];
     401             : 
     402         279 :         switch (payload->payload_type) {
     403         279 :         case H264_SEI_TYPE_BUFFERING_PERIOD:
     404             :         case H264_SEI_TYPE_PIC_TIMING:
     405             :         case H264_SEI_TYPE_RECOVERY_POINT:
     406             :         case H264_SEI_TYPE_DISPLAY_ORIENTATION:
     407         279 :             break;
     408           0 :         case H264_SEI_TYPE_USER_DATA_REGISTERED:
     409           0 :             av_freep(&payload->payload.user_data_registered.data);
     410           0 :             break;
     411           0 :         case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
     412           0 :             av_freep(&payload->payload.user_data_unregistered.data);
     413           0 :             break;
     414           0 :         default:
     415           0 :             av_freep(&payload->payload.other.data);
     416           0 :             break;
     417             :         }
     418             :     }
     419         278 : }
     420             : 
     421        4908 : static void cbs_h264_free_slice(H264RawSlice *slice)
     422             : {
     423        4908 :     av_freep(&slice->data);
     424        4908 : }
     425             : 
     426        6594 : static void cbs_h264_free_nal_unit(CodedBitstreamUnit *unit)
     427             : {
     428        6594 :     switch (unit->type) {
     429         278 :     case H264_NAL_SEI:
     430         278 :         cbs_h264_free_sei(unit->content);
     431         278 :         break;
     432        4908 :     case H264_NAL_IDR_SLICE:
     433             :     case H264_NAL_SLICE:
     434        4908 :         cbs_h264_free_slice(unit->content);
     435        4908 :         break;
     436             :     }
     437        6594 :     av_freep(&unit->content);
     438        6594 : }
     439             : 
     440        6258 : static void cbs_h265_free_nal_unit(CodedBitstreamUnit *unit)
     441             : {
     442        6258 :     switch (unit->type) {
     443          46 :     case HEVC_NAL_VPS:
     444          46 :         av_freep(&((H265RawVPS*)unit->content)->extension_data.data);
     445          46 :         break;
     446          48 :     case HEVC_NAL_SPS:
     447          48 :         av_freep(&((H265RawSPS*)unit->content)->extension_data.data);
     448          48 :         break;
     449         273 :     case HEVC_NAL_PPS:
     450         273 :         av_freep(&((H265RawPPS*)unit->content)->extension_data.data);
     451         273 :         break;
     452        5831 :     case HEVC_NAL_TRAIL_N:
     453             :     case HEVC_NAL_TRAIL_R:
     454             :     case HEVC_NAL_TSA_N:
     455             :     case HEVC_NAL_TSA_R:
     456             :     case HEVC_NAL_STSA_N:
     457             :     case HEVC_NAL_STSA_R:
     458             :     case HEVC_NAL_RADL_N:
     459             :     case HEVC_NAL_RADL_R:
     460             :     case HEVC_NAL_RASL_N:
     461             :     case HEVC_NAL_RASL_R:
     462             :     case HEVC_NAL_BLA_W_LP:
     463             :     case HEVC_NAL_BLA_W_RADL:
     464             :     case HEVC_NAL_BLA_N_LP:
     465             :     case HEVC_NAL_IDR_W_RADL:
     466             :     case HEVC_NAL_IDR_N_LP:
     467             :     case HEVC_NAL_CRA_NUT:
     468        5831 :         av_freep(&((H265RawSlice*)unit->content)->data);
     469        5831 :         break;
     470             :     }
     471        6258 :     av_freep(&unit->content);
     472        6258 : }
     473             : 
     474        4462 : static int cbs_h2645_fragment_add_nals(CodedBitstreamContext *ctx,
     475             :                                        CodedBitstreamFragment *frag,
     476             :                                        const H2645Packet *packet)
     477             : {
     478             :     int err, i;
     479             : 
     480       37638 :     for (i = 0; i < packet->nb_nals; i++) {
     481       14357 :         const H2645NAL *nal = &packet->nals[i];
     482       14357 :         size_t size = nal->size;
     483             :         uint8_t *data;
     484             : 
     485             :         // Remove trailing zeroes.
     486       36509 :         while (size > 0 && nal->data[size - 1] == 0)
     487        7795 :             --size;
     488       14357 :         av_assert0(size > 0);
     489             : 
     490       14357 :         data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
     491       14357 :         if (!data)
     492           0 :             return AVERROR(ENOMEM);
     493       14357 :         memcpy(data, nal->data, size);
     494       14357 :         memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
     495             : 
     496       14357 :         err = ff_cbs_insert_unit_data(ctx, frag, -1, nal->type,
     497             :                                       data, size);
     498       14357 :         if (err < 0) {
     499           0 :             av_freep(&data);
     500           0 :             return err;
     501             :         }
     502             :     }
     503             : 
     504        4462 :     return 0;
     505             : }
     506             : 
     507        4462 : static int cbs_h2645_split_fragment(CodedBitstreamContext *ctx,
     508             :                                     CodedBitstreamFragment *frag,
     509             :                                     int header)
     510             : {
     511        4462 :     enum AVCodecID codec_id = ctx->codec->codec_id;
     512        4462 :     CodedBitstreamH2645Context *priv = ctx->priv_data;
     513             :     GetByteContext gbc;
     514             :     int err;
     515             : 
     516        4462 :     av_assert0(frag->data && frag->nb_units == 0);
     517        4462 :     if (frag->data_size == 0)
     518           0 :         return 0;
     519             : 
     520        4462 :     if (header && frag->data[0] && codec_id == AV_CODEC_ID_H264) {
     521             :         // AVCC header.
     522             :         size_t size, start, end;
     523             :         int i, count, version;
     524             : 
     525           0 :         priv->mp4 = 1;
     526             : 
     527           0 :         bytestream2_init(&gbc, frag->data, frag->data_size);
     528             : 
     529           0 :         if (bytestream2_get_bytes_left(&gbc) < 6)
     530           0 :             return AVERROR_INVALIDDATA;
     531             : 
     532           0 :         version = bytestream2_get_byte(&gbc);
     533           0 :         if (version != 1) {
     534           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid AVCC header: "
     535             :                    "first byte %u.", version);
     536           0 :             return AVERROR_INVALIDDATA;
     537             :         }
     538             : 
     539           0 :         bytestream2_skip(&gbc, 3);
     540           0 :         priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
     541             : 
     542             :         // SPS array.
     543           0 :         count = bytestream2_get_byte(&gbc) & 0x1f;
     544           0 :         start = bytestream2_tell(&gbc);
     545           0 :         for (i = 0; i < count; i++) {
     546           0 :             if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
     547           0 :                 return AVERROR_INVALIDDATA;
     548           0 :             size = bytestream2_get_be16(&gbc);
     549           0 :             if (bytestream2_get_bytes_left(&gbc) < size)
     550           0 :                 return AVERROR_INVALIDDATA;
     551           0 :             bytestream2_skip(&gbc, size);
     552             :         }
     553           0 :         end = bytestream2_tell(&gbc);
     554             : 
     555           0 :         err = ff_h2645_packet_split(&priv->read_packet,
     556           0 :                                     frag->data + start, end - start,
     557             :                                     ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
     558           0 :         if (err < 0) {
     559           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC SPS array.\n");
     560           0 :             return err;
     561             :         }
     562           0 :         err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
     563           0 :         if (err < 0)
     564           0 :             return err;
     565             : 
     566             :         // PPS array.
     567           0 :         count = bytestream2_get_byte(&gbc);
     568           0 :         start = bytestream2_tell(&gbc);
     569           0 :         for (i = 0; i < count; i++) {
     570           0 :             if (bytestream2_get_bytes_left(&gbc) < 2 * (count - i))
     571           0 :                 return AVERROR_INVALIDDATA;
     572           0 :             size = bytestream2_get_be16(&gbc);
     573           0 :             if (bytestream2_get_bytes_left(&gbc) < size)
     574           0 :                 return AVERROR_INVALIDDATA;
     575           0 :             bytestream2_skip(&gbc, size);
     576             :         }
     577           0 :         end = bytestream2_tell(&gbc);
     578             : 
     579           0 :         err = ff_h2645_packet_split(&priv->read_packet,
     580           0 :                                     frag->data + start, end - start,
     581             :                                     ctx->log_ctx, 1, 2, AV_CODEC_ID_H264, 1);
     582           0 :         if (err < 0) {
     583           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split AVCC PPS array.\n");
     584           0 :             return err;
     585             :         }
     586           0 :         err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
     587           0 :         if (err < 0)
     588           0 :             return err;
     589             : 
     590           0 :         if (bytestream2_get_bytes_left(&gbc) > 0) {
     591           0 :             av_log(ctx->log_ctx, AV_LOG_WARNING, "%u bytes left at end of AVCC "
     592             :                    "header.\n", bytestream2_get_bytes_left(&gbc));
     593             :         }
     594             : 
     595        4462 :     } else if (header && frag->data[0] && codec_id == AV_CODEC_ID_HEVC) {
     596             :         // HVCC header.
     597             :         size_t size, start, end;
     598             :         int i, j, nb_arrays, nal_unit_type, nb_nals, version;
     599             : 
     600           0 :         priv->mp4 = 1;
     601             : 
     602           0 :         bytestream2_init(&gbc, frag->data, frag->data_size);
     603             : 
     604           0 :         if (bytestream2_get_bytes_left(&gbc) < 23)
     605           0 :             return AVERROR_INVALIDDATA;
     606             : 
     607           0 :         version = bytestream2_get_byte(&gbc);
     608           0 :         if (version != 1) {
     609           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid HVCC header: "
     610             :                    "first byte %u.", version);
     611           0 :             return AVERROR_INVALIDDATA;
     612             :         }
     613             : 
     614           0 :         bytestream2_skip(&gbc, 20);
     615           0 :         priv->nal_length_size = (bytestream2_get_byte(&gbc) & 3) + 1;
     616             : 
     617           0 :         nb_arrays = bytestream2_get_byte(&gbc);
     618           0 :         for (i = 0; i < nb_arrays; i++) {
     619           0 :             nal_unit_type = bytestream2_get_byte(&gbc) & 0x3f;
     620           0 :             nb_nals = bytestream2_get_be16(&gbc);
     621             : 
     622           0 :             start = bytestream2_tell(&gbc);
     623           0 :             for (j = 0; j < nb_nals; j++) {
     624           0 :                 if (bytestream2_get_bytes_left(&gbc) < 2)
     625           0 :                     return AVERROR_INVALIDDATA;
     626           0 :                 size = bytestream2_get_be16(&gbc);
     627           0 :                 if (bytestream2_get_bytes_left(&gbc) < size)
     628           0 :                     return AVERROR_INVALIDDATA;
     629           0 :                 bytestream2_skip(&gbc, size);
     630             :             }
     631           0 :             end = bytestream2_tell(&gbc);
     632             : 
     633           0 :             err = ff_h2645_packet_split(&priv->read_packet,
     634           0 :                                         frag->data + start, end - start,
     635             :                                         ctx->log_ctx, 1, 2, AV_CODEC_ID_HEVC, 1);
     636           0 :             if (err < 0) {
     637           0 :                 av_log(ctx->log_ctx, AV_LOG_ERROR, "Failed to split "
     638             :                        "HVCC array %d (%d NAL units of type %d).\n",
     639             :                        i, nb_nals, nal_unit_type);
     640           0 :                 return err;
     641             :             }
     642           0 :             err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
     643           0 :             if (err < 0)
     644           0 :                 return err;
     645             :         }
     646             : 
     647             :     } else {
     648             :         // Annex B, or later MP4 with already-known parameters.
     649             : 
     650       13386 :         err = ff_h2645_packet_split(&priv->read_packet,
     651        8924 :                                     frag->data, frag->data_size,
     652             :                                     ctx->log_ctx,
     653             :                                     priv->mp4, priv->nal_length_size,
     654             :                                     codec_id, 1);
     655        4462 :         if (err < 0)
     656           0 :             return err;
     657             : 
     658        4462 :         err = cbs_h2645_fragment_add_nals(ctx, frag, &priv->read_packet);
     659        4462 :         if (err < 0)
     660           0 :             return err;
     661             :     }
     662             : 
     663        4462 :     return 0;
     664             : }
     665             : 
     666             : #define cbs_h2645_replace_ps(h26n, ps_name, ps_var, id_element) \
     667             : static int cbs_h26 ## h26n ## _replace_ ## ps_var(CodedBitstreamContext *ctx, \
     668             :                                                   const H26 ## h26n ## Raw ## ps_name *ps_var)  \
     669             : { \
     670             :     CodedBitstreamH26 ## h26n ## Context *priv = ctx->priv_data; \
     671             :     unsigned int id = ps_var->id_element; \
     672             :     if (id > FF_ARRAY_ELEMS(priv->ps_var)) { \
     673             :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid " #ps_name \
     674             :                " id : %d.\n", id); \
     675             :         return AVERROR_INVALIDDATA; \
     676             :     } \
     677             :     av_freep(&priv->ps_var[id]); \
     678             :     priv->ps_var[id] = av_malloc(sizeof(*ps_var)); \
     679             :     if (!priv->ps_var[id]) \
     680             :         return AVERROR(ENOMEM); \
     681             :     memcpy(priv->ps_var[id], ps_var, sizeof(*ps_var)); \
     682             :     return 0; \
     683             : }
     684             : 
     685          76 : cbs_h2645_replace_ps(4, SPS, sps, seq_parameter_set_id)
     686        2360 : cbs_h2645_replace_ps(4, PPS, pps, pic_parameter_set_id)
     687          92 : cbs_h2645_replace_ps(5, VPS, vps, vps_video_parameter_set_id)
     688          96 : cbs_h2645_replace_ps(5, SPS, sps, sps_seq_parameter_set_id)
     689         546 : cbs_h2645_replace_ps(5, PPS, pps, pps_pic_parameter_set_id)
     690             : 
     691        6597 : static int cbs_h264_read_nal_unit(CodedBitstreamContext *ctx,
     692             :                                   CodedBitstreamUnit *unit)
     693             : {
     694             :     GetBitContext gbc;
     695             :     int err;
     696             : 
     697        6597 :     err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
     698        6597 :     if (err < 0)
     699           0 :         return err;
     700             : 
     701        6597 :     switch (unit->type) {
     702          38 :     case H264_NAL_SPS:
     703             :         {
     704             :             H264RawSPS *sps;
     705             : 
     706          38 :             sps = av_mallocz(sizeof(*sps));
     707          38 :             if (!sps)
     708           0 :                 return AVERROR(ENOMEM);
     709          38 :             err = cbs_h264_read_sps(ctx, &gbc, sps);
     710          38 :             if (err >= 0)
     711          38 :                 err = cbs_h264_replace_sps(ctx, sps);
     712          38 :             if (err < 0) {
     713           0 :                 av_free(sps);
     714           0 :                 return err;
     715             :             }
     716             : 
     717          38 :             unit->content = sps;
     718             :         }
     719          38 :         break;
     720             : 
     721           0 :     case H264_NAL_SPS_EXT:
     722             :         {
     723             :             H264RawSPSExtension *sps_ext;
     724             : 
     725           0 :             sps_ext = av_mallocz(sizeof(*sps_ext));
     726           0 :             if (!sps_ext)
     727           0 :                 return AVERROR(ENOMEM);
     728           0 :             err = cbs_h264_read_sps_extension(ctx, &gbc, sps_ext);
     729           0 :             if (err < 0) {
     730           0 :                 av_free(sps_ext);
     731           0 :                 return err;
     732             :             }
     733             : 
     734           0 :             unit->content = sps_ext;
     735             :         }
     736           0 :         break;
     737             : 
     738        1180 :     case H264_NAL_PPS:
     739             :         {
     740             :             H264RawPPS *pps;
     741             : 
     742        1180 :             pps = av_mallocz(sizeof(*pps));
     743        1180 :             if (!pps)
     744           0 :                 return AVERROR(ENOMEM);
     745        1180 :             err = cbs_h264_read_pps(ctx, &gbc, pps);
     746        1180 :             if (err >= 0)
     747        1180 :                 err = cbs_h264_replace_pps(ctx, pps);
     748        1180 :             if (err < 0) {
     749           0 :                 av_free(pps);
     750           0 :                 return err;
     751             :             }
     752             : 
     753        1180 :             unit->content = pps;
     754             :         }
     755        1180 :         break;
     756             : 
     757        4908 :     case H264_NAL_SLICE:
     758             :     case H264_NAL_IDR_SLICE:
     759             :     case H264_NAL_AUXILIARY_SLICE:
     760             :         {
     761             :             H264RawSlice *slice;
     762             :             int pos, len;
     763             : 
     764        4908 :             slice = av_mallocz(sizeof(*slice));
     765        4908 :             if (!slice)
     766           0 :                 return AVERROR(ENOMEM);
     767        4908 :             err = cbs_h264_read_slice_header(ctx, &gbc, &slice->header);
     768        4908 :             if (err < 0) {
     769           0 :                 av_free(slice);
     770           0 :                 return err;
     771             :             }
     772             : 
     773        4908 :             pos = get_bits_count(&gbc);
     774        4908 :             len = unit->data_size;
     775        4908 :             if (!unit->data[len - 1]) {
     776             :                 int z;
     777           0 :                 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
     778           0 :                 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
     779             :                        "from slice data.\n", z);
     780           0 :                 len -= z;
     781             :             }
     782             : 
     783        4908 :             slice->data_size = len - pos / 8;
     784        4908 :             slice->data = av_malloc(slice->data_size +
     785             :                                     AV_INPUT_BUFFER_PADDING_SIZE);
     786        4908 :             if (!slice->data) {
     787           0 :                 av_free(slice);
     788           0 :                 return AVERROR(ENOMEM);
     789             :             }
     790        9816 :             memcpy(slice->data,
     791        4908 :                    unit->data + pos / 8, slice->data_size);
     792        4908 :             memset(slice->data + slice->data_size, 0,
     793             :                    AV_INPUT_BUFFER_PADDING_SIZE);
     794        4908 :             slice->data_bit_start = pos % 8;
     795             : 
     796        4908 :             unit->content = slice;
     797             :         }
     798        4908 :         break;
     799             : 
     800         190 :     case H264_NAL_AUD:
     801             :         {
     802             :             H264RawAUD *aud;
     803             : 
     804         190 :             aud = av_mallocz(sizeof(*aud));
     805         190 :             if (!aud)
     806           0 :                 return AVERROR(ENOMEM);
     807         190 :             err = cbs_h264_read_aud(ctx, &gbc, aud);
     808         190 :             if (err < 0) {
     809           0 :                 av_free(aud);
     810           0 :                 return err;
     811             :             }
     812             : 
     813         190 :             unit->content = aud;
     814             :         }
     815         190 :         break;
     816             : 
     817         278 :     case H264_NAL_SEI:
     818             :         {
     819             :             H264RawSEI *sei;
     820             : 
     821         278 :             sei = av_mallocz(sizeof(*sei));
     822         278 :             if (!sei)
     823           0 :                 return AVERROR(ENOMEM);
     824         278 :             err = cbs_h264_read_sei(ctx, &gbc, sei);
     825         278 :             if (err < 0) {
     826           0 :                 cbs_h264_free_sei(sei);
     827           0 :                 av_free(sei);
     828           0 :                 return err;
     829             :             }
     830             : 
     831         278 :             unit->content = sei;
     832             :         }
     833         278 :         break;
     834             : 
     835           3 :     default:
     836           3 :         return AVERROR(ENOSYS);
     837             :     }
     838             : 
     839        6594 :     return 0;
     840             : }
     841             : 
     842        7760 : static int cbs_h265_read_nal_unit(CodedBitstreamContext *ctx,
     843             :                                   CodedBitstreamUnit *unit)
     844             : {
     845             :     GetBitContext gbc;
     846             :     int err;
     847             : 
     848        7760 :     err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
     849        7760 :     if (err < 0)
     850           0 :         return err;
     851             : 
     852        7760 :     switch (unit->type) {
     853          46 :     case HEVC_NAL_VPS:
     854             :         {
     855             :             H265RawVPS *vps;
     856             : 
     857          46 :             vps = av_mallocz(sizeof(*vps));
     858          46 :             if (!vps)
     859           0 :                 return AVERROR(ENOMEM);
     860          46 :             err = cbs_h265_read_vps(ctx, &gbc, vps);
     861          46 :             if (err >= 0)
     862          46 :                 err = cbs_h265_replace_vps(ctx, vps);
     863          46 :             if (err < 0) {
     864           0 :                 av_free(vps);
     865           0 :                 return err;
     866             :             }
     867             : 
     868          46 :             unit->content = vps;
     869             :         }
     870          46 :         break;
     871          48 :     case HEVC_NAL_SPS:
     872             :         {
     873             :             H265RawSPS *sps;
     874             : 
     875          48 :             sps = av_mallocz(sizeof(*sps));
     876          48 :             if (!sps)
     877           0 :                 return AVERROR(ENOMEM);
     878          48 :             err = cbs_h265_read_sps(ctx, &gbc, sps);
     879          48 :             if (err >= 0)
     880          48 :                 err = cbs_h265_replace_sps(ctx, sps);
     881          48 :             if (err < 0) {
     882           0 :                 av_free(sps);
     883           0 :                 return err;
     884             :             }
     885             : 
     886          48 :             unit->content = sps;
     887             :         }
     888          48 :         break;
     889             : 
     890         273 :     case HEVC_NAL_PPS:
     891             :         {
     892             :             H265RawPPS *pps;
     893             : 
     894         273 :             pps = av_mallocz(sizeof(*pps));
     895         273 :             if (!pps)
     896           0 :                 return AVERROR(ENOMEM);
     897         273 :             err = cbs_h265_read_pps(ctx, &gbc, pps);
     898         273 :             if (err >= 0)
     899         273 :                 err = cbs_h265_replace_pps(ctx, pps);
     900         273 :             if (err < 0) {
     901           0 :                 av_free(pps);
     902           0 :                 return err;
     903             :             }
     904             : 
     905         273 :             unit->content = pps;
     906             :         }
     907         273 :         break;
     908             : 
     909        5831 :     case HEVC_NAL_TRAIL_N:
     910             :     case HEVC_NAL_TRAIL_R:
     911             :     case HEVC_NAL_TSA_N:
     912             :     case HEVC_NAL_TSA_R:
     913             :     case HEVC_NAL_STSA_N:
     914             :     case HEVC_NAL_STSA_R:
     915             :     case HEVC_NAL_RADL_N:
     916             :     case HEVC_NAL_RADL_R:
     917             :     case HEVC_NAL_RASL_N:
     918             :     case HEVC_NAL_RASL_R:
     919             :     case HEVC_NAL_BLA_W_LP:
     920             :     case HEVC_NAL_BLA_W_RADL:
     921             :     case HEVC_NAL_BLA_N_LP:
     922             :     case HEVC_NAL_IDR_W_RADL:
     923             :     case HEVC_NAL_IDR_N_LP:
     924             :     case HEVC_NAL_CRA_NUT:
     925             :         {
     926             :             H265RawSlice *slice;
     927             :             int pos, len;
     928             : 
     929        5831 :             slice = av_mallocz(sizeof(*slice));
     930        5831 :             if (!slice)
     931           0 :                 return AVERROR(ENOMEM);
     932        5831 :             err = cbs_h265_read_slice_segment_header(ctx, &gbc, &slice->header);
     933        5831 :             if (err < 0) {
     934           0 :                 av_free(slice);
     935           0 :                 return err;
     936             :             }
     937             : 
     938        5831 :             pos = get_bits_count(&gbc);
     939        5831 :             len = unit->data_size;
     940        5831 :             if (!unit->data[len - 1]) {
     941             :                 int z;
     942           0 :                 for (z = 0; z < len && !unit->data[len - z - 1]; z++);
     943           0 :                 av_log(ctx->log_ctx, AV_LOG_DEBUG, "Deleted %d trailing zeroes "
     944             :                        "from slice data.\n", z);
     945           0 :                 len -= z;
     946             :             }
     947             : 
     948        5831 :             slice->data_size = len - pos / 8;
     949        5831 :             slice->data = av_malloc(slice->data_size +
     950             :                                     AV_INPUT_BUFFER_PADDING_SIZE);
     951        5831 :             if (!slice->data) {
     952           0 :                 av_free(slice);
     953           0 :                 return AVERROR(ENOMEM);
     954             :             }
     955       11662 :             memcpy(slice->data,
     956        5831 :                    unit->data + pos / 8, slice->data_size);
     957        5831 :             memset(slice->data + slice->data_size, 0,
     958             :                    AV_INPUT_BUFFER_PADDING_SIZE);
     959        5831 :             slice->data_bit_start = pos % 8;
     960             : 
     961        5831 :             unit->content = slice;
     962             :         }
     963        5831 :         break;
     964             : 
     965          60 :     case HEVC_NAL_AUD:
     966             :         {
     967             :             H265RawAUD *aud;
     968             : 
     969          60 :             aud = av_mallocz(sizeof(*aud));
     970          60 :             if (!aud)
     971           0 :                 return AVERROR(ENOMEM);
     972          60 :             err = cbs_h265_read_aud(ctx, &gbc, aud);
     973          60 :             if (err < 0) {
     974           0 :                 av_free(aud);
     975           0 :                 return err;
     976             :             }
     977             : 
     978          60 :             unit->content = aud;
     979             :         }
     980          60 :         break;
     981             : 
     982        1502 :     default:
     983        1502 :         return AVERROR(ENOSYS);
     984             :     }
     985             : 
     986        6258 :     return 0;
     987             : }
     988             : 
     989        6594 : static int cbs_h264_write_nal_unit(CodedBitstreamContext *ctx,
     990             :                                    CodedBitstreamUnit *unit,
     991             :                                    PutBitContext *pbc)
     992             : {
     993             :     int err;
     994             : 
     995        6594 :     switch (unit->type) {
     996          38 :     case H264_NAL_SPS:
     997             :         {
     998          38 :             H264RawSPS *sps = unit->content;
     999             : 
    1000          38 :             err = cbs_h264_write_sps(ctx, pbc, sps);
    1001          38 :             if (err < 0)
    1002           0 :                 return err;
    1003             : 
    1004          38 :             err = cbs_h264_replace_sps(ctx, sps);
    1005          38 :             if (err < 0)
    1006           0 :                 return err;
    1007             :         }
    1008          38 :         break;
    1009             : 
    1010           0 :     case H264_NAL_SPS_EXT:
    1011             :         {
    1012           0 :             H264RawSPSExtension *sps_ext = unit->content;
    1013             : 
    1014           0 :             err = cbs_h264_write_sps_extension(ctx, pbc, sps_ext);
    1015           0 :             if (err < 0)
    1016           0 :                 return err;
    1017             :         }
    1018           0 :         break;
    1019             : 
    1020        1180 :     case H264_NAL_PPS:
    1021             :         {
    1022        1180 :             H264RawPPS *pps = unit->content;
    1023             : 
    1024        1180 :             err = cbs_h264_write_pps(ctx, pbc, pps);
    1025        1180 :             if (err < 0)
    1026           0 :                 return err;
    1027             : 
    1028        1180 :             err = cbs_h264_replace_pps(ctx, pps);
    1029        1180 :             if (err < 0)
    1030           0 :                 return err;
    1031             :         }
    1032        1180 :         break;
    1033             : 
    1034        4908 :     case H264_NAL_SLICE:
    1035             :     case H264_NAL_IDR_SLICE:
    1036             :     case H264_NAL_AUXILIARY_SLICE:
    1037             :         {
    1038        4908 :             H264RawSlice *slice = unit->content;
    1039             :             GetBitContext gbc;
    1040             :             int bits_left, end, zeroes;
    1041             : 
    1042        4908 :             err = cbs_h264_write_slice_header(ctx, pbc, &slice->header);
    1043        4908 :             if (err < 0)
    1044           0 :                 return err;
    1045             : 
    1046        4908 :             if (slice->data) {
    1047        4908 :                 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
    1048           0 :                     return AVERROR(ENOSPC);
    1049             : 
    1050        4908 :                 init_get_bits(&gbc, slice->data, slice->data_size * 8);
    1051        4908 :                 skip_bits_long(&gbc, slice->data_bit_start);
    1052             : 
    1053             :                 // Copy in two-byte blocks, but stop before copying the
    1054             :                 // rbsp_stop_one_bit in the final byte.
    1055     3081090 :                 while (get_bits_left(&gbc) > 23)
    1056     3071274 :                     put_bits(pbc, 16, get_bits(&gbc, 16));
    1057             : 
    1058        4908 :                 bits_left = get_bits_left(&gbc);
    1059        4908 :                 end = get_bits(&gbc, bits_left);
    1060             : 
    1061             :                 // rbsp_stop_one_bit must be present here.
    1062        4908 :                 av_assert0(end);
    1063        4908 :                 zeroes = ff_ctz(end);
    1064        4908 :                 if (bits_left > zeroes + 1)
    1065        4844 :                     put_bits(pbc, bits_left - zeroes - 1,
    1066        4844 :                              end >> (zeroes + 1));
    1067        4908 :                 put_bits(pbc, 1, 1);
    1068       27032 :                 while (put_bits_count(pbc) % 8 != 0)
    1069       17216 :                     put_bits(pbc, 1, 0);
    1070             :             } else {
    1071             :                 // No slice data - that was just the header.
    1072             :                 // (Bitstream may be unaligned!)
    1073             :             }
    1074             :         }
    1075        4908 :         break;
    1076             : 
    1077         190 :     case H264_NAL_AUD:
    1078             :         {
    1079         190 :             err = cbs_h264_write_aud(ctx, pbc, unit->content);
    1080         190 :             if (err < 0)
    1081           0 :                 return err;
    1082             :         }
    1083         190 :         break;
    1084             : 
    1085         278 :     case H264_NAL_SEI:
    1086             :         {
    1087         278 :             err = cbs_h264_write_sei(ctx, pbc, unit->content);
    1088         278 :             if (err < 0)
    1089           0 :                 return err;
    1090             :         }
    1091         278 :         break;
    1092             : 
    1093           0 :     default:
    1094           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
    1095             :                "NAL unit type %"PRIu32".\n", unit->type);
    1096           0 :         return AVERROR_PATCHWELCOME;
    1097             :     }
    1098             : 
    1099        6594 :     return 0;
    1100             : }
    1101             : 
    1102        6258 : static int cbs_h265_write_nal_unit(CodedBitstreamContext *ctx,
    1103             :                                    CodedBitstreamUnit *unit,
    1104             :                                    PutBitContext *pbc)
    1105             : {
    1106             :     int err;
    1107             : 
    1108        6258 :     switch (unit->type) {
    1109          46 :     case HEVC_NAL_VPS:
    1110             :         {
    1111          46 :             H265RawVPS *vps = unit->content;
    1112             : 
    1113          46 :             err = cbs_h265_write_vps(ctx, pbc, vps);
    1114          46 :             if (err < 0)
    1115           0 :                 return err;
    1116             : 
    1117          46 :             err = cbs_h265_replace_vps(ctx, vps);
    1118          46 :             if (err < 0)
    1119           0 :                 return err;
    1120             :         }
    1121          46 :         break;
    1122             : 
    1123          48 :     case HEVC_NAL_SPS:
    1124             :         {
    1125          48 :             H265RawSPS *sps = unit->content;
    1126             : 
    1127          48 :             err = cbs_h265_write_sps(ctx, pbc, sps);
    1128          48 :             if (err < 0)
    1129           0 :                 return err;
    1130             : 
    1131          48 :             err = cbs_h265_replace_sps(ctx, sps);
    1132          48 :             if (err < 0)
    1133           0 :                 return err;
    1134             :         }
    1135          48 :         break;
    1136             : 
    1137         273 :     case HEVC_NAL_PPS:
    1138             :         {
    1139         273 :             H265RawPPS *pps = unit->content;
    1140             : 
    1141         273 :             err = cbs_h265_write_pps(ctx, pbc, pps);
    1142         273 :             if (err < 0)
    1143           0 :                 return err;
    1144             : 
    1145         273 :             err = cbs_h265_replace_pps(ctx, pps);
    1146         273 :             if (err < 0)
    1147           0 :                 return err;
    1148             :         }
    1149         273 :         break;
    1150             : 
    1151        5831 :     case HEVC_NAL_TRAIL_N:
    1152             :     case HEVC_NAL_TRAIL_R:
    1153             :     case HEVC_NAL_TSA_N:
    1154             :     case HEVC_NAL_TSA_R:
    1155             :     case HEVC_NAL_STSA_N:
    1156             :     case HEVC_NAL_STSA_R:
    1157             :     case HEVC_NAL_RADL_N:
    1158             :     case HEVC_NAL_RADL_R:
    1159             :     case HEVC_NAL_RASL_N:
    1160             :     case HEVC_NAL_RASL_R:
    1161             :     case HEVC_NAL_BLA_W_LP:
    1162             :     case HEVC_NAL_BLA_W_RADL:
    1163             :     case HEVC_NAL_BLA_N_LP:
    1164             :     case HEVC_NAL_IDR_W_RADL:
    1165             :     case HEVC_NAL_IDR_N_LP:
    1166             :     case HEVC_NAL_CRA_NUT:
    1167             :         {
    1168        5831 :             H265RawSlice *slice = unit->content;
    1169             :             GetBitContext gbc;
    1170             :             int bits_left, end, zeroes;
    1171             : 
    1172        5831 :             err = cbs_h265_write_slice_segment_header(ctx, pbc, &slice->header);
    1173        5831 :             if (err < 0)
    1174           0 :                 return err;
    1175             : 
    1176        5831 :             if (slice->data) {
    1177        5831 :                 if (slice->data_size * 8 + 8 > put_bits_left(pbc))
    1178           0 :                     return AVERROR(ENOSPC);
    1179             : 
    1180        5831 :                 init_get_bits(&gbc, slice->data, slice->data_size * 8);
    1181        5831 :                 skip_bits_long(&gbc, slice->data_bit_start);
    1182             : 
    1183             :                 // Copy in two-byte blocks, but stop before copying the
    1184             :                 // rbsp_stop_one_bit in the final byte.
    1185     2147986 :                 while (get_bits_left(&gbc) > 23)
    1186     2136324 :                     put_bits(pbc, 16, get_bits(&gbc, 16));
    1187             : 
    1188        5831 :                 bits_left = get_bits_left(&gbc);
    1189        5831 :                 end = get_bits(&gbc, bits_left);
    1190             : 
    1191             :                 // rbsp_stop_one_bit must be present here.
    1192        5831 :                 av_assert0(end);
    1193        5831 :                 zeroes = ff_ctz(end);
    1194        5831 :                 if (bits_left > zeroes + 1)
    1195        5492 :                     put_bits(pbc, bits_left - zeroes - 1,
    1196        5492 :                              end >> (zeroes + 1));
    1197        5831 :                 put_bits(pbc, 1, 1);
    1198       32103 :                 while (put_bits_count(pbc) % 8 != 0)
    1199       20441 :                     put_bits(pbc, 1, 0);
    1200             :             } else {
    1201             :                 // No slice data - that was just the header.
    1202             :             }
    1203             :         }
    1204        5831 :         break;
    1205             : 
    1206          60 :     case HEVC_NAL_AUD:
    1207             :         {
    1208          60 :             err = cbs_h265_write_aud(ctx, pbc, unit->content);
    1209          60 :             if (err < 0)
    1210           0 :                 return err;
    1211             :         }
    1212          60 :         break;
    1213             : 
    1214           0 :     default:
    1215           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for "
    1216             :                "NAL unit type %"PRIu32".\n", unit->type);
    1217           0 :         return AVERROR_PATCHWELCOME;
    1218             :     }
    1219             : 
    1220        6258 :     return 0;
    1221             : }
    1222             : 
    1223       12852 : static int cbs_h2645_write_nal_unit(CodedBitstreamContext *ctx,
    1224             :                                     CodedBitstreamUnit *unit)
    1225             : {
    1226       12852 :     CodedBitstreamH2645Context *priv = ctx->priv_data;
    1227       12852 :     enum AVCodecID codec_id = ctx->codec->codec_id;
    1228             :     PutBitContext pbc;
    1229             :     int err;
    1230             : 
    1231       12852 :     if (!priv->write_buffer) {
    1232             :         // Initial write buffer size is 1MB.
    1233          33 :         priv->write_buffer_size = 1024 * 1024;
    1234             : 
    1235          33 :     reallocate_and_try_again:
    1236          33 :         err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
    1237          33 :         if (err < 0) {
    1238           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
    1239             :                    "sufficiently large write buffer (last attempt "
    1240             :                    "%zu bytes).\n", priv->write_buffer_size);
    1241           0 :             return err;
    1242             :         }
    1243             :     }
    1244             : 
    1245       12852 :     init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
    1246             : 
    1247       12852 :     if (codec_id == AV_CODEC_ID_H264)
    1248        6594 :         err = cbs_h264_write_nal_unit(ctx, unit, &pbc);
    1249             :     else
    1250        6258 :         err = cbs_h265_write_nal_unit(ctx, unit, &pbc);
    1251             : 
    1252       12852 :     if (err == AVERROR(ENOSPC)) {
    1253             :         // Overflow.
    1254           0 :         priv->write_buffer_size *= 2;
    1255           0 :         goto reallocate_and_try_again;
    1256             :     }
    1257             :     // Overflow but we didn't notice.
    1258       12852 :     av_assert0(put_bits_count(&pbc) <= 8 * priv->write_buffer_size);
    1259             : 
    1260       12852 :     if (err < 0) {
    1261             :         // Write failed for some other reason.
    1262           0 :         return err;
    1263             :     }
    1264             : 
    1265       12852 :     if (put_bits_count(&pbc) % 8)
    1266           0 :         unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
    1267             :     else
    1268       12852 :         unit->data_bit_padding = 0;
    1269             : 
    1270       12852 :     unit->data_size = (put_bits_count(&pbc) + 7) / 8;
    1271       12852 :     flush_put_bits(&pbc);
    1272             : 
    1273       12852 :     err = av_reallocp(&unit->data, unit->data_size);
    1274       12852 :     if (err < 0)
    1275           0 :         return err;
    1276             : 
    1277       12852 :     memcpy(unit->data, priv->write_buffer, unit->data_size);
    1278             : 
    1279       12852 :     return 0;
    1280             : }
    1281             : 
    1282        4462 : static int cbs_h2645_assemble_fragment(CodedBitstreamContext *ctx,
    1283             :                                        CodedBitstreamFragment *frag)
    1284             : {
    1285             :     uint8_t *data;
    1286             :     size_t max_size, dp, sp;
    1287             :     int err, i, zero_run;
    1288             : 
    1289       18819 :     for (i = 0; i < frag->nb_units; i++) {
    1290             :         // Data should already all have been written when we get here.
    1291       14357 :         av_assert0(frag->units[i].data);
    1292             :     }
    1293             : 
    1294        4462 :     max_size = 0;
    1295       18819 :     for (i = 0; i < frag->nb_units; i++) {
    1296             :         // Start code + content with worst-case emulation prevention.
    1297       14357 :         max_size += 3 + frag->units[i].data_size * 3 / 2;
    1298             :     }
    1299             : 
    1300        4462 :     data = av_malloc(max_size);
    1301        4462 :     if (!data)
    1302           0 :         return AVERROR(ENOMEM);
    1303             : 
    1304        4462 :     dp = 0;
    1305       18819 :     for (i = 0; i < frag->nb_units; i++) {
    1306       14357 :         CodedBitstreamUnit *unit = &frag->units[i];
    1307             : 
    1308       14357 :         if (unit->data_bit_padding > 0) {
    1309           0 :             if (i < frag->nb_units - 1)
    1310           0 :                 av_log(ctx->log_ctx, AV_LOG_WARNING, "Probably invalid "
    1311             :                        "unaligned padding on non-final NAL unit.\n");
    1312             :             else
    1313           0 :                 frag->data_bit_padding = unit->data_bit_padding;
    1314             :         }
    1315             : 
    1316       20954 :         if ((ctx->codec->codec_id == AV_CODEC_ID_H264 &&
    1317       13156 :              (unit->type == H264_NAL_SPS ||
    1318       19698 :               unit->type == H264_NAL_PPS)) ||
    1319       20899 :             (ctx->codec->codec_id == AV_CODEC_ID_HEVC &&
    1320       15474 :              (unit->type == HEVC_NAL_VPS ||
    1321       15380 :               unit->type == HEVC_NAL_SPS ||
    1322       20438 :               unit->type == HEVC_NAL_PPS)) ||
    1323             :             i == 0 /* (Assume this is the start of an access unit.) */) {
    1324             :             // zero_byte
    1325        4979 :             data[dp++] = 0;
    1326             :         }
    1327             :         // start_code_prefix_one_3bytes
    1328       14357 :         data[dp++] = 0;
    1329       14357 :         data[dp++] = 0;
    1330       14357 :         data[dp++] = 1;
    1331             : 
    1332       14357 :         zero_run = 0;
    1333    10660461 :         for (sp = 0; sp < unit->data_size; sp++) {
    1334    10646104 :             if (zero_run < 2) {
    1335    10642180 :                 if (unit->data[sp] == 0)
    1336      105738 :                     ++zero_run;
    1337             :                 else
    1338    10536442 :                     zero_run = 0;
    1339             :             } else {
    1340        3924 :                 if ((unit->data[sp] & ~3) == 0) {
    1341             :                     // emulation_prevention_three_byte
    1342         695 :                     data[dp++] = 3;
    1343             :                 }
    1344        3924 :                 zero_run = unit->data[sp] == 0;
    1345             :             }
    1346    10646104 :             data[dp++] = unit->data[sp];
    1347             :         }
    1348             :     }
    1349             : 
    1350        4462 :     av_assert0(dp <= max_size);
    1351        4462 :     err = av_reallocp(&data, dp);
    1352        4462 :     if (err)
    1353           0 :         return err;
    1354             : 
    1355        4462 :     frag->data = data;
    1356        4462 :     frag->data_size = dp;
    1357             : 
    1358        4462 :     return 0;
    1359             : }
    1360             : 
    1361          14 : static void cbs_h264_close(CodedBitstreamContext *ctx)
    1362             : {
    1363          14 :     CodedBitstreamH264Context *h264 = ctx->priv_data;
    1364             :     int i;
    1365             : 
    1366          14 :     ff_h2645_packet_uninit(&h264->common.read_packet);
    1367             : 
    1368          14 :     av_freep(&h264->common.write_buffer);
    1369             : 
    1370         462 :     for (i = 0; i < FF_ARRAY_ELEMS(h264->sps); i++)
    1371         448 :         av_freep(&h264->sps[i]);
    1372        3598 :     for (i = 0; i < FF_ARRAY_ELEMS(h264->pps); i++)
    1373        3584 :         av_freep(&h264->pps[i]);
    1374          14 : }
    1375             : 
    1376          19 : static void cbs_h265_close(CodedBitstreamContext *ctx)
    1377             : {
    1378          19 :     CodedBitstreamH265Context *h265 = ctx->priv_data;
    1379             :     int i;
    1380             : 
    1381          19 :     ff_h2645_packet_uninit(&h265->common.read_packet);
    1382             : 
    1383          19 :     av_freep(&h265->common.write_buffer);
    1384             : 
    1385         323 :     for (i = 0; i < FF_ARRAY_ELEMS(h265->vps); i++)
    1386         304 :         av_freep(&h265->vps[i]);
    1387         323 :     for (i = 0; i < FF_ARRAY_ELEMS(h265->sps); i++)
    1388         304 :         av_freep(&h265->sps[i]);
    1389        1235 :     for (i = 0; i < FF_ARRAY_ELEMS(h265->pps); i++)
    1390        1216 :         av_freep(&h265->pps[i]);
    1391          19 : }
    1392             : 
    1393             : const CodedBitstreamType ff_cbs_type_h264 = {
    1394             :     .codec_id          = AV_CODEC_ID_H264,
    1395             : 
    1396             :     .priv_data_size    = sizeof(CodedBitstreamH264Context),
    1397             : 
    1398             :     .split_fragment    = &cbs_h2645_split_fragment,
    1399             :     .read_unit         = &cbs_h264_read_nal_unit,
    1400             :     .write_unit        = &cbs_h2645_write_nal_unit,
    1401             :     .assemble_fragment = &cbs_h2645_assemble_fragment,
    1402             : 
    1403             :     .free_unit         = &cbs_h264_free_nal_unit,
    1404             :     .close             = &cbs_h264_close,
    1405             : };
    1406             : 
    1407             : const CodedBitstreamType ff_cbs_type_h265 = {
    1408             :     .codec_id          = AV_CODEC_ID_HEVC,
    1409             : 
    1410             :     .priv_data_size    = sizeof(CodedBitstreamH265Context),
    1411             : 
    1412             :     .split_fragment    = &cbs_h2645_split_fragment,
    1413             :     .read_unit         = &cbs_h265_read_nal_unit,
    1414             :     .write_unit        = &cbs_h2645_write_nal_unit,
    1415             :     .assemble_fragment = &cbs_h2645_assemble_fragment,
    1416             : 
    1417             :     .free_unit         = &cbs_h265_free_nal_unit,
    1418             :     .close             = &cbs_h265_close,
    1419             : };

Generated by: LCOV version 1.13