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.

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