LCOV - code coverage report
Current view: top level - libavcodec - dnxhd_parser.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 58 67 86.6 %
Date: 2017-12-11 04:34:20 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * DNxHD/VC-3 parser
       3             :  * Copyright (c) 2008 Baptiste Coudurier <baptiste.coudurier@free.fr>
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * DNxHD/VC-3 parser
      25             :  */
      26             : 
      27             : #include "parser.h"
      28             : #include "dnxhddata.h"
      29             : 
      30             : typedef struct {
      31             :     ParseContext pc;
      32             :     int cur_byte;
      33             :     int remaining;
      34             :     int w, h;
      35             : } DNXHDParserContext;
      36             : 
      37      177444 : static int dnxhd_find_frame_end(DNXHDParserContext *dctx,
      38             :                                 const uint8_t *buf, int buf_size)
      39             : {
      40      177444 :     ParseContext *pc = &dctx->pc;
      41      177444 :     uint64_t state = pc->state64;
      42      177444 :     int pic_found = pc->frame_start_found;
      43      177444 :     int i = 0;
      44             : 
      45      177444 :     if (!pic_found) {
      46        2122 :         for (i = 0; i < buf_size; i++) {
      47        2022 :             state = (state << 8) | buf[i];
      48        2022 :             if (ff_dnxhd_check_header_prefix(state & 0xffffffffff00LL) != 0) {
      49         337 :                 i++;
      50         337 :                 pic_found = 1;
      51         337 :                 dctx->cur_byte = 0;
      52         337 :                 dctx->remaining = 0;
      53         337 :                 break;
      54             :             }
      55             :         }
      56             :     }
      57             : 
      58      177781 :     if (pic_found && !dctx->remaining) {
      59         337 :         if (!buf_size) /* EOF considered as end of frame */
      60           0 :             return 0;
      61      343403 :         for (; i < buf_size; i++) {
      62      343066 :             dctx->cur_byte++;
      63      343066 :             state = (state << 8) | buf[i];
      64             : 
      65      343066 :             if (dctx->cur_byte == 24) {
      66         337 :                 dctx->h = (state >> 32) & 0xFFFF;
      67      342729 :             } else if (dctx->cur_byte == 26) {
      68         337 :                 dctx->w = (state >> 32) & 0xFFFF;
      69      342392 :             } else if (dctx->cur_byte == 42) {
      70         337 :                 int cid = (state >> 32) & 0xFFFFFFFF;
      71             :                 int remaining;
      72             : 
      73         337 :                 if (cid <= 0)
      74           0 :                     continue;
      75             : 
      76         337 :                 remaining = avpriv_dnxhd_get_frame_size(cid);
      77         337 :                 if (remaining <= 0) {
      78         210 :                     remaining = ff_dnxhd_get_hr_frame_size(cid, dctx->w, dctx->h);
      79         210 :                     if (remaining <= 0)
      80           0 :                         continue;
      81             :                 }
      82         337 :                 dctx->remaining = remaining;
      83         337 :                 if (buf_size - i + 47 >= dctx->remaining) {
      84           0 :                     int remaining = dctx->remaining;
      85             : 
      86           0 :                     pc->frame_start_found = 0;
      87           0 :                     pc->state64 = -1;
      88           0 :                     dctx->cur_byte = 0;
      89           0 :                     dctx->remaining = 0;
      90           0 :                     return remaining;
      91             :                 } else {
      92         337 :                     dctx->remaining -= buf_size;
      93             :                 }
      94             :             }
      95             :         }
      96      177107 :     } else if (pic_found) {
      97      177007 :         if (dctx->remaining > buf_size) {
      98      176670 :             dctx->remaining -= buf_size;
      99             :         } else {
     100         337 :             int remaining = dctx->remaining;
     101             : 
     102         337 :             pc->frame_start_found = 0;
     103         337 :             pc->state64 = -1;
     104         337 :             dctx->cur_byte = 0;
     105         337 :             dctx->remaining = 0;
     106         337 :             return remaining;
     107             :         }
     108             :     }
     109      177107 :     pc->frame_start_found = pic_found;
     110      177107 :     pc->state64 = state;
     111      177107 :     return END_NOT_FOUND;
     112             : }
     113             : 
     114      177532 : static int dnxhd_parse(AVCodecParserContext *s,
     115             :                        AVCodecContext *avctx,
     116             :                        const uint8_t **poutbuf, int *poutbuf_size,
     117             :                        const uint8_t *buf, int buf_size)
     118             : {
     119      177532 :     DNXHDParserContext *dctx = s->priv_data;
     120      177532 :     ParseContext *pc = &dctx->pc;
     121             :     int next;
     122             : 
     123      177532 :     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
     124          88 :         next = buf_size;
     125             :     } else {
     126      177444 :         next = dnxhd_find_frame_end(dctx, buf, buf_size);
     127      177444 :         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
     128      177007 :             *poutbuf      = NULL;
     129      177007 :             *poutbuf_size = 0;
     130      177007 :             return buf_size;
     131             :         }
     132             :     }
     133         525 :     *poutbuf      = buf;
     134         525 :     *poutbuf_size = buf_size;
     135         525 :     return next;
     136             : }
     137             : 
     138             : AVCodecParser ff_dnxhd_parser = {
     139             :     .codec_ids      = { AV_CODEC_ID_DNXHD },
     140             :     .priv_data_size = sizeof(DNXHDParserContext),
     141             :     .parser_parse   = dnxhd_parse,
     142             :     .parser_close   = ff_parse_close,
     143             : };

Generated by: LCOV version 1.13