| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 "libavutil/mem.h" | ||
| 29 | #include "avcodec.h" | ||
| 30 | #include "bytestream.h" | ||
| 31 | #include "opus.h" | ||
| 32 | #include "parse.h" | ||
| 33 | #include "parser.h" | ||
| 34 | #include "parser_internal.h" | ||
| 35 | |||
| 36 | typedef struct OpusParserContext { | ||
| 37 | ParseContext pc; | ||
| 38 | OpusParseContext ctx; | ||
| 39 | OpusPacket pkt; | ||
| 40 | int extradata_parsed; | ||
| 41 | int ts_framing; | ||
| 42 | } OpusParserContext; | ||
| 43 | |||
| 44 | 765 | static const uint8_t *parse_opus_ts_header(const uint8_t *start, int *payload_len, int buf_len) | |
| 45 | { | ||
| 46 | 765 | const uint8_t *buf = start + 1; | |
| 47 | int start_trim_flag, end_trim_flag, control_extension_flag, control_extension_length; | ||
| 48 | uint8_t flags; | ||
| 49 | uint64_t payload_len_tmp; | ||
| 50 | |||
| 51 | GetByteContext gb; | ||
| 52 | 765 | bytestream2_init(&gb, buf, buf_len); | |
| 53 | |||
| 54 | 765 | flags = bytestream2_get_byte(&gb); | |
| 55 | 765 | start_trim_flag = (flags >> 4) & 1; | |
| 56 | 765 | end_trim_flag = (flags >> 3) & 1; | |
| 57 | 765 | control_extension_flag = (flags >> 2) & 1; | |
| 58 | |||
| 59 | 765 | payload_len_tmp = *payload_len = 0; | |
| 60 |
2/2✓ Branch 1 taken 2164 times.
✓ Branch 2 taken 765 times.
|
2929 | while (bytestream2_peek_byte(&gb) == 0xff) |
| 61 | 2164 | payload_len_tmp += bytestream2_get_byte(&gb); | |
| 62 | |||
| 63 | 765 | payload_len_tmp += bytestream2_get_byte(&gb); | |
| 64 | |||
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
|
765 | if (start_trim_flag) |
| 66 | ✗ | bytestream2_skip(&gb, 2); | |
| 67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
|
765 | if (end_trim_flag) |
| 68 | ✗ | bytestream2_skip(&gb, 2); | |
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
|
765 | if (control_extension_flag) { |
| 70 | ✗ | control_extension_length = bytestream2_get_byte(&gb); | |
| 71 | ✗ | bytestream2_skip(&gb, control_extension_length); | |
| 72 | } | ||
| 73 | |||
| 74 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 765 times.
|
765 | if (bytestream2_tell(&gb) + payload_len_tmp > buf_len) |
| 75 | ✗ | return NULL; | |
| 76 | |||
| 77 | 765 | *payload_len = payload_len_tmp; | |
| 78 | |||
| 79 | 765 | return buf + bytestream2_tell(&gb); | |
| 80 | } | ||
| 81 | |||
| 82 | 22877 | static int set_frame_duration(AVCodecParserContext *ctx, AVCodecContext *avctx, | |
| 83 | const uint8_t *buf, int buf_size) | ||
| 84 | { | ||
| 85 | 22877 | OpusParserContext *s = ctx->priv_data; | |
| 86 | |||
| 87 |
2/2✓ Branch 1 taken 22 times.
✓ Branch 2 taken 22855 times.
|
22877 | if (ff_opus_parse_packet(&s->pkt, buf, buf_size, s->ctx.nb_streams > 1) < 0) { |
| 88 | 22 | av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n"); | |
| 89 | 22 | return AVERROR_INVALIDDATA; | |
| 90 | } | ||
| 91 | |||
| 92 | 22855 | ctx->duration = s->pkt.frame_count * s->pkt.frame_duration; | |
| 93 | |||
| 94 | 22855 | return 0; | |
| 95 | } | ||
| 96 | |||
| 97 | /** | ||
| 98 | * Find the end of the current frame in the bitstream. | ||
| 99 | * @return the position of the first byte of the next frame, or -1 | ||
| 100 | */ | ||
| 101 | 766 | static int opus_find_frame_end(AVCodecParserContext *ctx, AVCodecContext *avctx, | |
| 102 | const uint8_t *buf, int buf_size, int *header_len) | ||
| 103 | { | ||
| 104 | 766 | OpusParserContext *s = ctx->priv_data; | |
| 105 | 766 | ParseContext *pc = &s->pc; | |
| 106 | 766 | int ret, start_found, i = 0, payload_len = 0; | |
| 107 | const uint8_t *payload; | ||
| 108 | uint32_t state; | ||
| 109 | uint16_t hdr; | ||
| 110 | 766 | *header_len = 0; | |
| 111 | |||
| 112 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 765 times.
|
766 | if (!buf_size) |
| 113 | 1 | return 0; | |
| 114 | |||
| 115 | 765 | start_found = pc->frame_start_found; | |
| 116 | 765 | state = pc->state; | |
| 117 | 765 | payload = buf; | |
| 118 | |||
| 119 | /* Check if we're using Opus in MPEG-TS framing */ | ||
| 120 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 763 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
765 | if (!s->ts_framing && buf_size > 2) { |
| 121 | 2 | hdr = AV_RB16(buf); | |
| 122 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if ((hdr & OPUS_TS_MASK) == OPUS_TS_HEADER) |
| 123 | 2 | s->ts_framing = 1; | |
| 124 | } | ||
| 125 | |||
| 126 |
2/4✓ Branch 0 taken 765 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 765 times.
✗ Branch 3 not taken.
|
765 | if (s->ts_framing && !start_found) { |
| 127 |
1/2✓ Branch 0 taken 1530 times.
✗ Branch 1 not taken.
|
1530 | for (i = 0; i < buf_size-2; i++) { |
| 128 | 1530 | state = (state << 8) | payload[i]; | |
| 129 |
2/2✓ Branch 0 taken 765 times.
✓ Branch 1 taken 765 times.
|
1530 | if ((state & OPUS_TS_MASK) == OPUS_TS_HEADER) { |
| 130 | 765 | payload = parse_opus_ts_header(payload, &payload_len, buf_size - i); | |
| 131 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
|
765 | if (!payload) { |
| 132 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg TS header.\n"); | |
| 133 | ✗ | return AVERROR_INVALIDDATA; | |
| 134 | } | ||
| 135 | 765 | *header_len = payload - buf; | |
| 136 | 765 | start_found = 1; | |
| 137 | 765 | break; | |
| 138 | } | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
|
765 | if (!s->ts_framing) |
| 143 | ✗ | payload_len = buf_size; | |
| 144 | |||
| 145 |
3/6✓ Branch 0 taken 765 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 765 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 765 times.
✗ Branch 5 not taken.
|
765 | if (payload_len <= buf_size && (!s->ts_framing || start_found)) { |
| 146 | 765 | ret = set_frame_duration(ctx, avctx, payload, payload_len); | |
| 147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
|
765 | if (ret < 0) { |
| 148 | ✗ | pc->frame_start_found = 0; | |
| 149 | ✗ | return AVERROR_INVALIDDATA; | |
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 |
1/2✓ Branch 0 taken 765 times.
✗ Branch 1 not taken.
|
765 | if (s->ts_framing) { |
| 154 |
1/2✓ Branch 0 taken 765 times.
✗ Branch 1 not taken.
|
765 | if (start_found) { |
| 155 |
1/2✓ Branch 0 taken 765 times.
✗ Branch 1 not taken.
|
765 | if (payload_len + *header_len <= buf_size) { |
| 156 | 765 | pc->frame_start_found = 0; | |
| 157 | 765 | pc->state = -1; | |
| 158 | 765 | return payload_len + *header_len; | |
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | ✗ | pc->frame_start_found = start_found; | |
| 163 | ✗ | pc->state = state; | |
| 164 | ✗ | return END_NOT_FOUND; | |
| 165 | } | ||
| 166 | |||
| 167 | ✗ | return buf_size; | |
| 168 | } | ||
| 169 | |||
| 170 | 22878 | static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx, | |
| 171 | const uint8_t **poutbuf, int *poutbuf_size, | ||
| 172 | const uint8_t *buf, int buf_size) | ||
| 173 | { | ||
| 174 | 22878 | OpusParserContext *s = ctx->priv_data; | |
| 175 | 22878 | ParseContext *pc = &s->pc; | |
| 176 | 22878 | int next, header_len = 0; | |
| 177 | |||
| 178 | 22878 | avctx->sample_rate = 48000; | |
| 179 | |||
| 180 |
3/4✓ Branch 0 taken 22878 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 22830 times.
|
22878 | if (avctx->extradata && !s->extradata_parsed) { |
| 181 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
|
48 | if (ff_opus_parse_extradata(avctx, &s->ctx) < 0) { |
| 182 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n"); | |
| 183 | ✗ | goto fail; | |
| 184 | } | ||
| 185 | 48 | av_freep(&s->ctx.channel_maps); | |
| 186 | 48 | s->extradata_parsed = 1; | |
| 187 | } | ||
| 188 | |||
| 189 |
2/2✓ Branch 0 taken 22112 times.
✓ Branch 1 taken 766 times.
|
22878 | if (ctx->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
| 190 | 22112 | next = buf_size; | |
| 191 | |||
| 192 |
2/2✓ Branch 1 taken 22 times.
✓ Branch 2 taken 22090 times.
|
22112 | if (set_frame_duration(ctx, avctx, buf, buf_size) < 0) |
| 193 | 22 | goto fail; | |
| 194 | } else { | ||
| 195 | 766 | next = opus_find_frame_end(ctx, avctx, buf, buf_size, &header_len); | |
| 196 | |||
| 197 |
3/6✓ Branch 0 taken 766 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 766 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 766 times.
|
1532 | if (s->ts_framing && next != AVERROR_INVALIDDATA && |
| 198 | 766 | ff_combine_frame(pc, next, &buf, &buf_size) < 0) { | |
| 199 | ✗ | goto fail; | |
| 200 | } | ||
| 201 | |||
| 202 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 766 times.
|
766 | if (next == AVERROR_INVALIDDATA){ |
| 203 | ✗ | goto fail; | |
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | 22856 | *poutbuf = buf + header_len; | |
| 208 | 22856 | *poutbuf_size = buf_size - header_len; | |
| 209 | 22856 | return next; | |
| 210 | |||
| 211 | 22 | fail: | |
| 212 | 22 | *poutbuf = NULL; | |
| 213 | 22 | *poutbuf_size = 0; | |
| 214 | 22 | return buf_size; | |
| 215 | } | ||
| 216 | |||
| 217 | const FFCodecParser ff_opus_parser = { | ||
| 218 | PARSER_CODEC_LIST(AV_CODEC_ID_OPUS), | ||
| 219 | .priv_data_size = sizeof(OpusParserContext), | ||
| 220 | .parse = opus_parse, | ||
| 221 | .close = ff_parse_close | ||
| 222 | }; | ||
| 223 |