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.

380 lines
13KB

  1. /*
  2. * IEC958 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. */
  28. /*
  29. * Terminology used in specification:
  30. * data-burst - IEC958 frame, contains header and encapsuled frame
  31. * burst-preambule - IEC958 frame header, contains 16-bits words named Pa, Pb, Pc and Pd
  32. * burst-payload - encapsuled frame
  33. * Pa, Pb - syncword - 0xF872, 0x4E1F
  34. * Pc - burst-info, contains data-type (bits 0-6), error flag (bit 7), data-type-dependent info (bits 8-12)
  35. * and bitstream number (bits 13-15)
  36. * data-type - determines type of encapsuled frames
  37. * Pd - length code (number of bits or bytes of encapsuled frame - according to data_type)
  38. *
  39. * IEC958 frames at normal usage start every specific count of bytes,
  40. * dependent from data-type (spaces between packets are filled by zeros)
  41. */
  42. #include "avformat.h"
  43. #include "spdif.h"
  44. #include "libavcodec/ac3.h"
  45. #include "libavcodec/dca.h"
  46. #include "libavcodec/aacadtsdec.h"
  47. typedef struct IEC958Context {
  48. enum IEC958DataType data_type; ///< burst info - reference to type of payload of the data-burst
  49. int length_code; ///< length code in bits or bytes, depending on data type
  50. int pkt_offset; ///< data burst repetition period in bytes
  51. uint8_t *buffer; ///< allocated buffer, used for swap bytes
  52. int buffer_size; ///< size of allocated buffer
  53. uint8_t *out_buf; ///< pointer to the outgoing data before byte-swapping
  54. int out_bytes; ///< amount of outgoing bytes
  55. uint8_t *hd_buf; ///< allocated buffer to concatenate hd audio frames
  56. int hd_buf_size; ///< size of the hd audio buffer
  57. int hd_buf_count; ///< number of frames in the hd audio buffer
  58. int hd_buf_filled; ///< amount of bytes in the hd audio buffer
  59. /// function, which generates codec dependent header information.
  60. /// Sets data_type and pkt_offset, and length_code, out_bytes, out_buf if necessary
  61. int (*header_info) (AVFormatContext *s, AVPacket *pkt);
  62. } IEC958Context;
  63. static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
  64. {
  65. IEC958Context *ctx = s->priv_data;
  66. int bitstream_mode = pkt->data[6] & 0x7;
  67. ctx->data_type = IEC958_AC3 | (bitstream_mode << 8);
  68. ctx->pkt_offset = AC3_FRAME_SIZE << 2;
  69. return 0;
  70. }
  71. static int spdif_header_eac3(AVFormatContext *s, AVPacket *pkt)
  72. {
  73. IEC958Context *ctx = s->priv_data;
  74. static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
  75. int repeat = 1;
  76. if ((pkt->data[4] & 0xc0) != 0xc0) /* fscod */
  77. repeat = eac3_repeat[(pkt->data[4] & 0x30) >> 4]; /* numblkscod */
  78. ctx->hd_buf = av_fast_realloc(ctx->hd_buf, &ctx->hd_buf_size, ctx->hd_buf_filled + pkt->size);
  79. if (!ctx->hd_buf)
  80. return AVERROR(ENOMEM);
  81. memcpy(&ctx->hd_buf[ctx->hd_buf_filled], pkt->data, pkt->size);
  82. ctx->hd_buf_filled += pkt->size;
  83. if (++ctx->hd_buf_count < repeat){
  84. ctx->pkt_offset = 0;
  85. return 0;
  86. }
  87. ctx->data_type = IEC958_EAC3;
  88. ctx->pkt_offset = 24576;
  89. ctx->out_buf = ctx->hd_buf;
  90. ctx->out_bytes = ctx->hd_buf_filled;
  91. ctx->length_code = ctx->hd_buf_filled;
  92. ctx->hd_buf_count = 0;
  93. ctx->hd_buf_filled = 0;
  94. return 0;
  95. }
  96. static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
  97. {
  98. IEC958Context *ctx = s->priv_data;
  99. uint32_t syncword_dts = AV_RB32(pkt->data);
  100. int blocks;
  101. switch (syncword_dts) {
  102. case DCA_MARKER_RAW_BE:
  103. blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
  104. break;
  105. case DCA_MARKER_RAW_LE:
  106. blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;
  107. break;
  108. case DCA_MARKER_14B_BE:
  109. blocks =
  110. (((pkt->data[5] & 0x07) << 4) | ((pkt->data[6] & 0x3f) >> 2));
  111. break;
  112. case DCA_MARKER_14B_LE:
  113. blocks =
  114. (((pkt->data[4] & 0x07) << 4) | ((pkt->data[7] & 0x3f) >> 2));
  115. break;
  116. default:
  117. av_log(s, AV_LOG_ERROR, "bad DTS syncword 0x%x\n", syncword_dts);
  118. return -1;
  119. }
  120. blocks++;
  121. switch (blocks) {
  122. case 512 >> 5: ctx->data_type = IEC958_DTS1; break;
  123. case 1024 >> 5: ctx->data_type = IEC958_DTS2; break;
  124. case 2048 >> 5: ctx->data_type = IEC958_DTS3; break;
  125. default:
  126. av_log(s, AV_LOG_ERROR, "%i samples in DTS frame not supported\n",
  127. blocks << 5);
  128. return -1;
  129. }
  130. ctx->pkt_offset = blocks << 7;
  131. return 0;
  132. }
  133. static const enum IEC958DataType mpeg_data_type[2][3] = {
  134. // LAYER1 LAYER2 LAYER3
  135. { IEC958_MPEG2_LAYER1_LSF, IEC958_MPEG2_LAYER2_LSF, IEC958_MPEG2_LAYER3_LSF }, //MPEG2 LSF
  136. { IEC958_MPEG1_LAYER1, IEC958_MPEG1_LAYER23, IEC958_MPEG1_LAYER23 }, //MPEG1
  137. };
  138. static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
  139. {
  140. IEC958Context *ctx = s->priv_data;
  141. int version = (pkt->data[1] >> 3) & 3;
  142. int layer = 3 - ((pkt->data[1] >> 1) & 3);
  143. int extension = pkt->data[2] & 1;
  144. if (layer == 3 || version == 1) {
  145. av_log(s, AV_LOG_ERROR, "Wrong MPEG file format\n");
  146. return -1;
  147. }
  148. av_log(s, AV_LOG_DEBUG, "version: %i layer: %i extension: %i\n", version, layer, extension);
  149. if (version == 2 && extension) {
  150. ctx->data_type = IEC958_MPEG2_EXT;
  151. ctx->pkt_offset = 4608;
  152. } else {
  153. ctx->data_type = mpeg_data_type [version & 1][layer];
  154. ctx->pkt_offset = spdif_mpeg_pkt_offset[version & 1][layer];
  155. }
  156. // TODO Data type dependant info (normal/karaoke, dynamic range control)
  157. return 0;
  158. }
  159. static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
  160. {
  161. IEC958Context *ctx = s->priv_data;
  162. AACADTSHeaderInfo hdr;
  163. GetBitContext gbc;
  164. int ret;
  165. init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);
  166. ret = ff_aac_parse_header(&gbc, &hdr);
  167. if (ret < 0) {
  168. av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n");
  169. return -1;
  170. }
  171. ctx->pkt_offset = hdr.samples << 2;
  172. switch (hdr.num_aac_frames) {
  173. case 1:
  174. ctx->data_type = IEC958_MPEG2_AAC;
  175. break;
  176. case 2:
  177. ctx->data_type = IEC958_MPEG2_AAC_LSF_2048;
  178. break;
  179. case 4:
  180. ctx->data_type = IEC958_MPEG2_AAC_LSF_4096;
  181. break;
  182. default:
  183. av_log(s, AV_LOG_ERROR, "%i samples in AAC frame not supported\n",
  184. hdr.samples);
  185. return -1;
  186. }
  187. //TODO Data type dependent info (LC profile/SBR)
  188. return 0;
  189. }
  190. /*
  191. * It seems Dolby TrueHD frames have to be encapsulated in MAT frames before
  192. * they can be encapsulated in IEC 61937.
  193. * Here we encapsulate 24 TrueHD frames in a single MAT frame, padding them
  194. * to achieve constant rate.
  195. * The actual format of a MAT frame is unknown, but the below seems to work.
  196. * However, it seems it is not actually necessary for the 24 TrueHD frames to
  197. * be in an exact alignment with the MAT frame.
  198. */
  199. #define MAT_FRAME_SIZE 61424
  200. #define TRUEHD_FRAME_OFFSET 2560
  201. #define MAT_MIDDLE_CODE_OFFSET -4
  202. static int spdif_header_truehd(AVFormatContext *s, AVPacket *pkt)
  203. {
  204. IEC958Context *ctx = s->priv_data;
  205. int mat_code_length = 0;
  206. const char mat_end_code[16] = { 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11 };
  207. if (!ctx->hd_buf_count) {
  208. 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 };
  209. mat_code_length = sizeof(mat_start_code) + BURST_HEADER_SIZE;
  210. memcpy(ctx->hd_buf, mat_start_code, sizeof(mat_start_code));
  211. } else if (ctx->hd_buf_count == 12) {
  212. const char mat_middle_code[12] = { 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0 };
  213. mat_code_length = sizeof(mat_middle_code) + MAT_MIDDLE_CODE_OFFSET;
  214. memcpy(&ctx->hd_buf[12 * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + MAT_MIDDLE_CODE_OFFSET],
  215. mat_middle_code, sizeof(mat_middle_code));
  216. }
  217. if (pkt->size > TRUEHD_FRAME_OFFSET - mat_code_length) {
  218. /* if such frames exist, we'd need some more complex logic to
  219. * distribute the TrueHD frames in the MAT frame */
  220. av_log(s, AV_LOG_ERROR, "TrueHD frame too big, %d bytes\n", pkt->size);
  221. av_log_ask_for_sample(s, NULL);
  222. return -1;
  223. }
  224. memcpy(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length],
  225. pkt->data, pkt->size);
  226. memset(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length + pkt->size],
  227. 0, TRUEHD_FRAME_OFFSET - pkt->size - mat_code_length);
  228. if (++ctx->hd_buf_count < 24){
  229. ctx->pkt_offset = 0;
  230. return 0;
  231. }
  232. memcpy(&ctx->hd_buf[MAT_FRAME_SIZE - sizeof(mat_end_code)], mat_end_code, sizeof(mat_end_code));
  233. ctx->hd_buf_count = 0;
  234. ctx->data_type = IEC958_TRUEHD;
  235. ctx->pkt_offset = 61440;
  236. ctx->out_buf = ctx->hd_buf;
  237. ctx->out_bytes = MAT_FRAME_SIZE;
  238. ctx->length_code = MAT_FRAME_SIZE;
  239. return 0;
  240. }
  241. static int spdif_write_header(AVFormatContext *s)
  242. {
  243. IEC958Context *ctx = s->priv_data;
  244. switch (s->streams[0]->codec->codec_id) {
  245. case CODEC_ID_AC3:
  246. ctx->header_info = spdif_header_ac3;
  247. break;
  248. case CODEC_ID_EAC3:
  249. ctx->header_info = spdif_header_eac3;
  250. break;
  251. case CODEC_ID_MP1:
  252. case CODEC_ID_MP2:
  253. case CODEC_ID_MP3:
  254. ctx->header_info = spdif_header_mpeg;
  255. break;
  256. case CODEC_ID_DTS:
  257. ctx->header_info = spdif_header_dts;
  258. break;
  259. case CODEC_ID_AAC:
  260. ctx->header_info = spdif_header_aac;
  261. break;
  262. case CODEC_ID_TRUEHD:
  263. ctx->header_info = spdif_header_truehd;
  264. ctx->hd_buf = av_malloc(MAT_FRAME_SIZE);
  265. if (!ctx->hd_buf)
  266. return AVERROR(ENOMEM);
  267. break;
  268. default:
  269. av_log(s, AV_LOG_ERROR, "codec not supported\n");
  270. return -1;
  271. }
  272. return 0;
  273. }
  274. static int spdif_write_trailer(AVFormatContext *s)
  275. {
  276. IEC958Context *ctx = s->priv_data;
  277. av_freep(&ctx->buffer);
  278. av_freep(&ctx->hd_buf);
  279. return 0;
  280. }
  281. static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  282. {
  283. IEC958Context *ctx = s->priv_data;
  284. int ret, padding;
  285. ctx->out_buf = pkt->data;
  286. ctx->out_bytes = pkt->size;
  287. ctx->length_code = FFALIGN(pkt->size, 2) << 3;
  288. ret = ctx->header_info(s, pkt);
  289. if (ret < 0)
  290. return -1;
  291. if (!ctx->pkt_offset)
  292. return 0;
  293. padding = (ctx->pkt_offset - BURST_HEADER_SIZE - ctx->out_bytes) >> 1;
  294. if (padding < 0) {
  295. av_log(s, AV_LOG_ERROR, "bitrate is too high\n");
  296. return -1;
  297. }
  298. put_le16(s->pb, SYNCWORD1); //Pa
  299. put_le16(s->pb, SYNCWORD2); //Pb
  300. put_le16(s->pb, ctx->data_type); //Pc
  301. put_le16(s->pb, ctx->length_code);//Pd
  302. #if HAVE_BIGENDIAN
  303. put_buffer(s->pb, ctx->out_buf, ctx->out_bytes & ~1);
  304. #else
  305. av_fast_malloc(&ctx->buffer, &ctx->buffer_size, ctx->out_bytes + FF_INPUT_BUFFER_PADDING_SIZE);
  306. if (!ctx->buffer)
  307. return AVERROR(ENOMEM);
  308. ff_spdif_bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)ctx->out_buf, ctx->out_bytes >> 1);
  309. put_buffer(s->pb, ctx->buffer, ctx->out_bytes & ~1);
  310. #endif
  311. if (ctx->out_bytes & 1)
  312. put_be16(s->pb, ctx->out_buf[ctx->out_bytes - 1]);
  313. for (; padding > 0; padding--)
  314. put_be16(s->pb, 0);
  315. av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",
  316. ctx->data_type, ctx->out_bytes, ctx->pkt_offset);
  317. put_flush_packet(s->pb);
  318. return 0;
  319. }
  320. AVOutputFormat spdif_muxer = {
  321. "spdif",
  322. NULL_IF_CONFIG_SMALL("IEC958 - S/PDIF (IEC-61937)"),
  323. NULL,
  324. "spdif",
  325. sizeof(IEC958Context),
  326. CODEC_ID_AC3,
  327. CODEC_ID_NONE,
  328. spdif_write_header,
  329. spdif_write_packet,
  330. spdif_write_trailer,
  331. };