LCOV - code coverage report
Current view: top level - libavcodec - parser.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 149 181 82.3 %
Date: 2017-12-18 06:23:41 Functions: 9 10 90.0 %

          Line data    Source code
       1             : /*
       2             :  * Audio and Video frame extraction
       3             :  * Copyright (c) 2003 Fabrice Bellard
       4             :  * Copyright (c) 2003 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             : #include <inttypes.h>
      24             : #include <stdint.h>
      25             : #include <string.h>
      26             : 
      27             : #include "libavutil/avassert.h"
      28             : #include "libavutil/atomic.h"
      29             : #include "libavutil/internal.h"
      30             : #include "libavutil/mem.h"
      31             : 
      32             : #include "internal.h"
      33             : #include "parser.h"
      34             : 
      35             : static AVCodecParser *av_first_parser = NULL;
      36             : 
      37           0 : AVCodecParser *av_parser_next(const AVCodecParser *p)
      38             : {
      39           0 :     if (p)
      40           0 :         return p->next;
      41             :     else
      42           0 :         return av_first_parser;
      43             : }
      44             : 
      45      223840 : void av_register_codec_parser(AVCodecParser *parser)
      46             : {
      47             :     do {
      48      223840 :         parser->next = av_first_parser;
      49      223840 :     } while (parser->next != avpriv_atomic_ptr_cas((void * volatile *)&av_first_parser, parser->next, parser));
      50      223840 : }
      51             : 
      52        8341 : AVCodecParserContext *av_parser_init(int codec_id)
      53             : {
      54        8341 :     AVCodecParserContext *s = NULL;
      55             :     AVCodecParser *parser;
      56             :     int ret;
      57             : 
      58        8341 :     if (codec_id == AV_CODEC_ID_NONE)
      59          58 :         return NULL;
      60             : 
      61      205553 :     for (parser = av_first_parser; parser; parser = parser->next) {
      62      402736 :         if (parser->codec_ids[0] == codec_id ||
      63      397620 :             parser->codec_ids[1] == codec_id ||
      64      394705 :             parser->codec_ids[2] == codec_id ||
      65      394564 :             parser->codec_ids[3] == codec_id ||
      66      197278 :             parser->codec_ids[4] == codec_id)
      67             :             goto found;
      68             :     }
      69        3018 :     return NULL;
      70             : 
      71        5265 : found:
      72        5265 :     s = av_mallocz(sizeof(AVCodecParserContext));
      73        5265 :     if (!s)
      74           0 :         goto err_out;
      75        5265 :     s->parser = parser;
      76        5265 :     s->priv_data = av_mallocz(parser->priv_data_size);
      77        5265 :     if (!s->priv_data)
      78           0 :         goto err_out;
      79        5265 :     s->fetch_timestamp=1;
      80        5265 :     s->pict_type = AV_PICTURE_TYPE_I;
      81        5265 :     if (parser->parser_init) {
      82        1761 :         ret = parser->parser_init(s);
      83        1761 :         if (ret != 0)
      84           0 :             goto err_out;
      85             :     }
      86        5265 :     s->key_frame            = -1;
      87             : #if FF_API_CONVERGENCE_DURATION
      88             : FF_DISABLE_DEPRECATION_WARNINGS
      89        5265 :     s->convergence_duration = 0;
      90             : FF_ENABLE_DEPRECATION_WARNINGS
      91             : #endif
      92        5265 :     s->dts_sync_point       = INT_MIN;
      93        5265 :     s->dts_ref_dts_delta    = INT_MIN;
      94        5265 :     s->pts_dts_delta        = INT_MIN;
      95        5265 :     s->format               = -1;
      96             : 
      97        5265 :     return s;
      98             : 
      99           0 : err_out:
     100           0 :     if (s)
     101           0 :         av_freep(&s->priv_data);
     102           0 :     av_free(s);
     103           0 :     return NULL;
     104             : }
     105             : 
     106      139636 : void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
     107             : {
     108             :     int i;
     109             : 
     110      139636 :     if (!fuzzy) {
     111      138863 :         s->dts    =
     112      138863 :         s->pts    = AV_NOPTS_VALUE;
     113      138863 :         s->pos    = -1;
     114      138863 :         s->offset = 0;
     115             :     }
     116      433012 :     for (i = 0; i < AV_PARSER_PTS_NB; i++) {
     117      790369 :         if (s->cur_offset + off >= s->cur_frame_offset[i] &&
     118      635812 :             (s->frame_offset < s->cur_frame_offset[i] ||
     119      397575 :              (!s->frame_offset && !s->next_frame_offset)) && // first field/frame
     120             :             // check disabled since MPEG-TS does not send complete PES packets
     121      148664 :             /*s->next_frame_offset + off <*/  s->cur_frame_end[i]){
     122             : 
     123      147822 :             if (!fuzzy || s->cur_frame_dts[i] != AV_NOPTS_VALUE) {
     124      147005 :                 s->dts    = s->cur_frame_dts[i];
     125      147005 :                 s->pts    = s->cur_frame_pts[i];
     126      147005 :                 s->pos    = s->cur_frame_pos[i];
     127      147005 :                 s->offset = s->next_frame_offset - s->cur_frame_offset[i];
     128             :             }
     129      147822 :             if (remove)
     130       10544 :                 s->cur_frame_offset[i] = INT64_MAX;
     131      147822 :             if (s->cur_offset + off < s->cur_frame_end[i])
     132      105586 :                 break;
     133             :         }
     134             :     }
     135      139636 : }
     136             : 
     137      644951 : int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
     138             :                      uint8_t **poutbuf, int *poutbuf_size,
     139             :                      const uint8_t *buf, int buf_size,
     140             :                      int64_t pts, int64_t dts, int64_t pos)
     141             : {
     142             :     int index, i;
     143             :     uint8_t dummy_buf[AV_INPUT_BUFFER_PADDING_SIZE];
     144             : 
     145             :     av_assert1(avctx->codec_id != AV_CODEC_ID_NONE);
     146             : 
     147             :     /* Parsers only work for the specified codec ids. */
     148             :     av_assert1(avctx->codec_id == s->parser->codec_ids[0] ||
     149             :                avctx->codec_id == s->parser->codec_ids[1] ||
     150             :                avctx->codec_id == s->parser->codec_ids[2] ||
     151             :                avctx->codec_id == s->parser->codec_ids[3] ||
     152             :                avctx->codec_id == s->parser->codec_ids[4]);
     153             : 
     154      644951 :     if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
     155        2789 :         s->next_frame_offset =
     156        2789 :         s->cur_offset        = pos;
     157        2789 :         s->flags            |= PARSER_FLAG_FETCHED_OFFSET;
     158             :     }
     159             : 
     160      644951 :     if (buf_size == 0) {
     161             :         /* padding is always necessary even if EOF, so we add it here */
     162        2408 :         memset(dummy_buf, 0, sizeof(dummy_buf));
     163        2408 :         buf = dummy_buf;
     164      642543 :     } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
     165             :         /* add a new packet descriptor */
     166      565147 :         i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
     167      565147 :         s->cur_frame_start_index = i;
     168      565147 :         s->cur_frame_offset[i]   = s->cur_offset;
     169      565147 :         s->cur_frame_end[i]      = s->cur_offset + buf_size;
     170      565147 :         s->cur_frame_pts[i]      = pts;
     171      565147 :         s->cur_frame_dts[i]      = dts;
     172      565147 :         s->cur_frame_pos[i]      = pos;
     173             :     }
     174             : 
     175      644951 :     if (s->fetch_timestamp) {
     176      133669 :         s->fetch_timestamp = 0;
     177      133669 :         s->last_pts        = s->pts;
     178      133669 :         s->last_dts        = s->dts;
     179      133669 :         s->last_pos        = s->pos;
     180      133669 :         ff_fetch_timestamp(s, 0, 0, 0);
     181             :     }
     182             :     /* WARNING: the returned index can be negative */
     183      644951 :     index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
     184             :                                     poutbuf_size, buf, buf_size);
     185      644951 :     av_assert0(index > -0x20000000); // The API does not allow returning AVERROR codes
     186             : #define FILL(name) if(s->name > 0 && avctx->name <= 0) avctx->name = s->name
     187      644951 :     if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
     188      522502 :         FILL(field_order);
     189             :     }
     190             : 
     191             :     /* update the file pointer */
     192      644951 :     if (*poutbuf_size) {
     193             :         /* fill the data for the current frame */
     194      131722 :         s->frame_offset = s->next_frame_offset;
     195             : 
     196             :         /* offset of the next frame */
     197      131722 :         s->next_frame_offset = s->cur_offset + index;
     198      131722 :         s->fetch_timestamp   = 1;
     199             :     }
     200      644951 :     if (index < 0)
     201        4072 :         index = 0;
     202      644951 :     s->cur_offset += index;
     203      644951 :     return index;
     204             : }
     205             : 
     206       22594 : int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx,
     207             :                      uint8_t **poutbuf, int *poutbuf_size,
     208             :                      const uint8_t *buf, int buf_size, int keyframe)
     209             : {
     210       22594 :     if (s && s->parser->split) {
     211        5172 :         if (avctx->flags  & AV_CODEC_FLAG_GLOBAL_HEADER ||
     212        2586 :             avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER) {
     213           0 :             int i = s->parser->split(avctx, buf, buf_size);
     214           0 :             buf      += i;
     215           0 :             buf_size -= i;
     216             :         }
     217             :     }
     218             : 
     219             :     /* cast to avoid warning about discarding qualifiers */
     220       22594 :     *poutbuf      = (uint8_t *) buf;
     221       22594 :     *poutbuf_size = buf_size;
     222       22594 :     if (avctx->extradata) {
     223       12774 :         if (keyframe && (avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER)) {
     224           0 :             int size = buf_size + avctx->extradata_size;
     225             : 
     226           0 :             *poutbuf_size = size;
     227           0 :             *poutbuf      = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
     228           0 :             if (!*poutbuf)
     229           0 :                 return AVERROR(ENOMEM);
     230             : 
     231           0 :             memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
     232           0 :             memcpy(*poutbuf + avctx->extradata_size, buf,
     233           0 :                    buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
     234           0 :             return 1;
     235             :         }
     236             :     }
     237             : 
     238       22594 :     return 0;
     239             : }
     240             : 
     241       10667 : void av_parser_close(AVCodecParserContext *s)
     242             : {
     243       10667 :     if (s) {
     244        5265 :         if (s->parser->parser_close)
     245        4965 :             s->parser->parser_close(s);
     246        5265 :         av_freep(&s->priv_data);
     247        5265 :         av_free(s);
     248             :     }
     249       10667 : }
     250             : 
     251      601183 : int ff_combine_frame(ParseContext *pc, int next,
     252             :                      const uint8_t **buf, int *buf_size)
     253             : {
     254      601183 :     if (pc->overread) {
     255             :         ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
     256             :                 pc->overread, pc->state, next, pc->index, pc->overread_index);
     257             :         ff_dlog(NULL, "%X %X %X %X\n",
     258             :                 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
     259             :     }
     260             : 
     261             :     /* Copy overread bytes from last frame into buffer. */
     262      602291 :     for (; pc->overread > 0; pc->overread--)
     263        1108 :         pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
     264             : 
     265             :     /* flush remaining if EOF */
     266      601183 :     if (!*buf_size && next == END_NOT_FOUND)
     267        1235 :         next = 0;
     268             : 
     269      601183 :     pc->last_index = pc->index;
     270             : 
     271             :     /* copy into buffer end return */
     272      601183 :     if (next == END_NOT_FOUND) {
     273      508283 :         void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
     274      508283 :                                            *buf_size + pc->index +
     275             :                                            AV_INPUT_BUFFER_PADDING_SIZE);
     276             : 
     277      508283 :         if (!new_buffer) {
     278           0 :             av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", *buf_size + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
     279           0 :             pc->index = 0;
     280           0 :             return AVERROR(ENOMEM);
     281             :         }
     282      508283 :         pc->buffer = new_buffer;
     283      508283 :         memcpy(&pc->buffer[pc->index], *buf, *buf_size);
     284      508283 :         pc->index += *buf_size;
     285      508283 :         return -1;
     286             :     }
     287             : 
     288       92900 :     av_assert0(next >= 0 || pc->buffer);
     289             : 
     290       92900 :     *buf_size          =
     291       92900 :     pc->overread_index = pc->index + next;
     292             : 
     293             :     /* append to buffer */
     294       92900 :     if (pc->index) {
     295       44108 :         void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
     296       44108 :                                            next + pc->index +
     297             :                                            AV_INPUT_BUFFER_PADDING_SIZE);
     298       44108 :         if (!new_buffer) {
     299           0 :             av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", next + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
     300           0 :             pc->overread_index =
     301           0 :             pc->index = 0;
     302           0 :             return AVERROR(ENOMEM);
     303             :         }
     304       44108 :         pc->buffer = new_buffer;
     305       44108 :         if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
     306       44071 :             memcpy(&pc->buffer[pc->index], *buf,
     307       44071 :                    next + AV_INPUT_BUFFER_PADDING_SIZE);
     308       44108 :         pc->index = 0;
     309       44108 :         *buf      = pc->buffer;
     310             :     }
     311             : 
     312             :     /* store overread bytes */
     313       95977 :     for (; next < 0; next++) {
     314        3077 :         pc->state   = pc->state   << 8 | pc->buffer[pc->last_index + next];
     315        3077 :         pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
     316        3077 :         pc->overread++;
     317             :     }
     318             : 
     319       92900 :     if (pc->overread) {
     320             :         ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
     321             :                 pc->overread, pc->state, next, pc->index, pc->overread_index);
     322             :         ff_dlog(NULL, "%X %X %X %X\n",
     323             :                 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
     324             :     }
     325             : 
     326       92900 :     return 0;
     327             : }
     328             : 
     329        4181 : void ff_parse_close(AVCodecParserContext *s)
     330             : {
     331        4181 :     ParseContext *pc = s->priv_data;
     332             : 
     333        4181 :     av_freep(&pc->buffer);
     334        4181 : }
     335             : 
     336          50 : int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
     337             : {
     338          50 :     uint32_t state = -1;
     339          50 :     const uint8_t *ptr = buf, *end = buf + buf_size;
     340             : 
     341         124 :     while (ptr < end) {
     342          74 :         ptr = avpriv_find_start_code(ptr, end, &state);
     343          74 :         if (state == 0x1B3 || state == 0x1B6)
     344          50 :             return ptr - 4 - buf;
     345             :     }
     346             : 
     347           0 :     return 0;
     348             : }

Generated by: LCOV version 1.13