spdifdec.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "avformat.h"
  27. #include "spdif.h"
  28. #include "libavcodec/ac3.h"
  29. #include "libavcodec/aacadtsdec.h"
  30. static int spdif_get_offset_and_codec(AVFormatContext *s,
  31. enum IEC61937DataType data_type,
  32. const char *buf, int *offset,
  33. enum CodecID *codec)
  34. {
  35. AACADTSHeaderInfo aac_hdr;
  36. GetBitContext gbc;
  37. switch (data_type & 0xff) {
  38. case IEC61937_AC3:
  39. *offset = AC3_FRAME_SIZE << 2;
  40. *codec = CODEC_ID_AC3;
  41. break;
  42. case IEC61937_MPEG1_LAYER1:
  43. *offset = spdif_mpeg_pkt_offset[1][0];
  44. *codec = CODEC_ID_MP1;
  45. break;
  46. case IEC61937_MPEG1_LAYER23:
  47. *offset = spdif_mpeg_pkt_offset[1][0];
  48. *codec = CODEC_ID_MP3;
  49. break;
  50. case IEC61937_MPEG2_EXT:
  51. *offset = 4608;
  52. *codec = CODEC_ID_MP3;
  53. break;
  54. case IEC61937_MPEG2_AAC:
  55. init_get_bits(&gbc, buf, AAC_ADTS_HEADER_SIZE * 8);
  56. if (ff_aac_parse_header(&gbc, &aac_hdr)) {
  57. if (s) /* be silent during a probe */
  58. av_log(s, AV_LOG_ERROR, "Invalid AAC packet in IEC 61937\n");
  59. return AVERROR_INVALIDDATA;
  60. }
  61. *offset = aac_hdr.samples << 2;
  62. *codec = CODEC_ID_AAC;
  63. break;
  64. case IEC61937_MPEG2_LAYER1_LSF:
  65. *offset = spdif_mpeg_pkt_offset[0][0];
  66. *codec = CODEC_ID_MP1;
  67. break;
  68. case IEC61937_MPEG2_LAYER2_LSF:
  69. *offset = spdif_mpeg_pkt_offset[0][1];
  70. *codec = CODEC_ID_MP2;
  71. break;
  72. case IEC61937_MPEG2_LAYER3_LSF:
  73. *offset = spdif_mpeg_pkt_offset[0][2];
  74. *codec = CODEC_ID_MP3;
  75. break;
  76. case IEC61937_DTS1:
  77. *offset = 2048;
  78. *codec = CODEC_ID_DTS;
  79. break;
  80. case IEC61937_DTS2:
  81. *offset = 4096;
  82. *codec = CODEC_ID_DTS;
  83. break;
  84. case IEC61937_DTS3:
  85. *offset = 8192;
  86. *codec = CODEC_ID_DTS;
  87. break;
  88. default:
  89. if (s) { /* be silent during a probe */
  90. av_log(s, AV_LOG_WARNING, "Data type 0x%04x", data_type);
  91. av_log_missing_feature(s, " in IEC 61937 is", 1);
  92. }
  93. return AVERROR_PATCHWELCOME;
  94. }
  95. return 0;
  96. }
  97. /* Largest offset between bursts we currently handle, i.e. AAC with
  98. aac_hdr.samples = 4096 */
  99. #define SPDIF_MAX_OFFSET 16384
  100. static int spdif_probe(AVProbeData *p)
  101. {
  102. const uint8_t *buf = p->buf;
  103. const uint8_t *probe_end = p->buf + FFMIN(2 * SPDIF_MAX_OFFSET, p->buf_size - 1);
  104. const uint8_t *expected_code = buf + 7;
  105. uint32_t state = 0;
  106. int sync_codes = 0;
  107. int consecutive_codes = 0;
  108. int offset;
  109. enum CodecID codec;
  110. for (; buf < probe_end; buf++) {
  111. state = (state << 8) | *buf;
  112. if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))
  113. && buf[1] < 0x37) {
  114. sync_codes++;
  115. if (buf == expected_code) {
  116. if (++consecutive_codes >= 2)
  117. return AVPROBE_SCORE_MAX;
  118. } else
  119. consecutive_codes = 0;
  120. if (buf + 4 + AAC_ADTS_HEADER_SIZE > p->buf + p->buf_size)
  121. break;
  122. /* continue probing to find more sync codes */
  123. probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p->buf + p->buf_size - 1);
  124. /* skip directly to the next sync code */
  125. if (!spdif_get_offset_and_codec(NULL, (buf[2] << 8) | buf[1],
  126. &buf[5], &offset, &codec)) {
  127. if (buf + offset >= p->buf + p->buf_size)
  128. break;
  129. expected_code = buf + offset;
  130. buf = expected_code - 7;
  131. }
  132. }
  133. }
  134. if (!sync_codes)
  135. return 0;
  136. if (sync_codes >= 6)
  137. /* good amount of sync codes but with unexpected offsets */
  138. return AVPROBE_SCORE_MAX / 2;
  139. /* some sync codes were found */
  140. return AVPROBE_SCORE_MAX / 8;
  141. }
  142. static int spdif_read_header(AVFormatContext *s, AVFormatParameters *ap)
  143. {
  144. s->ctx_flags |= AVFMTCTX_NOHEADER;
  145. return 0;
  146. }
  147. static int spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
  148. {
  149. AVIOContext *pb = s->pb;
  150. enum IEC61937DataType data_type;
  151. enum CodecID codec_id;
  152. uint32_t state = 0;
  153. int pkt_size_bits, offset, ret;
  154. while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
  155. state = (state << 8) | avio_r8(pb);
  156. if (url_feof(pb))
  157. return AVERROR_EOF;
  158. }
  159. data_type = avio_rl16(pb);
  160. pkt_size_bits = avio_rl16(pb);
  161. if (pkt_size_bits % 16)
  162. av_log_ask_for_sample(s, "Packet does not end to a 16-bit boundary.");
  163. ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
  164. if (ret)
  165. return ret;
  166. pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
  167. if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
  168. av_free_packet(pkt);
  169. return AVERROR_EOF;
  170. }
  171. ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
  172. ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
  173. &offset, &codec_id);
  174. if (ret) {
  175. av_free_packet(pkt);
  176. return ret;
  177. }
  178. /* skip over the padding to the beginning of the next frame */
  179. avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE);
  180. if (!s->nb_streams) {
  181. /* first packet, create a stream */
  182. AVStream *st = av_new_stream(s, 0);
  183. if (!st) {
  184. av_free_packet(pkt);
  185. return AVERROR(ENOMEM);
  186. }
  187. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  188. st->codec->codec_id = codec_id;
  189. } else if (codec_id != s->streams[0]->codec->codec_id) {
  190. av_log_missing_feature(s, "codec change in IEC 61937", 0);
  191. return AVERROR_PATCHWELCOME;
  192. }
  193. if (!s->bit_rate && s->streams[0]->codec->sample_rate)
  194. /* stream bitrate matches 16-bit stereo PCM bitrate for currently
  195. supported codecs */
  196. s->bit_rate = 2 * 16 * s->streams[0]->codec->sample_rate;
  197. return 0;
  198. }
  199. AVInputFormat ff_spdif_demuxer = {
  200. .name = "spdif",
  201. .long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
  202. .read_probe = spdif_probe,
  203. .read_header = spdif_read_header,
  204. .read_packet = spdif_read_packet,
  205. .flags = AVFMT_GENERIC_INDEX,
  206. };