You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
5.8KB

  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. * @file
  22. * Opus parser
  23. *
  24. * Determines the duration for each packet.
  25. */
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "opus.h"
  29. #include "parser.h"
  30. typedef struct OpusParseContext {
  31. ParseContext pc;
  32. OpusContext ctx;
  33. OpusPacket pkt;
  34. int extradata_parsed;
  35. int ts_framing;
  36. } OpusParseContext;
  37. static const uint8_t *parse_opus_ts_header(const uint8_t *start, int *payload_len, int buf_len)
  38. {
  39. const uint8_t *buf = start + 1;
  40. int start_trim_flag, end_trim_flag, control_extension_flag, control_extension_length;
  41. uint8_t flags;
  42. uint64_t payload_len_tmp;
  43. GetByteContext gb;
  44. bytestream2_init(&gb, buf, buf_len);
  45. flags = bytestream2_get_byte(&gb);
  46. start_trim_flag = (flags >> 4) & 1;
  47. end_trim_flag = (flags >> 3) & 1;
  48. control_extension_flag = (flags >> 2) & 1;
  49. payload_len_tmp = *payload_len = 0;
  50. while (bytestream2_peek_byte(&gb) == 0xff)
  51. payload_len_tmp += bytestream2_get_byte(&gb);
  52. payload_len_tmp += bytestream2_get_byte(&gb);
  53. if (start_trim_flag)
  54. bytestream2_skip(&gb, 2);
  55. if (end_trim_flag)
  56. bytestream2_skip(&gb, 2);
  57. if (control_extension_flag) {
  58. control_extension_length = bytestream2_get_byte(&gb);
  59. bytestream2_skip(&gb, control_extension_length);
  60. }
  61. if (bytestream2_tell(&gb) + payload_len_tmp > buf_len)
  62. return NULL;
  63. *payload_len = payload_len_tmp;
  64. return buf + bytestream2_tell(&gb);
  65. }
  66. /**
  67. * Find the end of the current frame in the bitstream.
  68. * @return the position of the first byte of the next frame, or -1
  69. */
  70. static int opus_find_frame_end(AVCodecParserContext *ctx, AVCodecContext *avctx,
  71. const uint8_t *buf, int buf_size, int *header_len)
  72. {
  73. OpusParseContext *s = ctx->priv_data;
  74. ParseContext *pc = &s->pc;
  75. int ret, start_found, i = 0, payload_len = 0;
  76. const uint8_t *payload;
  77. uint32_t state;
  78. uint16_t hdr;
  79. *header_len = 0;
  80. if (!buf_size)
  81. return 0;
  82. start_found = pc->frame_start_found;
  83. state = pc->state;
  84. payload = buf;
  85. /* Check if we're using Opus in MPEG-TS framing */
  86. if (!s->ts_framing && buf_size > 2) {
  87. hdr = AV_RB16(buf);
  88. if ((hdr & OPUS_TS_MASK) == OPUS_TS_HEADER)
  89. s->ts_framing = 1;
  90. }
  91. if (s->ts_framing && !start_found) {
  92. for (i = 0; i < buf_size-2; i++) {
  93. state = (state << 8) | payload[i];
  94. if ((state & OPUS_TS_MASK) == OPUS_TS_HEADER) {
  95. payload = parse_opus_ts_header(payload, &payload_len, buf_size - i);
  96. if (!payload) {
  97. av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg TS header.\n");
  98. return AVERROR_INVALIDDATA;
  99. }
  100. *header_len = payload - buf;
  101. start_found = 1;
  102. break;
  103. }
  104. }
  105. }
  106. if (!s->ts_framing)
  107. payload_len = buf_size;
  108. if (avctx->extradata && !s->extradata_parsed) {
  109. ret = ff_opus_parse_extradata(avctx, &s->ctx);
  110. if (ret < 0) {
  111. av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n");
  112. return AVERROR_INVALIDDATA;
  113. }
  114. av_freep(&s->ctx.channel_maps);
  115. s->extradata_parsed = 1;
  116. }
  117. if (payload_len <= buf_size && (!s->ts_framing || start_found)) {
  118. ret = ff_opus_parse_packet(&s->pkt, payload, payload_len, s->ctx.nb_streams > 1);
  119. if (ret < 0) {
  120. av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n");
  121. pc->frame_start_found = 0;
  122. return AVERROR_INVALIDDATA;
  123. }
  124. ctx->duration = s->pkt.frame_count * s->pkt.frame_duration;
  125. }
  126. if (s->ts_framing) {
  127. if (start_found) {
  128. if (payload_len + *header_len <= buf_size) {
  129. pc->frame_start_found = 0;
  130. pc->state = -1;
  131. return payload_len + *header_len;
  132. }
  133. }
  134. pc->frame_start_found = start_found;
  135. pc->state = state;
  136. return END_NOT_FOUND;
  137. }
  138. return buf_size;
  139. }
  140. static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx,
  141. const uint8_t **poutbuf, int *poutbuf_size,
  142. const uint8_t *buf, int buf_size)
  143. {
  144. OpusParseContext *s = ctx->priv_data;
  145. ParseContext *pc = &s->pc;
  146. int next, header_len;
  147. next = opus_find_frame_end(ctx, avctx, buf, buf_size, &header_len);
  148. if (s->ts_framing && next != AVERROR_INVALIDDATA &&
  149. ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
  150. *poutbuf = NULL;
  151. *poutbuf_size = 0;
  152. return buf_size;
  153. }
  154. if (next == AVERROR_INVALIDDATA){
  155. *poutbuf = NULL;
  156. *poutbuf_size = 0;
  157. return buf_size;
  158. }
  159. *poutbuf = buf + header_len;
  160. *poutbuf_size = buf_size - header_len;
  161. return next;
  162. }
  163. AVCodecParser ff_opus_parser = {
  164. .codec_ids = { AV_CODEC_ID_OPUS },
  165. .priv_data_size = sizeof(OpusParseContext),
  166. .parser_parse = opus_parse,
  167. .parser_close = ff_parse_close
  168. };