LCOV - code coverage report
Current view: top level - libavcodec - tak_parser.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 45 55 81.8 %
Date: 2017-12-15 18:13:28 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * TAK parser
       3             :  * Copyright (c) 2012 Michael Niedermayer
       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             :  * TAK parser
      25             :  **/
      26             : 
      27             : #define BITSTREAM_READER_LE
      28             : #include "parser.h"
      29             : #include "tak.h"
      30             : 
      31             : typedef struct TAKParseContext {
      32             :     ParseContext  pc;
      33             :     TAKStreamInfo ti;
      34             :     int           index;
      35             : } TAKParseContext;
      36             : 
      37        1026 : static int tak_parse(AVCodecParserContext *s, AVCodecContext *avctx,
      38             :                      const uint8_t **poutbuf, int *poutbuf_size,
      39             :                      const uint8_t *buf, int buf_size)
      40             : {
      41        1026 :     TAKParseContext *t = s->priv_data;
      42        1026 :     ParseContext *pc   = &t->pc;
      43        1026 :     int next           = END_NOT_FOUND;
      44             :     GetBitContext gb;
      45        1026 :     int consumed = 0;
      46        1026 :     int needed   = buf_size ? TAK_MAX_FRAME_HEADER_BYTES : 8;
      47             :     int ret;
      48             : 
      49        1026 :     if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
      50             :         TAKStreamInfo ti;
      51           0 :         if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
      52           0 :             return ret;
      53           0 :         if (!ff_tak_decode_frame_header(avctx, &gb, &ti, 127))
      54           0 :             s->duration = t->ti.last_frame_samples ? t->ti.last_frame_samples
      55           0 :                                                    : t->ti.frame_samples;
      56           0 :         *poutbuf      = buf;
      57           0 :         *poutbuf_size = buf_size;
      58           0 :         return buf_size;
      59             :     }
      60             : 
      61       29703 :     while (buf_size || t->index + needed <= pc->index) {
      62       27688 :         if (buf_size && t->index + TAK_MAX_FRAME_HEADER_BYTES > pc->index) {
      63       27687 :             int tmp_buf_size       = FFMIN(TAK_MAX_FRAME_HEADER_BYTES,
      64             :                                            buf_size);
      65       27687 :             const uint8_t *tmp_buf = buf;
      66             : 
      67       27687 :             if (ff_combine_frame(pc, END_NOT_FOUND, &tmp_buf, &tmp_buf_size) != -1)
      68           0 :                 return AVERROR(ENOMEM);
      69       27687 :             consumed += tmp_buf_size;
      70       27687 :             buf      += tmp_buf_size;
      71       27687 :             buf_size -= tmp_buf_size;
      72             :         }
      73             : 
      74     1037901 :         for (; t->index + needed <= pc->index; t->index++) {
      75     1013913 :             if (pc->buffer[ t->index     ] == 0xFF &&
      76        3663 :                 pc->buffer[ t->index + 1 ] == 0xA0) {
      77             :                 TAKStreamInfo ti;
      78             : 
      79          86 :                 if ((ret = init_get_bits8(&gb, pc->buffer + t->index,
      80          86 :                                           pc->index - t->index)) < 0)
      81           0 :                     return ret;
      82          86 :                 if (!ff_tak_decode_frame_header(avctx, &gb,
      83         169 :                         pc->frame_start_found ? &ti : &t->ti, 127) &&
      84          83 :                     !ff_tak_check_crc(pc->buffer + t->index,
      85          83 :                                       get_bits_count(&gb) / 8)) {
      86          75 :                     if (!pc->frame_start_found) {
      87          38 :                         pc->frame_start_found = 1;
      88          76 :                         s->duration           = t->ti.last_frame_samples ?
      89          38 :                                                 t->ti.last_frame_samples :
      90             :                                                 t->ti.frame_samples;
      91          38 :                         s->key_frame          = !!(t->ti.flags & TAK_FRAME_FLAG_HAS_INFO);
      92             :                     } else {
      93          37 :                         pc->frame_start_found = 0;
      94          37 :                         next                  = t->index - pc->index;
      95          37 :                         t->index              = 0;
      96          37 :                         goto found;
      97             :                     }
      98             :                 }
      99             :             }
     100             :         }
     101             :     }
     102         989 : found:
     103             : 
     104        1065 :     if (consumed && !buf_size && next == END_NOT_FOUND ||
     105          39 :         ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
     106         987 :         *poutbuf      = NULL;
     107         987 :         *poutbuf_size = 0;
     108         987 :         return buf_size + consumed;
     109             :     }
     110             : 
     111          39 :     if (next != END_NOT_FOUND) {
     112          37 :         next        += consumed;
     113          37 :         pc->overread = FFMAX(0, -next);
     114             :     }
     115             : 
     116          39 :     *poutbuf      = buf;
     117          39 :     *poutbuf_size = buf_size;
     118          39 :     return next;
     119             : }
     120             : 
     121             : AVCodecParser ff_tak_parser = {
     122             :     .codec_ids      = { AV_CODEC_ID_TAK },
     123             :     .priv_data_size = sizeof(TAKParseContext),
     124             :     .parser_parse   = tak_parse,
     125             :     .parser_close   = ff_parse_close,
     126             : };

Generated by: LCOV version 1.13