LCOV - code coverage report
Current view: top level - libavcodec - cbs_mpeg2.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 119 140 85.0 %
Date: 2018-05-20 11:54:08 Functions: 9 9 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/avassert.h"
      20             : 
      21             : #include "cbs.h"
      22             : #include "cbs_internal.h"
      23             : #include "cbs_mpeg2.h"
      24             : #include "internal.h"
      25             : 
      26             : 
      27             : #define HEADER(name) do { \
      28             :         ff_cbs_trace_header(ctx, name); \
      29             :     } while (0)
      30             : 
      31             : #define CHECK(call) do { \
      32             :         err = (call); \
      33             :         if (err < 0) \
      34             :             return err; \
      35             :     } while (0)
      36             : 
      37             : #define FUNC_NAME(rw, codec, name) cbs_ ## codec ## _ ## rw ## _ ## name
      38             : #define FUNC_MPEG2(rw, name) FUNC_NAME(rw, mpeg2, name)
      39             : #define FUNC(name) FUNC_MPEG2(READWRITE, name)
      40             : 
      41             : #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
      42             : 
      43             : #define ui(width, name) \
      44             :         xui(width, name, current->name, 0)
      45             : #define uis(width, name, subs, ...) \
      46             :         xui(width, name, current->name, subs, __VA_ARGS__)
      47             : 
      48             : 
      49             : #define READ
      50             : #define READWRITE read
      51             : #define RWContext GetBitContext
      52             : 
      53             : #define xui(width, name, var, subs, ...) do { \
      54             :         uint32_t value = 0; \
      55             :         CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
      56             :                                    SUBSCRIPTS(subs, __VA_ARGS__), \
      57             :                                    &value, 0, (1 << width) - 1)); \
      58             :         var = value; \
      59             :     } while (0)
      60             : 
      61             : #define marker_bit() do { \
      62             :         av_unused uint32_t one; \
      63             :         CHECK(ff_cbs_read_unsigned(ctx, rw, 1, "marker_bit", NULL, &one, 1, 1)); \
      64             :     } while (0)
      65             : 
      66             : #define nextbits(width, compare, var) \
      67             :     (get_bits_left(rw) >= width && \
      68             :      (var = show_bits(rw, width)) == (compare))
      69             : 
      70             : #include "cbs_mpeg2_syntax_template.c"
      71             : 
      72             : #undef READ
      73             : #undef READWRITE
      74             : #undef RWContext
      75             : #undef xui
      76             : #undef marker_bit
      77             : #undef nextbits
      78             : 
      79             : 
      80             : #define WRITE
      81             : #define READWRITE write
      82             : #define RWContext PutBitContext
      83             : 
      84             : #define xui(width, name, var, subs, ...) do { \
      85             :         CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
      86             :                                     SUBSCRIPTS(subs, __VA_ARGS__), \
      87             :                                     var, 0, (1 << width) - 1)); \
      88             :     } while (0)
      89             : 
      90             : #define marker_bit() do { \
      91             :         CHECK(ff_cbs_write_unsigned(ctx, rw, 1, "marker_bit", NULL, 1, 1, 1)); \
      92             :     } while (0)
      93             : 
      94             : #define nextbits(width, compare, var) (var)
      95             : 
      96             : #include "cbs_mpeg2_syntax_template.c"
      97             : 
      98             : #undef READ
      99             : #undef READWRITE
     100             : #undef RWContext
     101             : #undef xui
     102             : #undef marker_bit
     103             : #undef nextbits
     104             : 
     105             : 
     106          14 : static void cbs_mpeg2_free_user_data(void *unit, uint8_t *content)
     107             : {
     108          14 :     MPEG2RawUserData *user = (MPEG2RawUserData*)content;
     109          14 :     av_buffer_unref(&user->user_data_ref);
     110          14 :     av_freep(&content);
     111          14 : }
     112             : 
     113        8746 : static void cbs_mpeg2_free_slice(void *unit, uint8_t *content)
     114             : {
     115        8746 :     MPEG2RawSlice *slice = (MPEG2RawSlice*)content;
     116        8746 :     av_buffer_unref(&slice->header.extra_information_ref);
     117        8746 :     av_buffer_unref(&slice->data_ref);
     118        8746 :     av_freep(&content);
     119        8746 : }
     120             : 
     121          18 : static int cbs_mpeg2_split_fragment(CodedBitstreamContext *ctx,
     122             :                                     CodedBitstreamFragment *frag,
     123             :                                     int header)
     124             : {
     125             :     const uint8_t *start, *end;
     126             :     uint8_t *unit_data;
     127          18 :     uint32_t start_code = -1, next_start_code = -1;
     128             :     size_t unit_size;
     129             :     int err, i, unit_type;
     130             : 
     131          18 :     start = avpriv_find_start_code(frag->data, frag->data + frag->data_size,
     132             :                                    &start_code);
     133        8840 :     for (i = 0;; i++) {
     134       17662 :         end = avpriv_find_start_code(start, frag->data + frag->data_size,
     135             :                                      &next_start_code);
     136             : 
     137        8840 :         unit_type = start_code & 0xff;
     138             : 
     139             :         // The start and end pointers point at to the byte following the
     140             :         // start_code_identifier in the start code that they found.
     141        8840 :         if (end == frag->data + frag->data_size) {
     142             :             // We didn't find a start code, so this is the final unit.
     143          18 :             unit_size = end - (start - 1);
     144             :         } else {
     145             :             // Unit runs from start to the beginning of the start code
     146             :             // pointed to by end (including any padding zeroes).
     147        8822 :             unit_size = (end - 4) - (start - 1);
     148             :         }
     149             : 
     150        8840 :         unit_data = (uint8_t *)start - 1;
     151             : 
     152        8840 :         err = ff_cbs_insert_unit_data(ctx, frag, i, unit_type,
     153             :                                       unit_data, unit_size, frag->data_ref);
     154        8840 :         if (err < 0)
     155           0 :             return err;
     156             : 
     157        8840 :         if (end == frag->data + frag->data_size)
     158          18 :             break;
     159             : 
     160        8822 :         start_code = next_start_code;
     161        8822 :         start = end;
     162             :     }
     163             : 
     164          18 :     return 0;
     165             : }
     166             : 
     167        8840 : static int cbs_mpeg2_read_unit(CodedBitstreamContext *ctx,
     168             :                                CodedBitstreamUnit *unit)
     169             : {
     170             :     GetBitContext gbc;
     171             :     int err;
     172             : 
     173        8840 :     err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
     174        8840 :     if (err < 0)
     175           0 :         return err;
     176             : 
     177       17586 :     if (MPEG2_START_IS_SLICE(unit->type)) {
     178             :         MPEG2RawSlice *slice;
     179             :         int pos, len;
     180             : 
     181        8746 :         err = ff_cbs_alloc_unit_content(ctx, unit, sizeof(*slice),
     182             :                                         &cbs_mpeg2_free_slice);
     183        8746 :         if (err < 0)
     184           0 :             return err;
     185        8746 :         slice = unit->content;
     186             : 
     187        8746 :         err = cbs_mpeg2_read_slice_header(ctx, &gbc, &slice->header);
     188        8746 :         if (err < 0)
     189           0 :             return err;
     190             : 
     191        8746 :         pos = get_bits_count(&gbc);
     192        8746 :         len = unit->data_size;
     193             : 
     194        8746 :         slice->data_size = len - pos / 8;
     195        8746 :         slice->data_ref  = av_buffer_ref(unit->data_ref);
     196        8746 :         if (!slice->data_ref)
     197           0 :             return AVERROR(ENOMEM);
     198        8746 :         slice->data = unit->data + pos / 8;
     199             : 
     200        8746 :         slice->data_bit_start = pos % 8;
     201             : 
     202             :     } else {
     203          94 :         switch (unit->type) {
     204             : #define START(start_code, type, read_func, free_func) \
     205             :         case start_code: \
     206             :             { \
     207             :                 type *header; \
     208             :                 err = ff_cbs_alloc_unit_content(ctx, unit, \
     209             :                                                 sizeof(*header), free_func); \
     210             :                 if (err < 0) \
     211             :                     return err; \
     212             :                 header = unit->content; \
     213             :                 err = cbs_mpeg2_read_ ## read_func(ctx, &gbc, header); \
     214             :                 if (err < 0) \
     215             :                     return err; \
     216             :             } \
     217             :             break;
     218          19 :             START(0x00, MPEG2RawPictureHeader,  picture_header,  NULL);
     219          14 :             START(0xb2, MPEG2RawUserData,       user_data,
     220             :                                             &cbs_mpeg2_free_user_data);
     221           6 :             START(0xb3, MPEG2RawSequenceHeader, sequence_header, NULL);
     222          53 :             START(0xb5, MPEG2RawExtensionData,  extension_data,  NULL);
     223           2 :             START(0xb8, MPEG2RawGroupOfPicturesHeader,
     224             :                                        group_of_pictures_header, NULL);
     225             : #undef START
     226           0 :         default:
     227           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Unknown start code %02"PRIx32".\n",
     228             :                    unit->type);
     229           0 :             return AVERROR_INVALIDDATA;
     230             :         }
     231             :     }
     232             : 
     233        8840 :     return 0;
     234             : }
     235             : 
     236          94 : static int cbs_mpeg2_write_header(CodedBitstreamContext *ctx,
     237             :                                   CodedBitstreamUnit *unit,
     238             :                                   PutBitContext *pbc)
     239             : {
     240             :     int err;
     241             : 
     242          94 :     switch (unit->type) {
     243             : #define START(start_code, type, func) \
     244             :     case start_code: \
     245             :         err = cbs_mpeg2_write_ ## func(ctx, pbc, unit->content); \
     246             :         break;
     247          19 :         START(0x00, MPEG2RawPictureHeader,  picture_header);
     248          14 :         START(0xb2, MPEG2RawUserData,       user_data);
     249           6 :         START(0xb3, MPEG2RawSequenceHeader, sequence_header);
     250          53 :         START(0xb5, MPEG2RawExtensionData,  extension_data);
     251           2 :         START(0xb8, MPEG2RawGroupOfPicturesHeader, group_of_pictures_header);
     252             : #undef START
     253           0 :     default:
     254           0 :         av_log(ctx->log_ctx, AV_LOG_ERROR, "Write unimplemented for start "
     255             :                "code %02"PRIx32".\n", unit->type);
     256           0 :         return AVERROR_PATCHWELCOME;
     257             :     }
     258             : 
     259          94 :     return err;
     260             : }
     261             : 
     262        8746 : static int cbs_mpeg2_write_slice(CodedBitstreamContext *ctx,
     263             :                                  CodedBitstreamUnit *unit,
     264             :                                  PutBitContext *pbc)
     265             : {
     266        8746 :     MPEG2RawSlice *slice = unit->content;
     267             :     GetBitContext gbc;
     268             :     size_t bits_left;
     269             :     int err;
     270             : 
     271        8746 :     err = cbs_mpeg2_write_slice_header(ctx, pbc, &slice->header);
     272        8746 :     if (err < 0)
     273           0 :         return err;
     274             : 
     275        8746 :     if (slice->data) {
     276        8746 :         if (slice->data_size * 8 + 8 > put_bits_left(pbc))
     277           0 :             return AVERROR(ENOSPC);
     278             : 
     279        8746 :         init_get_bits(&gbc, slice->data, slice->data_size * 8);
     280        8746 :         skip_bits_long(&gbc, slice->data_bit_start);
     281             : 
     282      678746 :         while (get_bits_left(&gbc) > 15)
     283      661254 :             put_bits(pbc, 16, get_bits(&gbc, 16));
     284             : 
     285        8746 :         bits_left = get_bits_left(&gbc);
     286        8746 :         put_bits(pbc, bits_left, get_bits(&gbc, bits_left));
     287             : 
     288             :         // Align with zeroes.
     289       17492 :         while (put_bits_count(pbc) % 8 != 0)
     290           0 :             put_bits(pbc, 1, 0);
     291             :     }
     292             : 
     293        8746 :     return 0;
     294             : }
     295             : 
     296        8840 : static int cbs_mpeg2_write_unit(CodedBitstreamContext *ctx,
     297             :                                 CodedBitstreamUnit *unit)
     298             : {
     299        8840 :     CodedBitstreamMPEG2Context *priv = ctx->priv_data;
     300             :     PutBitContext pbc;
     301             :     int err;
     302             : 
     303        8840 :     if (!priv->write_buffer) {
     304             :         // Initial write buffer size is 1MB.
     305           3 :         priv->write_buffer_size = 1024 * 1024;
     306             : 
     307           3 :     reallocate_and_try_again:
     308           3 :         err = av_reallocp(&priv->write_buffer, priv->write_buffer_size);
     309           3 :         if (err < 0) {
     310           0 :             av_log(ctx->log_ctx, AV_LOG_ERROR, "Unable to allocate a "
     311             :                    "sufficiently large write buffer (last attempt "
     312             :                    "%"SIZE_SPECIFIER" bytes).\n", priv->write_buffer_size);
     313           0 :             return err;
     314             :         }
     315             :     }
     316             : 
     317        8840 :     init_put_bits(&pbc, priv->write_buffer, priv->write_buffer_size);
     318             : 
     319        8840 :     if (unit->type >= 0x01 && unit->type <= 0xaf)
     320        8746 :         err = cbs_mpeg2_write_slice(ctx, unit, &pbc);
     321             :     else
     322          94 :         err = cbs_mpeg2_write_header(ctx, unit, &pbc);
     323             : 
     324        8840 :     if (err == AVERROR(ENOSPC)) {
     325             :         // Overflow.
     326           0 :         priv->write_buffer_size *= 2;
     327           0 :         goto reallocate_and_try_again;
     328             :     }
     329        8840 :     if (err < 0) {
     330             :         // Write failed for some other reason.
     331           0 :         return err;
     332             :     }
     333             : 
     334        8840 :     if (put_bits_count(&pbc) % 8)
     335          52 :         unit->data_bit_padding = 8 - put_bits_count(&pbc) % 8;
     336             :     else
     337        8788 :         unit->data_bit_padding = 0;
     338             : 
     339        8840 :     unit->data_size = (put_bits_count(&pbc) + 7) / 8;
     340        8840 :     flush_put_bits(&pbc);
     341             : 
     342        8840 :     err = ff_cbs_alloc_unit_data(ctx, unit, unit->data_size);
     343        8840 :     if (err < 0)
     344           0 :         return err;
     345             : 
     346        8840 :     memcpy(unit->data, priv->write_buffer, unit->data_size);
     347             : 
     348        8840 :     return 0;
     349             : }
     350             : 
     351          18 : static int cbs_mpeg2_assemble_fragment(CodedBitstreamContext *ctx,
     352             :                                        CodedBitstreamFragment *frag)
     353             : {
     354             :     uint8_t *data;
     355             :     size_t size, dp;
     356             :     int i;
     357             : 
     358          18 :     size = 0;
     359        8858 :     for (i = 0; i < frag->nb_units; i++)
     360        8840 :         size += 3 + frag->units[i].data_size;
     361             : 
     362          18 :     frag->data_ref = av_buffer_alloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
     363          18 :     if (!frag->data_ref)
     364           0 :         return AVERROR(ENOMEM);
     365          18 :     data = frag->data_ref->data;
     366             : 
     367          18 :     dp = 0;
     368        8858 :     for (i = 0; i < frag->nb_units; i++) {
     369        8840 :         CodedBitstreamUnit *unit = &frag->units[i];
     370             : 
     371        8840 :         data[dp++] = 0;
     372        8840 :         data[dp++] = 0;
     373        8840 :         data[dp++] = 1;
     374             : 
     375        8840 :         memcpy(data + dp, unit->data, unit->data_size);
     376        8840 :         dp += unit->data_size;
     377             :     }
     378             : 
     379          18 :     av_assert0(dp == size);
     380             : 
     381          18 :     memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
     382          18 :     frag->data      = data;
     383          18 :     frag->data_size = size;
     384             : 
     385          18 :     return 0;
     386             : }
     387             : 
     388           3 : static void cbs_mpeg2_close(CodedBitstreamContext *ctx)
     389             : {
     390           3 :     CodedBitstreamMPEG2Context *priv = ctx->priv_data;
     391             : 
     392           3 :     av_freep(&priv->write_buffer);
     393           3 : }
     394             : 
     395             : const CodedBitstreamType ff_cbs_type_mpeg2 = {
     396             :     .codec_id          = AV_CODEC_ID_MPEG2VIDEO,
     397             : 
     398             :     .priv_data_size    = sizeof(CodedBitstreamMPEG2Context),
     399             : 
     400             :     .split_fragment    = &cbs_mpeg2_split_fragment,
     401             :     .read_unit         = &cbs_mpeg2_read_unit,
     402             :     .write_unit        = &cbs_mpeg2_write_unit,
     403             :     .assemble_fragment = &cbs_mpeg2_assemble_fragment,
     404             : 
     405             :     .close             = &cbs_mpeg2_close,
     406             : };

Generated by: LCOV version 1.13