LCOV - code coverage report
Current view: top level - libavcodec - opus_parser.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 65 83 78.3 %
Date: 2017-12-11 04:34:20 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013-2014 Mozilla Corporation
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : /**
      22             :  * @file
      23             :  * Opus parser
      24             :  *
      25             :  * Determines the duration for each packet.
      26             :  */
      27             : 
      28             : #include "avcodec.h"
      29             : #include "bytestream.h"
      30             : #include "opus.h"
      31             : #include "parser.h"
      32             : 
      33             : typedef struct OpusParseContext {
      34             :     ParseContext pc;
      35             :     OpusContext ctx;
      36             :     OpusPacket pkt;
      37             :     int extradata_parsed;
      38             :     int ts_framing;
      39             : } OpusParseContext;
      40             : 
      41         765 : static const uint8_t *parse_opus_ts_header(const uint8_t *start, int *payload_len, int buf_len)
      42             : {
      43         765 :     const uint8_t *buf = start + 1;
      44             :     int start_trim_flag, end_trim_flag, control_extension_flag, control_extension_length;
      45             :     uint8_t flags;
      46             : 
      47             :     GetByteContext gb;
      48         765 :     bytestream2_init(&gb, buf, buf_len);
      49             : 
      50         765 :     flags = bytestream2_get_byte(&gb);
      51         765 :     start_trim_flag        = (flags >> 4) & 1;
      52         765 :     end_trim_flag          = (flags >> 3) & 1;
      53         765 :     control_extension_flag = (flags >> 2) & 1;
      54             : 
      55         765 :     *payload_len = 0;
      56        3694 :     while (bytestream2_peek_byte(&gb) == 0xff)
      57        2164 :         *payload_len += bytestream2_get_byte(&gb);
      58             : 
      59         765 :     *payload_len += bytestream2_get_byte(&gb);
      60             : 
      61         765 :     if (start_trim_flag)
      62           0 :         bytestream2_skip(&gb, 2);
      63         765 :     if (end_trim_flag)
      64           0 :         bytestream2_skip(&gb, 2);
      65         765 :     if (control_extension_flag) {
      66           0 :         control_extension_length = bytestream2_get_byte(&gb);
      67           0 :         bytestream2_skip(&gb, control_extension_length);
      68             :     }
      69             : 
      70         765 :     return buf + bytestream2_tell(&gb);
      71             : }
      72             : 
      73             : /**
      74             :  * Find the end of the current frame in the bitstream.
      75             :  * @return the position of the first byte of the next frame, or -1
      76             :  */
      77       22671 : static int opus_find_frame_end(AVCodecParserContext *ctx, AVCodecContext *avctx,
      78             :                                const uint8_t *buf, int buf_size, int *header_len)
      79             : {
      80       22671 :     OpusParseContext *s = ctx->priv_data;
      81       22671 :     ParseContext *pc    = &s->pc;
      82       22671 :     int ret, start_found, i = 0, payload_len = 0;
      83             :     const uint8_t *payload;
      84             :     uint32_t state;
      85             :     uint16_t hdr;
      86       22671 :     *header_len = 0;
      87             : 
      88       22671 :     if (!buf_size)
      89          19 :         return 0;
      90             : 
      91       22652 :     start_found = pc->frame_start_found;
      92       22652 :     state = pc->state;
      93       22652 :     payload = buf;
      94             : 
      95             :     /* Check if we're using Opus in MPEG-TS framing */
      96       22652 :     if (!s->ts_framing && buf_size > 2) {
      97       21889 :         hdr = AV_RB16(buf);
      98       21889 :         if ((hdr & OPUS_TS_MASK) == OPUS_TS_HEADER)
      99           2 :             s->ts_framing = 1;
     100             :     }
     101             : 
     102       22652 :     if (s->ts_framing && !start_found) {
     103        1530 :         for (i = 0; i < buf_size-2; i++) {
     104        1530 :             state = (state << 8) | payload[i];
     105        1530 :             if ((state & OPUS_TS_MASK) == OPUS_TS_HEADER) {
     106         765 :                 payload = parse_opus_ts_header(payload, &payload_len, buf_size - i);
     107         765 :                 *header_len = payload - buf;
     108         765 :                 start_found = 1;
     109         765 :                 break;
     110             :             }
     111             :         }
     112             :     }
     113             : 
     114       22652 :     if (!s->ts_framing)
     115       21887 :         payload_len = buf_size;
     116             : 
     117       22652 :     if (avctx->extradata && !s->extradata_parsed) {
     118          42 :         ret = ff_opus_parse_extradata(avctx, &s->ctx);
     119          42 :         if (ret < 0) {
     120           0 :             av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n");
     121           0 :             return AVERROR_INVALIDDATA;
     122             :         }
     123          42 :         av_freep(&s->ctx.channel_maps);
     124          42 :         s->extradata_parsed = 1;
     125             :     }
     126             : 
     127       22652 :     if (payload_len <= buf_size && (!s->ts_framing || start_found)) {
     128       22652 :         ret = ff_opus_parse_packet(&s->pkt, payload, payload_len, s->ctx.nb_streams > 1);
     129       22652 :         if (ret < 0) {
     130           0 :             av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n");
     131           0 :             pc->frame_start_found = 0;
     132           0 :             return AVERROR_INVALIDDATA;
     133             :         }
     134             : 
     135       22652 :         ctx->duration = s->pkt.frame_count * s->pkt.frame_duration;
     136             :     }
     137             : 
     138       22652 :     if (s->ts_framing) {
     139         765 :         if (start_found) {
     140         765 :             if (payload_len + *header_len <= buf_size) {
     141         765 :                 pc->frame_start_found = 0;
     142         765 :                 pc->state             = -1;
     143         765 :                 return payload_len + *header_len;
     144             :             }
     145             :         }
     146             : 
     147           0 :         pc->frame_start_found = start_found;
     148           0 :         pc->state = state;
     149           0 :         return END_NOT_FOUND;
     150             :     }
     151             : 
     152       21887 :     return buf_size;
     153             : }
     154             : 
     155       22671 : static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx,
     156             :                        const uint8_t **poutbuf, int *poutbuf_size,
     157             :                        const uint8_t *buf, int buf_size)
     158             : {
     159       22671 :     OpusParseContext *s = ctx->priv_data;
     160       22671 :     ParseContext *pc    = &s->pc;
     161             :     int next, header_len;
     162             : 
     163       22671 :     next = opus_find_frame_end(ctx, avctx, buf, buf_size, &header_len);
     164             : 
     165       23437 :     if (s->ts_framing && next != AVERROR_INVALIDDATA &&
     166         766 :         ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
     167           0 :         *poutbuf      = NULL;
     168           0 :         *poutbuf_size = 0;
     169           0 :         return buf_size;
     170             :     }
     171             : 
     172       22671 :     if (next == AVERROR_INVALIDDATA){
     173           0 :         *poutbuf      = NULL;
     174           0 :         *poutbuf_size = 0;
     175           0 :         return buf_size;
     176             :     }
     177             : 
     178       22671 :     *poutbuf      = buf + header_len;
     179       22671 :     *poutbuf_size = buf_size - header_len;
     180       22671 :     return next;
     181             : }
     182             : 
     183             : AVCodecParser ff_opus_parser = {
     184             :     .codec_ids      = { AV_CODEC_ID_OPUS },
     185             :     .priv_data_size = sizeof(OpusParseContext),
     186             :     .parser_parse   = opus_parse,
     187             :     .parser_close   = ff_parse_close
     188             : };

Generated by: LCOV version 1.13