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.

245 lines
7.3KB

  1. /*
  2. * IEC 61937 demuxer
  3. * Copyright (c) 2010 Anssi Hannula <anssi.hannula at iki.fi>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. enum AVCodecID codec;
  105. return ff_spdif_probe (p->buf, p->buf_size, &codec);
  106. }
  107. int ff_spdif_probe(const uint8_t *p_buf, int buf_size, enum AVCodecID *codec)
  108. {
  109. const uint8_t *buf = p_buf;
  110. const uint8_t *probe_end = p_buf + FFMIN(2 * SPDIF_MAX_OFFSET, buf_size - 1);
  111. const uint8_t *expected_code = buf + 7;
  112. uint32_t state = 0;
  113. int sync_codes = 0;
  114. int consecutive_codes = 0;
  115. int offset;
  116. for (; buf < probe_end; buf++) {
  117. state = (state << 8) | *buf;
  118. if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))
  119. && buf[1] < 0x37) {
  120. sync_codes++;
  121. if (buf == expected_code) {
  122. if (++consecutive_codes >= 2)
  123. return AVPROBE_SCORE_MAX;
  124. } else
  125. consecutive_codes = 0;
  126. if (buf + 4 + AV_AAC_ADTS_HEADER_SIZE > p_buf + buf_size)
  127. break;
  128. /* continue probing to find more sync codes */
  129. probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p_buf + buf_size - 1);
  130. /* skip directly to the next sync code */
  131. if (!spdif_get_offset_and_codec(NULL, (buf[2] << 8) | buf[1],
  132. &buf[5], &offset, codec)) {
  133. if (buf + offset >= p_buf + buf_size)
  134. break;
  135. expected_code = buf + offset;
  136. buf = expected_code - 7;
  137. }
  138. }
  139. }
  140. if (!sync_codes)
  141. return 0;
  142. if (sync_codes >= 6)
  143. /* good amount of sync codes but with unexpected offsets */
  144. return AVPROBE_SCORE_EXTENSION;
  145. /* some sync codes were found */
  146. return AVPROBE_SCORE_EXTENSION / 4;
  147. }
  148. static int spdif_read_header(AVFormatContext *s)
  149. {
  150. s->ctx_flags |= AVFMTCTX_NOHEADER;
  151. return 0;
  152. }
  153. int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
  154. {
  155. AVIOContext *pb = s->pb;
  156. enum IEC61937DataType data_type;
  157. enum AVCodecID codec_id;
  158. uint32_t state = 0;
  159. int pkt_size_bits, offset, ret;
  160. while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
  161. state = (state << 8) | avio_r8(pb);
  162. if (avio_feof(pb))
  163. return AVERROR_EOF;
  164. }
  165. data_type = avio_rl16(pb);
  166. pkt_size_bits = avio_rl16(pb);
  167. if (pkt_size_bits % 16)
  168. avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
  169. ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
  170. if (ret)
  171. return ret;
  172. pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
  173. if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
  174. av_packet_unref(pkt);
  175. return AVERROR_EOF;
  176. }
  177. ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
  178. ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
  179. &offset, &codec_id);
  180. if (ret) {
  181. av_packet_unref(pkt);
  182. return ret;
  183. }
  184. /* skip over the padding to the beginning of the next frame */
  185. avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE);
  186. if (!s->nb_streams) {
  187. /* first packet, create a stream */
  188. AVStream *st = avformat_new_stream(s, NULL);
  189. if (!st) {
  190. av_packet_unref(pkt);
  191. return AVERROR(ENOMEM);
  192. }
  193. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  194. st->codecpar->codec_id = codec_id;
  195. } else if (codec_id != s->streams[0]->codecpar->codec_id) {
  196. avpriv_report_missing_feature(s, "Codec change in IEC 61937");
  197. return AVERROR_PATCHWELCOME;
  198. }
  199. if (!s->bit_rate && s->streams[0]->codecpar->sample_rate)
  200. /* stream bitrate matches 16-bit stereo PCM bitrate for currently
  201. supported codecs */
  202. s->bit_rate = 2 * 16 * s->streams[0]->codecpar->sample_rate;
  203. return 0;
  204. }
  205. AVInputFormat ff_spdif_demuxer = {
  206. .name = "spdif",
  207. .long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
  208. .read_probe = spdif_probe,
  209. .read_header = spdif_read_header,
  210. .read_packet = ff_spdif_read_packet,
  211. .flags = AVFMT_GENERIC_INDEX,
  212. };