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.

240 lines
7.2KB

  1. /*
  2. * IEC 61937 demuxer
  3. * Copyright (c) 2010 Anssi Hannula <anssi.hannula at iki.fi>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * IEC 61937 demuxer, used for compressed data in S/PDIF
  24. * @author Anssi Hannula
  25. */
  26. #include "libavutil/bswap.h"
  27. #include "libavcodec/ac3.h"
  28. #include "libavcodec/adts_parser.h"
  29. #include "avformat.h"
  30. #include "spdif.h"
  31. static int spdif_get_offset_and_codec(AVFormatContext *s,
  32. enum IEC61937DataType data_type,
  33. const char *buf, int *offset,
  34. enum AVCodecID *codec)
  35. {
  36. uint32_t samples;
  37. uint8_t frames;
  38. int ret;
  39. switch (data_type & 0xff) {
  40. case IEC61937_AC3:
  41. *offset = AC3_FRAME_SIZE << 2;
  42. *codec = AV_CODEC_ID_AC3;
  43. break;
  44. case IEC61937_MPEG1_LAYER1:
  45. *offset = spdif_mpeg_pkt_offset[1][0];
  46. *codec = AV_CODEC_ID_MP1;
  47. break;
  48. case IEC61937_MPEG1_LAYER23:
  49. *offset = spdif_mpeg_pkt_offset[1][0];
  50. *codec = AV_CODEC_ID_MP3;
  51. break;
  52. case IEC61937_MPEG2_EXT:
  53. *offset = 4608;
  54. *codec = AV_CODEC_ID_MP3;
  55. break;
  56. case IEC61937_MPEG2_AAC:
  57. ret = av_adts_header_parse(buf, &samples, &frames);
  58. if (ret < 0) {
  59. if (s) /* be silent during a probe */
  60. av_log(s, AV_LOG_ERROR, "Invalid AAC packet in IEC 61937\n");
  61. return ret;
  62. }
  63. *offset = samples << 2;
  64. *codec = AV_CODEC_ID_AAC;
  65. break;
  66. case IEC61937_MPEG2_LAYER1_LSF:
  67. *offset = spdif_mpeg_pkt_offset[0][0];
  68. *codec = AV_CODEC_ID_MP1;
  69. break;
  70. case IEC61937_MPEG2_LAYER2_LSF:
  71. *offset = spdif_mpeg_pkt_offset[0][1];
  72. *codec = AV_CODEC_ID_MP2;
  73. break;
  74. case IEC61937_MPEG2_LAYER3_LSF:
  75. *offset = spdif_mpeg_pkt_offset[0][2];
  76. *codec = AV_CODEC_ID_MP3;
  77. break;
  78. case IEC61937_DTS1:
  79. *offset = 2048;
  80. *codec = AV_CODEC_ID_DTS;
  81. break;
  82. case IEC61937_DTS2:
  83. *offset = 4096;
  84. *codec = AV_CODEC_ID_DTS;
  85. break;
  86. case IEC61937_DTS3:
  87. *offset = 8192;
  88. *codec = AV_CODEC_ID_DTS;
  89. break;
  90. default:
  91. if (s) { /* be silent during a probe */
  92. avpriv_request_sample(s, "Data type 0x%04x in IEC 61937",
  93. data_type);
  94. }
  95. return AVERROR_PATCHWELCOME;
  96. }
  97. return 0;
  98. }
  99. /* Largest offset between bursts we currently handle, i.e. AAC with
  100. samples = 4096 */
  101. #define SPDIF_MAX_OFFSET 16384
  102. static int spdif_probe(AVProbeData *p)
  103. {
  104. const uint8_t *buf = p->buf;
  105. const uint8_t *probe_end = p->buf + FFMIN(2 * SPDIF_MAX_OFFSET, p->buf_size - 1);
  106. const uint8_t *expected_code = buf + 7;
  107. uint32_t state = 0;
  108. int sync_codes = 0;
  109. int consecutive_codes = 0;
  110. int offset;
  111. enum AVCodecID codec;
  112. for (; buf < probe_end; buf++) {
  113. state = (state << 8) | *buf;
  114. if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))
  115. && buf[1] < 0x37) {
  116. sync_codes++;
  117. if (buf == expected_code) {
  118. if (++consecutive_codes >= 2)
  119. return AVPROBE_SCORE_MAX;
  120. } else
  121. consecutive_codes = 0;
  122. if (buf + 4 + AV_AAC_ADTS_HEADER_SIZE > p->buf + p->buf_size)
  123. break;
  124. /* continue probing to find more sync codes */
  125. probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p->buf + p->buf_size - 1);
  126. /* skip directly to the next sync code */
  127. if (!spdif_get_offset_and_codec(NULL, (buf[2] << 8) | buf[1],
  128. &buf[5], &offset, &codec)) {
  129. if (buf + offset >= p->buf + p->buf_size)
  130. break;
  131. expected_code = buf + offset;
  132. buf = expected_code - 7;
  133. }
  134. }
  135. }
  136. if (!sync_codes)
  137. return 0;
  138. if (sync_codes >= 6)
  139. /* good amount of sync codes but with unexpected offsets */
  140. return AVPROBE_SCORE_EXTENSION;
  141. /* some sync codes were found */
  142. return AVPROBE_SCORE_EXTENSION / 4;
  143. }
  144. static int spdif_read_header(AVFormatContext *s)
  145. {
  146. s->ctx_flags |= AVFMTCTX_NOHEADER;
  147. return 0;
  148. }
  149. static int spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
  150. {
  151. AVIOContext *pb = s->pb;
  152. enum IEC61937DataType data_type;
  153. enum AVCodecID codec_id;
  154. uint32_t state = 0;
  155. int pkt_size_bits, offset, ret;
  156. while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
  157. state = (state << 8) | avio_r8(pb);
  158. if (pb->eof_reached)
  159. return AVERROR_EOF;
  160. }
  161. data_type = avio_rl16(pb);
  162. pkt_size_bits = avio_rl16(pb);
  163. if (pkt_size_bits % 16)
  164. avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
  165. ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
  166. if (ret)
  167. return ret;
  168. pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
  169. if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
  170. av_packet_unref(pkt);
  171. return AVERROR_EOF;
  172. }
  173. ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
  174. ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
  175. &offset, &codec_id);
  176. if (ret) {
  177. av_packet_unref(pkt);
  178. return ret;
  179. }
  180. /* skip over the padding to the beginning of the next frame */
  181. avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE);
  182. if (!s->nb_streams) {
  183. /* first packet, create a stream */
  184. AVStream *st = avformat_new_stream(s, NULL);
  185. if (!st) {
  186. av_packet_unref(pkt);
  187. return AVERROR(ENOMEM);
  188. }
  189. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  190. st->codecpar->codec_id = codec_id;
  191. } else if (codec_id != s->streams[0]->codecpar->codec_id) {
  192. avpriv_report_missing_feature(s, "Codec change in IEC 61937");
  193. return AVERROR_PATCHWELCOME;
  194. }
  195. if (!s->bit_rate && s->streams[0]->codecpar->sample_rate)
  196. /* stream bitrate matches 16-bit stereo PCM bitrate for currently
  197. supported codecs */
  198. s->bit_rate = 2 * 16 * s->streams[0]->codecpar->sample_rate;
  199. return 0;
  200. }
  201. AVInputFormat ff_spdif_demuxer = {
  202. .name = "spdif",
  203. .long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
  204. .read_probe = spdif_probe,
  205. .read_header = spdif_read_header,
  206. .read_packet = spdif_read_packet,
  207. .flags = AVFMT_GENERIC_INDEX,
  208. };