LCOV - code coverage report
Current view: top level - libavcodec - vc1_parser.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 118 140 84.3 %
Date: 2017-12-14 01:15:32 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /*
       2             :  * VC-1 and WMV3 parser
       3             :  * Copyright (c) 2006-2007 Konstantin Shishkov
       4             :  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
       5             :  *
       6             :  * This file is part of FFmpeg.
       7             :  *
       8             :  * FFmpeg is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public
      10             :  * License as published by the Free Software Foundation; either
      11             :  * version 2.1 of the License, or (at your option) any later version.
      12             :  *
      13             :  * FFmpeg is distributed in the hope that it will be useful,
      14             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public
      19             :  * License along with FFmpeg; if not, write to the Free Software
      20             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      21             :  */
      22             : 
      23             : /**
      24             :  * @file
      25             :  * VC-1 and WMV3 parser
      26             :  */
      27             : 
      28             : #include "libavutil/attributes.h"
      29             : #include "parser.h"
      30             : #include "vc1.h"
      31             : #include "get_bits.h"
      32             : #include "internal.h"
      33             : 
      34             : /** The maximum number of bytes of a sequence, entry point or
      35             :  *  frame header whose values we pay any attention to */
      36             : #define UNESCAPED_THRESHOLD 37
      37             : 
      38             : /** The maximum number of bytes of a sequence, entry point or
      39             :  *  frame header which must be valid memory (because they are
      40             :  *  used to update the bitstream cache in skip_bits() calls)
      41             :  */
      42             : #define UNESCAPED_LIMIT 144
      43             : 
      44             : typedef enum {
      45             :     NO_MATCH,
      46             :     ONE_ZERO,
      47             :     TWO_ZEROS,
      48             :     ONE
      49             : } VC1ParseSearchState;
      50             : 
      51             : typedef struct VC1ParseContext {
      52             :     ParseContext pc;
      53             :     VC1Context v;
      54             :     uint8_t prev_start_code;
      55             :     size_t bytes_to_skip;
      56             :     uint8_t unesc_buffer[UNESCAPED_LIMIT];
      57             :     size_t unesc_index;
      58             :     VC1ParseSearchState search_state;
      59             : } VC1ParseContext;
      60             : 
      61         971 : static void vc1_extract_header(AVCodecParserContext *s, AVCodecContext *avctx,
      62             :                                const uint8_t *buf, int buf_size)
      63             : {
      64             :     /* Parse the header we just finished unescaping */
      65         971 :     VC1ParseContext *vpc = s->priv_data;
      66             :     GetBitContext gb;
      67             :     int ret;
      68         971 :     vpc->v.s.avctx = avctx;
      69         971 :     vpc->v.parse_only = 1;
      70         971 :     init_get_bits(&gb, buf, buf_size * 8);
      71         971 :     switch (vpc->prev_start_code) {
      72           9 :     case VC1_CODE_SEQHDR & 0xFF:
      73           9 :         ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
      74           9 :         break;
      75          14 :     case VC1_CODE_ENTRYPOINT & 0xFF:
      76          14 :         ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
      77          14 :         break;
      78         305 :     case VC1_CODE_FRAME & 0xFF:
      79         305 :         if(vpc->v.profile < PROFILE_ADVANCED)
      80           0 :             ret = ff_vc1_parse_frame_header    (&vpc->v, &gb);
      81             :         else
      82         305 :             ret = ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
      83             : 
      84         305 :         if (ret < 0)
      85           0 :             break;
      86             : 
      87             :         /* keep AV_PICTURE_TYPE_BI internal to VC1 */
      88         305 :         if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
      89           0 :             s->pict_type = AV_PICTURE_TYPE_B;
      90             :         else
      91         305 :             s->pict_type = vpc->v.s.pict_type;
      92             : 
      93         305 :         if (avctx->ticks_per_frame > 1){
      94             :             // process pulldown flags
      95         138 :             s->repeat_pict = 1;
      96             :             // Pulldown flags are only valid when 'broadcast' has been set.
      97             :             // So ticks_per_frame will be 2
      98         138 :             if (vpc->v.rff){
      99             :                 // repeat field
     100           0 :                 s->repeat_pict = 2;
     101         138 :             }else if (vpc->v.rptfrm){
     102             :                 // repeat frames
     103           0 :                 s->repeat_pict = vpc->v.rptfrm * 2 + 1;
     104             :             }
     105             :         }else{
     106         167 :             s->repeat_pict = 0;
     107             :         }
     108             : 
     109         305 :         if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
     110          44 :             s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
     111             :         else
     112         261 :             s->field_order = AV_FIELD_PROGRESSIVE;
     113             : 
     114         305 :         break;
     115             :     }
     116         971 :     if (avctx->framerate.num)
     117         971 :         avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1}));
     118        1942 :     s->format = vpc->v.chromaformat == 1 ? AV_PIX_FMT_YUV420P
     119         971 :                                          : AV_PIX_FMT_NONE;
     120         971 :     if (avctx->width && avctx->height) {
     121         959 :         s->width        = avctx->width;
     122         959 :         s->height       = avctx->height;
     123         959 :         s->coded_width  = FFALIGN(avctx->coded_width,  16);
     124         959 :         s->coded_height = FFALIGN(avctx->coded_height, 16);
     125             :     }
     126         971 : }
     127             : 
     128        4746 : static int vc1_parse(AVCodecParserContext *s,
     129             :                            AVCodecContext *avctx,
     130             :                            const uint8_t **poutbuf, int *poutbuf_size,
     131             :                            const uint8_t *buf, int buf_size)
     132             : {
     133             :     /* Here we do the searching for frame boundaries and headers at
     134             :      * the same time. Only a minimal amount at the start of each
     135             :      * header is unescaped. */
     136        4746 :     VC1ParseContext *vpc = s->priv_data;
     137        4746 :     int pic_found = vpc->pc.frame_start_found;
     138        4746 :     uint8_t *unesc_buffer = vpc->unesc_buffer;
     139        4746 :     size_t unesc_index = vpc->unesc_index;
     140        4746 :     VC1ParseSearchState search_state = vpc->search_state;
     141        4746 :     int start_code_found = 0;
     142        4746 :     int next = END_NOT_FOUND;
     143        4746 :     int i = vpc->bytes_to_skip;
     144             : 
     145        4746 :     if (pic_found && buf_size == 0) {
     146             :         /* EOF considered as end of frame */
     147          14 :         memset(unesc_buffer + unesc_index, 0, UNESCAPED_THRESHOLD - unesc_index);
     148          14 :         vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
     149          14 :         next = 0;
     150             :     }
     151       14592 :     while (i < buf_size) {
     152             :         uint8_t b;
     153        5393 :         start_code_found = 0;
     154       43681 :         while (i < buf_size && unesc_index < UNESCAPED_THRESHOLD) {
     155       32986 :             b = buf[i++];
     156       32986 :             unesc_buffer[unesc_index++] = b;
     157       32986 :             if (search_state <= ONE_ZERO)
     158       32765 :                 search_state = b ? NO_MATCH : search_state + 1;
     159         221 :             else if (search_state == TWO_ZEROS) {
     160         130 :                 if (b == 1)
     161          91 :                     search_state = ONE;
     162          39 :                 else if (b > 1) {
     163          39 :                     if (b == 3)
     164          22 :                         unesc_index--; // swallow emulation prevention byte
     165          39 :                     search_state = NO_MATCH;
     166             :                 }
     167             :             }
     168             :             else { // search_state == ONE
     169             :                 // Header unescaping terminates early due to detection of next start code
     170          91 :                 search_state = NO_MATCH;
     171          91 :                 start_code_found = 1;
     172          91 :                 break;
     173             :             }
     174             :         }
     175        5393 :         if ((s->flags & PARSER_FLAG_COMPLETE_FRAMES) &&
     176           0 :                 unesc_index >= UNESCAPED_THRESHOLD &&
     177           0 :                 vpc->prev_start_code == (VC1_CODE_FRAME & 0xFF))
     178             :         {
     179             :             // No need to keep scanning the rest of the buffer for
     180             :             // start codes if we know it contains a complete frame and
     181             :             // we've already unescaped all we need of the frame header
     182           0 :             vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
     183           0 :             break;
     184             :         }
     185        5393 :         if (unesc_index >= UNESCAPED_THRESHOLD && !start_code_found) {
     186       62552 :             while (i < buf_size) {
     187       52860 :                 if (search_state == NO_MATCH) {
     188       27593 :                     i += vpc->v.vc1dsp.startcode_find_candidate(buf + i, buf_size - i);
     189       27593 :                     if (i < buf_size) {
     190       23222 :                         search_state = ONE_ZERO;
     191             :                     }
     192       27593 :                     i++;
     193             :                 } else {
     194       25267 :                     b = buf[i++];
     195       25267 :                     if (search_state == ONE_ZERO)
     196       23231 :                         search_state = b ? NO_MATCH : TWO_ZEROS;
     197        2036 :                     else if (search_state == TWO_ZEROS) {
     198        1170 :                         if (b >= 1)
     199        1170 :                             search_state = b == 1 ? ONE : NO_MATCH;
     200             :                     }
     201             :                     else { // search_state == ONE
     202         866 :                         search_state = NO_MATCH;
     203         866 :                         start_code_found = 1;
     204         866 :                         break;
     205             :                     }
     206             :                 }
     207             :             }
     208             :         }
     209        5393 :         if (start_code_found) {
     210         957 :             vc1_extract_header(s, avctx, unesc_buffer, unesc_index);
     211             : 
     212         957 :             vpc->prev_start_code = b;
     213         957 :             unesc_index = 0;
     214             : 
     215         957 :             if (!(s->flags & PARSER_FLAG_COMPLETE_FRAMES)) {
     216         957 :                 if (!pic_found && (b == (VC1_CODE_FRAME & 0xFF) || b == (VC1_CODE_FIELD & 0xFF))) {
     217           9 :                     pic_found = 1;
     218             :                 }
     219         948 :                 else if (pic_found && b != (VC1_CODE_FIELD & 0xFF) && b != (VC1_CODE_SLICE & 0xFF)) {
     220         293 :                     next = i - 4;
     221         293 :                     pic_found = b == (VC1_CODE_FRAME & 0xFF);
     222         293 :                     break;
     223             :                 }
     224             :             }
     225             :         }
     226             :     }
     227             : 
     228        4746 :     vpc->pc.frame_start_found = pic_found;
     229        4746 :     vpc->unesc_index = unesc_index;
     230        4746 :     vpc->search_state = search_state;
     231             : 
     232        4746 :     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
     233           0 :         next = buf_size;
     234             :     } else {
     235        4746 :         if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
     236        4437 :             vpc->bytes_to_skip = 0;
     237        4437 :             *poutbuf = NULL;
     238        4437 :             *poutbuf_size = 0;
     239        4437 :             return buf_size;
     240             :         }
     241             :     }
     242             : 
     243             :     /* If we return with a valid pointer to a combined frame buffer
     244             :      * then on the next call then we'll have been unhelpfully rewound
     245             :      * by up to 4 bytes (depending upon whether the start code
     246             :      * overlapped the input buffer, and if so by how much). We don't
     247             :      * want this: it will either cause spurious second detections of
     248             :      * the start code we've already seen, or cause extra bytes to be
     249             :      * inserted at the start of the unescaped buffer. */
     250         309 :     vpc->bytes_to_skip = 4;
     251         309 :     if (next < 0 && next != END_NOT_FOUND)
     252           0 :         vpc->bytes_to_skip += next;
     253             : 
     254         309 :     *poutbuf = buf;
     255         309 :     *poutbuf_size = buf_size;
     256         309 :     return next;
     257             : }
     258             : 
     259           0 : static int vc1_split(AVCodecContext *avctx,
     260             :                            const uint8_t *buf, int buf_size)
     261             : {
     262           0 :     uint32_t state = -1;
     263           0 :     int charged = 0;
     264           0 :     const uint8_t *ptr = buf, *end = buf + buf_size;
     265             : 
     266           0 :     while (ptr < end) {
     267           0 :         ptr = avpriv_find_start_code(ptr, end, &state);
     268           0 :         if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
     269           0 :             charged = 1;
     270           0 :         } else if (charged && IS_MARKER(state))
     271           0 :             return ptr - 4 - buf;
     272             :     }
     273             : 
     274           0 :     return 0;
     275             : }
     276             : 
     277           8 : static av_cold int vc1_parse_init(AVCodecParserContext *s)
     278             : {
     279           8 :     VC1ParseContext *vpc = s->priv_data;
     280           8 :     vpc->v.s.slice_context_count = 1;
     281           8 :     vpc->v.first_pic_header_flag = 1;
     282           8 :     vpc->prev_start_code = 0;
     283           8 :     vpc->bytes_to_skip = 0;
     284           8 :     vpc->unesc_index = 0;
     285           8 :     vpc->search_state = NO_MATCH;
     286           8 :     return ff_vc1_init_common(&vpc->v);
     287             : }
     288             : 
     289             : AVCodecParser ff_vc1_parser = {
     290             :     .codec_ids      = { AV_CODEC_ID_VC1 },
     291             :     .priv_data_size = sizeof(VC1ParseContext),
     292             :     .parser_init    = vc1_parse_init,
     293             :     .parser_parse   = vc1_parse,
     294             :     .parser_close   = ff_parse_close,
     295             :     .split          = vc1_split,
     296             : };

Generated by: LCOV version 1.13