LCOV - code coverage report
Current view: top level - libavcodec - cbs_h2645.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 461 736 62.6 %
Date: 2018-05-20 11:54:08 Functions: 30 32 93.8 %

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

Generated by: LCOV version 1.13