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.

397 lines
13KB

  1. /*
  2. * IEC 61937 muxer
  3. * Copyright (c) 2009 Bartlomiej Wolowiec
  4. * Copyright (c) 2010 Anssi Hannula
  5. * Copyright (c) 2010 Carl Eugen Hoyos
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * IEC-61937 encapsulation of various formats, used by S/PDIF
  26. * @author Bartlomiej Wolowiec
  27. * @author Anssi Hannula
  28. * @author Carl Eugen Hoyos
  29. */
  30. /*
  31. * Terminology used in specification:
  32. * data-burst - IEC61937 frame, contains header and encapsuled frame
  33. * burst-preambule - IEC61937 frame header, contains 16-bits words named Pa, Pb, Pc and Pd
  34. * burst-payload - encapsuled frame
  35. * Pa, Pb - syncword - 0xF872, 0x4E1F
  36. * Pc - burst-info, contains data-type (bits 0-6), error flag (bit 7), data-type-dependent info (bits 8-12)
  37. * and bitstream number (bits 13-15)
  38. * data-type - determines type of encapsuled frames
  39. * Pd - length code (number of bits or bytes of encapsuled frame - according to data_type)
  40. *
  41. * IEC 61937 frames at normal usage start every specific count of bytes,
  42. * dependent from data-type (spaces between packets are filled by zeros)
  43. */
  44. #include "avformat.h"
  45. #include "spdif.h"
  46. #include "libavcodec/ac3.h"
  47. #include "libavcodec/dca.h"
  48. #include "libavcodec/aacadtsdec.h"
  49. typedef struct IEC61937Context {
  50. enum IEC61937DataType data_type;///< burst info - reference to type of payload of the data-burst
  51. int length_code; ///< length code in bits or bytes, depending on data type
  52. int pkt_offset; ///< data burst repetition period in bytes
  53. uint8_t *buffer; ///< allocated buffer, used for swap bytes
  54. int buffer_size; ///< size of allocated buffer
  55. uint8_t *out_buf; ///< pointer to the outgoing data before byte-swapping
  56. int out_bytes; ///< amount of outgoing bytes
  57. int use_preamble; ///< preamble enabled (disabled for exactly pre-padded DTS)
  58. int extra_bswap; ///< extra bswap for payload (for LE DTS => standard BE DTS)
  59. uint8_t *hd_buf; ///< allocated buffer to concatenate hd audio frames
  60. int hd_buf_size; ///< size of the hd audio buffer
  61. int hd_buf_count; ///< number of frames in the hd audio buffer
  62. int hd_buf_filled; ///< amount of bytes in the hd audio buffer
  63. /// function, which generates codec dependent header information.
  64. /// Sets data_type and pkt_offset, and length_code, out_bytes, out_buf if necessary
  65. int (*header_info) (AVFormatContext *s, AVPacket *pkt);
  66. } IEC61937Context;
  67. static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
  68. {
  69. IEC61937Context *ctx = s->priv_data;
  70. int bitstream_mode = pkt->data[5] & 0x7;
  71. ctx->data_type = IEC61937_AC3 | (bitstream_mode << 8);
  72. ctx->pkt_offset = AC3_FRAME_SIZE << 2;
  73. return 0;
  74. }
  75. static int spdif_header_eac3(AVFormatContext *s, AVPacket *pkt)
  76. {
  77. IEC61937Context *ctx = s->priv_data;
  78. static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
  79. int repeat = 1;
  80. if ((pkt->data[4] & 0xc0) != 0xc0) /* fscod */
  81. repeat = eac3_repeat[(pkt->data[4] & 0x30) >> 4]; /* numblkscod */
  82. ctx->hd_buf = av_fast_realloc(ctx->hd_buf, &ctx->hd_buf_size, ctx->hd_buf_filled + pkt->size);
  83. if (!ctx->hd_buf)
  84. return AVERROR(ENOMEM);
  85. memcpy(&ctx->hd_buf[ctx->hd_buf_filled], pkt->data, pkt->size);
  86. ctx->hd_buf_filled += pkt->size;
  87. if (++ctx->hd_buf_count < repeat){
  88. ctx->pkt_offset = 0;
  89. return 0;
  90. }
  91. ctx->data_type = IEC61937_EAC3;
  92. ctx->pkt_offset = 24576;
  93. ctx->out_buf = ctx->hd_buf;
  94. ctx->out_bytes = ctx->hd_buf_filled;
  95. ctx->length_code = ctx->hd_buf_filled;
  96. ctx->hd_buf_count = 0;
  97. ctx->hd_buf_filled = 0;
  98. return 0;
  99. }
  100. static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
  101. {
  102. IEC61937Context *ctx = s->priv_data;
  103. uint32_t syncword_dts = AV_RB32(pkt->data);
  104. int blocks;
  105. switch (syncword_dts) {
  106. case DCA_MARKER_RAW_BE:
  107. blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
  108. break;
  109. case DCA_MARKER_RAW_LE:
  110. blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;
  111. ctx->extra_bswap = 1;
  112. break;
  113. case DCA_MARKER_14B_BE:
  114. blocks =
  115. (((pkt->data[5] & 0x07) << 4) | ((pkt->data[6] & 0x3f) >> 2));
  116. break;
  117. case DCA_MARKER_14B_LE:
  118. blocks =
  119. (((pkt->data[4] & 0x07) << 4) | ((pkt->data[7] & 0x3f) >> 2));
  120. ctx->extra_bswap = 1;
  121. break;
  122. default:
  123. av_log(s, AV_LOG_ERROR, "bad DTS syncword 0x%x\n", syncword_dts);
  124. return AVERROR_INVALIDDATA;
  125. }
  126. blocks++;
  127. switch (blocks) {
  128. case 512 >> 5: ctx->data_type = IEC61937_DTS1; break;
  129. case 1024 >> 5: ctx->data_type = IEC61937_DTS2; break;
  130. case 2048 >> 5: ctx->data_type = IEC61937_DTS3; break;
  131. default:
  132. av_log(s, AV_LOG_ERROR, "%i samples in DTS frame not supported\n",
  133. blocks << 5);
  134. return AVERROR(ENOSYS);
  135. }
  136. ctx->pkt_offset = blocks << 7;
  137. if (ctx->out_bytes == ctx->pkt_offset) {
  138. /* The DTS stream fits exactly into the output stream, so skip the
  139. * preamble as it would not fit in there. This is the case for dts
  140. * discs and dts-in-wav. */
  141. ctx->use_preamble = 0;
  142. }
  143. return 0;
  144. }
  145. static const enum IEC61937DataType mpeg_data_type[2][3] = {
  146. // LAYER1 LAYER2 LAYER3
  147. { IEC61937_MPEG2_LAYER1_LSF, IEC61937_MPEG2_LAYER2_LSF, IEC61937_MPEG2_LAYER3_LSF },//MPEG2 LSF
  148. { IEC61937_MPEG1_LAYER1, IEC61937_MPEG1_LAYER23, IEC61937_MPEG1_LAYER23 }, //MPEG1
  149. };
  150. static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
  151. {
  152. IEC61937Context *ctx = s->priv_data;
  153. int version = (pkt->data[1] >> 3) & 3;
  154. int layer = 3 - ((pkt->data[1] >> 1) & 3);
  155. int extension = pkt->data[2] & 1;
  156. if (layer == 3 || version == 1) {
  157. av_log(s, AV_LOG_ERROR, "Wrong MPEG file format\n");
  158. return AVERROR_INVALIDDATA;
  159. }
  160. av_log(s, AV_LOG_DEBUG, "version: %i layer: %i extension: %i\n", version, layer, extension);
  161. if (version == 2 && extension) {
  162. ctx->data_type = IEC61937_MPEG2_EXT;
  163. ctx->pkt_offset = 4608;
  164. } else {
  165. ctx->data_type = mpeg_data_type [version & 1][layer];
  166. ctx->pkt_offset = spdif_mpeg_pkt_offset[version & 1][layer];
  167. }
  168. // TODO Data type dependant info (normal/karaoke, dynamic range control)
  169. return 0;
  170. }
  171. static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
  172. {
  173. IEC61937Context *ctx = s->priv_data;
  174. AACADTSHeaderInfo hdr;
  175. GetBitContext gbc;
  176. int ret;
  177. init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);
  178. ret = ff_aac_parse_header(&gbc, &hdr);
  179. if (ret < 0) {
  180. av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n");
  181. return AVERROR_INVALIDDATA;
  182. }
  183. ctx->pkt_offset = hdr.samples << 2;
  184. switch (hdr.num_aac_frames) {
  185. case 1:
  186. ctx->data_type = IEC61937_MPEG2_AAC;
  187. break;
  188. case 2:
  189. ctx->data_type = IEC61937_MPEG2_AAC_LSF_2048;
  190. break;
  191. case 4:
  192. ctx->data_type = IEC61937_MPEG2_AAC_LSF_4096;
  193. break;
  194. default:
  195. av_log(s, AV_LOG_ERROR, "%i samples in AAC frame not supported\n",
  196. hdr.samples);
  197. return AVERROR(EINVAL);
  198. }
  199. //TODO Data type dependent info (LC profile/SBR)
  200. return 0;
  201. }
  202. /*
  203. * It seems Dolby TrueHD frames have to be encapsulated in MAT frames before
  204. * they can be encapsulated in IEC 61937.
  205. * Here we encapsulate 24 TrueHD frames in a single MAT frame, padding them
  206. * to achieve constant rate.
  207. * The actual format of a MAT frame is unknown, but the below seems to work.
  208. * However, it seems it is not actually necessary for the 24 TrueHD frames to
  209. * be in an exact alignment with the MAT frame.
  210. */
  211. #define MAT_FRAME_SIZE 61424
  212. #define TRUEHD_FRAME_OFFSET 2560
  213. #define MAT_MIDDLE_CODE_OFFSET -4
  214. static int spdif_header_truehd(AVFormatContext *s, AVPacket *pkt)
  215. {
  216. IEC61937Context *ctx = s->priv_data;
  217. int mat_code_length = 0;
  218. const char mat_end_code[16] = { 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11 };
  219. if (!ctx->hd_buf_count) {
  220. const char mat_start_code[20] = { 0x07, 0x9E, 0x00, 0x03, 0x84, 0x01, 0x01, 0x01, 0x80, 0x00, 0x56, 0xA5, 0x3B, 0xF4, 0x81, 0x83, 0x49, 0x80, 0x77, 0xE0 };
  221. mat_code_length = sizeof(mat_start_code) + BURST_HEADER_SIZE;
  222. memcpy(ctx->hd_buf, mat_start_code, sizeof(mat_start_code));
  223. } else if (ctx->hd_buf_count == 12) {
  224. const char mat_middle_code[12] = { 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0 };
  225. mat_code_length = sizeof(mat_middle_code) + MAT_MIDDLE_CODE_OFFSET;
  226. memcpy(&ctx->hd_buf[12 * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + MAT_MIDDLE_CODE_OFFSET],
  227. mat_middle_code, sizeof(mat_middle_code));
  228. }
  229. if (pkt->size > TRUEHD_FRAME_OFFSET - mat_code_length) {
  230. /* if such frames exist, we'd need some more complex logic to
  231. * distribute the TrueHD frames in the MAT frame */
  232. av_log(s, AV_LOG_ERROR, "TrueHD frame too big, %d bytes\n", pkt->size);
  233. av_log_ask_for_sample(s, NULL);
  234. return AVERROR_INVALIDDATA;
  235. }
  236. memcpy(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length],
  237. pkt->data, pkt->size);
  238. memset(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length + pkt->size],
  239. 0, TRUEHD_FRAME_OFFSET - pkt->size - mat_code_length);
  240. if (++ctx->hd_buf_count < 24){
  241. ctx->pkt_offset = 0;
  242. return 0;
  243. }
  244. memcpy(&ctx->hd_buf[MAT_FRAME_SIZE - sizeof(mat_end_code)], mat_end_code, sizeof(mat_end_code));
  245. ctx->hd_buf_count = 0;
  246. ctx->data_type = IEC61937_TRUEHD;
  247. ctx->pkt_offset = 61440;
  248. ctx->out_buf = ctx->hd_buf;
  249. ctx->out_bytes = MAT_FRAME_SIZE;
  250. ctx->length_code = MAT_FRAME_SIZE;
  251. return 0;
  252. }
  253. static int spdif_write_header(AVFormatContext *s)
  254. {
  255. IEC61937Context *ctx = s->priv_data;
  256. switch (s->streams[0]->codec->codec_id) {
  257. case CODEC_ID_AC3:
  258. ctx->header_info = spdif_header_ac3;
  259. break;
  260. case CODEC_ID_EAC3:
  261. ctx->header_info = spdif_header_eac3;
  262. break;
  263. case CODEC_ID_MP1:
  264. case CODEC_ID_MP2:
  265. case CODEC_ID_MP3:
  266. ctx->header_info = spdif_header_mpeg;
  267. break;
  268. case CODEC_ID_DTS:
  269. ctx->header_info = spdif_header_dts;
  270. break;
  271. case CODEC_ID_AAC:
  272. ctx->header_info = spdif_header_aac;
  273. break;
  274. case CODEC_ID_TRUEHD:
  275. ctx->header_info = spdif_header_truehd;
  276. ctx->hd_buf = av_malloc(MAT_FRAME_SIZE);
  277. if (!ctx->hd_buf)
  278. return AVERROR(ENOMEM);
  279. break;
  280. default:
  281. av_log(s, AV_LOG_ERROR, "codec not supported\n");
  282. return AVERROR_PATCHWELCOME;
  283. }
  284. return 0;
  285. }
  286. static int spdif_write_trailer(AVFormatContext *s)
  287. {
  288. IEC61937Context *ctx = s->priv_data;
  289. av_freep(&ctx->buffer);
  290. av_freep(&ctx->hd_buf);
  291. return 0;
  292. }
  293. static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  294. {
  295. IEC61937Context *ctx = s->priv_data;
  296. int ret, padding;
  297. ctx->out_buf = pkt->data;
  298. ctx->out_bytes = pkt->size;
  299. ctx->length_code = FFALIGN(pkt->size, 2) << 3;
  300. ctx->use_preamble = 1;
  301. ctx->extra_bswap = 0;
  302. ret = ctx->header_info(s, pkt);
  303. if (ret < 0)
  304. return ret;
  305. if (!ctx->pkt_offset)
  306. return 0;
  307. padding = (ctx->pkt_offset - ctx->use_preamble * BURST_HEADER_SIZE - ctx->out_bytes) & ~1;
  308. if (padding < 0) {
  309. av_log(s, AV_LOG_ERROR, "bitrate is too high\n");
  310. return AVERROR(EINVAL);
  311. }
  312. if (ctx->use_preamble) {
  313. put_le16(s->pb, SYNCWORD1); //Pa
  314. put_le16(s->pb, SYNCWORD2); //Pb
  315. put_le16(s->pb, ctx->data_type); //Pc
  316. put_le16(s->pb, ctx->length_code);//Pd
  317. }
  318. if (HAVE_BIGENDIAN ^ ctx->extra_bswap) {
  319. put_buffer(s->pb, ctx->out_buf, ctx->out_bytes & ~1);
  320. } else {
  321. av_fast_malloc(&ctx->buffer, &ctx->buffer_size, ctx->out_bytes + FF_INPUT_BUFFER_PADDING_SIZE);
  322. if (!ctx->buffer)
  323. return AVERROR(ENOMEM);
  324. ff_spdif_bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)ctx->out_buf, ctx->out_bytes >> 1);
  325. put_buffer(s->pb, ctx->buffer, ctx->out_bytes & ~1);
  326. }
  327. if (ctx->out_bytes & 1)
  328. put_be16(s->pb, ctx->out_buf[ctx->out_bytes - 1]);
  329. put_nbyte(s->pb, 0, padding);
  330. av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",
  331. ctx->data_type, ctx->out_bytes, ctx->pkt_offset);
  332. put_flush_packet(s->pb);
  333. return 0;
  334. }
  335. AVOutputFormat spdif_muxer = {
  336. "spdif",
  337. NULL_IF_CONFIG_SMALL("IEC 61937 (used on S/PDIF - IEC958)"),
  338. NULL,
  339. "spdif",
  340. sizeof(IEC61937Context),
  341. CODEC_ID_AC3,
  342. CODEC_ID_NONE,
  343. spdif_write_header,
  344. spdif_write_packet,
  345. spdif_write_trailer,
  346. };