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.

189 lines
5.5KB

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