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.

561 lines
20KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; 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-preamble - IEC61937 frame header, contains 16-bit 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 <inttypes.h>
  45. #include "avformat.h"
  46. #include "avio_internal.h"
  47. #include "spdif.h"
  48. #include "libavcodec/ac3.h"
  49. #include "libavcodec/dca.h"
  50. #include "libavcodec/dca_syncwords.h"
  51. #include "libavcodec/aacadtsdec.h"
  52. #include "libavutil/opt.h"
  53. typedef struct IEC61937Context {
  54. const AVClass *av_class;
  55. enum IEC61937DataType data_type;///< burst info - reference to type of payload of the data-burst
  56. int length_code; ///< length code in bits or bytes, depending on data type
  57. int pkt_offset; ///< data burst repetition period in bytes
  58. uint8_t *buffer; ///< allocated buffer, used for swap bytes
  59. int buffer_size; ///< size of allocated buffer
  60. uint8_t *out_buf; ///< pointer to the outgoing data before byte-swapping
  61. int out_bytes; ///< amount of outgoing bytes
  62. int use_preamble; ///< preamble enabled (disabled for exactly pre-padded DTS)
  63. int extra_bswap; ///< extra bswap for payload (for LE DTS => standard BE DTS)
  64. uint8_t *hd_buf; ///< allocated buffer to concatenate hd audio frames
  65. int hd_buf_size; ///< size of the hd audio buffer
  66. int hd_buf_count; ///< number of frames in the hd audio buffer
  67. int hd_buf_filled; ///< amount of bytes in the hd audio buffer
  68. int dtshd_skip; ///< counter used for skipping DTS-HD frames
  69. /* AVOptions: */
  70. int dtshd_rate;
  71. int dtshd_fallback;
  72. #define SPDIF_FLAG_BIGENDIAN 0x01
  73. int spdif_flags;
  74. /// function, which generates codec dependent header information.
  75. /// Sets data_type and pkt_offset, and length_code, out_bytes, out_buf if necessary
  76. int (*header_info) (AVFormatContext *s, AVPacket *pkt);
  77. } IEC61937Context;
  78. static const AVOption options[] = {
  79. { "spdif_flags", "IEC 61937 encapsulation flags", offsetof(IEC61937Context, spdif_flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "spdif_flags" },
  80. { "be", "output in big-endian format (for use as s16be)", 0, AV_OPT_TYPE_CONST, {.i64 = SPDIF_FLAG_BIGENDIAN}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "spdif_flags" },
  81. { "dtshd_rate", "mux complete DTS frames in HD mode at the specified IEC958 rate (in Hz, default 0=disabled)", offsetof(IEC61937Context, dtshd_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 768000, AV_OPT_FLAG_ENCODING_PARAM },
  82. { "dtshd_fallback_time", "min secs to strip HD for after an overflow (-1: till the end, default 60)", offsetof(IEC61937Context, dtshd_fallback), AV_OPT_TYPE_INT, {.i64 = 60}, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  83. { NULL },
  84. };
  85. static const AVClass class = {
  86. .class_name = "spdif",
  87. .item_name = av_default_item_name,
  88. .option = options,
  89. .version = LIBAVUTIL_VERSION_INT,
  90. };
  91. static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
  92. {
  93. IEC61937Context *ctx = s->priv_data;
  94. int bitstream_mode = pkt->data[5] & 0x7;
  95. ctx->data_type = IEC61937_AC3 | (bitstream_mode << 8);
  96. ctx->pkt_offset = AC3_FRAME_SIZE << 2;
  97. return 0;
  98. }
  99. static int spdif_header_eac3(AVFormatContext *s, AVPacket *pkt)
  100. {
  101. IEC61937Context *ctx = s->priv_data;
  102. static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
  103. int repeat = 1;
  104. if ((pkt->data[4] & 0xc0) != 0xc0) /* fscod */
  105. repeat = eac3_repeat[(pkt->data[4] & 0x30) >> 4]; /* numblkscod */
  106. ctx->hd_buf = av_fast_realloc(ctx->hd_buf, &ctx->hd_buf_size, ctx->hd_buf_filled + pkt->size);
  107. if (!ctx->hd_buf)
  108. return AVERROR(ENOMEM);
  109. memcpy(&ctx->hd_buf[ctx->hd_buf_filled], pkt->data, pkt->size);
  110. ctx->hd_buf_filled += pkt->size;
  111. if (++ctx->hd_buf_count < repeat){
  112. ctx->pkt_offset = 0;
  113. return 0;
  114. }
  115. ctx->data_type = IEC61937_EAC3;
  116. ctx->pkt_offset = 24576;
  117. ctx->out_buf = ctx->hd_buf;
  118. ctx->out_bytes = ctx->hd_buf_filled;
  119. ctx->length_code = ctx->hd_buf_filled;
  120. ctx->hd_buf_count = 0;
  121. ctx->hd_buf_filled = 0;
  122. return 0;
  123. }
  124. /*
  125. * DTS type IV (DTS-HD) can be transmitted with various frame repetition
  126. * periods; longer repetition periods allow for longer packets and therefore
  127. * higher bitrate. Longer repetition periods mean that the constant bitrate of
  128. * the output IEC 61937 stream is higher.
  129. * The repetition period is measured in IEC 60958 frames (4 bytes).
  130. */
  131. static int spdif_dts4_subtype(int period)
  132. {
  133. switch (period) {
  134. case 512: return 0x0;
  135. case 1024: return 0x1;
  136. case 2048: return 0x2;
  137. case 4096: return 0x3;
  138. case 8192: return 0x4;
  139. case 16384: return 0x5;
  140. }
  141. return -1;
  142. }
  143. static int spdif_header_dts4(AVFormatContext *s, AVPacket *pkt, int core_size,
  144. int sample_rate, int blocks)
  145. {
  146. IEC61937Context *ctx = s->priv_data;
  147. static const char dtshd_start_code[10] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe };
  148. int pkt_size = pkt->size;
  149. int period;
  150. int subtype;
  151. if (!core_size) {
  152. av_log(s, AV_LOG_ERROR, "HD mode not supported for this format\n");
  153. return AVERROR(EINVAL);
  154. }
  155. if (!sample_rate) {
  156. av_log(s, AV_LOG_ERROR, "Unknown DTS sample rate for HD\n");
  157. return AVERROR_INVALIDDATA;
  158. }
  159. period = ctx->dtshd_rate * (blocks << 5) / sample_rate;
  160. subtype = spdif_dts4_subtype(period);
  161. if (subtype < 0) {
  162. av_log(s, AV_LOG_ERROR, "Specified HD rate of %d Hz would require an "
  163. "impossible repetition period of %d for the current DTS stream"
  164. " (blocks = %d, sample rate = %d)\n", ctx->dtshd_rate, period,
  165. blocks << 5, sample_rate);
  166. return AVERROR(EINVAL);
  167. }
  168. /* set pkt_offset and DTS IV subtype according to the requested output
  169. * rate */
  170. ctx->pkt_offset = period * 4;
  171. ctx->data_type = IEC61937_DTSHD | subtype << 8;
  172. /* If the bitrate is too high for transmitting at the selected
  173. * repetition period setting, strip DTS-HD until a good amount
  174. * of consecutive non-overflowing HD frames have been observed.
  175. * This generally only happens if the caller is cramming a Master
  176. * Audio stream into 192kHz IEC 60958 (which may or may not fit). */
  177. if (sizeof(dtshd_start_code) + 2 + pkt_size
  178. > ctx->pkt_offset - BURST_HEADER_SIZE && core_size) {
  179. if (!ctx->dtshd_skip)
  180. av_log(s, AV_LOG_WARNING, "DTS-HD bitrate too high, "
  181. "temporarily sending core only\n");
  182. if (ctx->dtshd_fallback > 0)
  183. ctx->dtshd_skip = sample_rate * ctx->dtshd_fallback / (blocks << 5);
  184. else
  185. /* skip permanently (dtshd_fallback == -1) or just once
  186. * (dtshd_fallback == 0) */
  187. ctx->dtshd_skip = 1;
  188. }
  189. if (ctx->dtshd_skip && core_size) {
  190. pkt_size = core_size;
  191. if (ctx->dtshd_fallback >= 0)
  192. --ctx->dtshd_skip;
  193. }
  194. ctx->out_bytes = sizeof(dtshd_start_code) + 2 + pkt_size;
  195. /* Align so that (length_code & 0xf) == 0x8. This is reportedly needed
  196. * with some receivers, but the exact requirement is unconfirmed. */
  197. ctx->length_code = FFALIGN(ctx->out_bytes + 0x8, 0x10) - 0x8;
  198. av_fast_malloc(&ctx->hd_buf, &ctx->hd_buf_size, ctx->out_bytes);
  199. if (!ctx->hd_buf)
  200. return AVERROR(ENOMEM);
  201. ctx->out_buf = ctx->hd_buf;
  202. memcpy(ctx->hd_buf, dtshd_start_code, sizeof(dtshd_start_code));
  203. AV_WB16(ctx->hd_buf + sizeof(dtshd_start_code), pkt_size);
  204. memcpy(ctx->hd_buf + sizeof(dtshd_start_code) + 2, pkt->data, pkt_size);
  205. return 0;
  206. }
  207. static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
  208. {
  209. IEC61937Context *ctx = s->priv_data;
  210. uint32_t syncword_dts = AV_RB32(pkt->data);
  211. int blocks;
  212. int sample_rate = 0;
  213. int core_size = 0;
  214. if (pkt->size < 9)
  215. return AVERROR_INVALIDDATA;
  216. switch (syncword_dts) {
  217. case DCA_SYNCWORD_CORE_BE:
  218. blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
  219. core_size = ((AV_RB24(pkt->data + 5) >> 4) & 0x3fff) + 1;
  220. sample_rate = avpriv_dca_sample_rates[(pkt->data[8] >> 2) & 0x0f];
  221. break;
  222. case DCA_SYNCWORD_CORE_LE:
  223. blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;
  224. ctx->extra_bswap = 1;
  225. break;
  226. case DCA_SYNCWORD_CORE_14B_BE:
  227. blocks =
  228. (((pkt->data[5] & 0x07) << 4) | ((pkt->data[6] & 0x3f) >> 2));
  229. break;
  230. case DCA_SYNCWORD_CORE_14B_LE:
  231. blocks =
  232. (((pkt->data[4] & 0x07) << 4) | ((pkt->data[7] & 0x3f) >> 2));
  233. ctx->extra_bswap = 1;
  234. break;
  235. case DCA_SYNCWORD_SUBSTREAM:
  236. /* We only handle HD frames that are paired with core. However,
  237. sometimes DTS-HD streams with core have a stray HD frame without
  238. core in the beginning of the stream. */
  239. av_log(s, AV_LOG_ERROR, "stray DTS-HD frame\n");
  240. return AVERROR_INVALIDDATA;
  241. default:
  242. av_log(s, AV_LOG_ERROR, "bad DTS syncword 0x%"PRIx32"\n", syncword_dts);
  243. return AVERROR_INVALIDDATA;
  244. }
  245. blocks++;
  246. if (ctx->dtshd_rate)
  247. /* DTS type IV output requested */
  248. return spdif_header_dts4(s, pkt, core_size, sample_rate, blocks);
  249. switch (blocks) {
  250. case 512 >> 5: ctx->data_type = IEC61937_DTS1; break;
  251. case 1024 >> 5: ctx->data_type = IEC61937_DTS2; break;
  252. case 2048 >> 5: ctx->data_type = IEC61937_DTS3; break;
  253. default:
  254. av_log(s, AV_LOG_ERROR, "%i samples in DTS frame not supported\n",
  255. blocks << 5);
  256. return AVERROR(ENOSYS);
  257. }
  258. /* discard extraneous data by default */
  259. if (core_size && core_size < pkt->size) {
  260. ctx->out_bytes = core_size;
  261. ctx->length_code = core_size << 3;
  262. }
  263. ctx->pkt_offset = blocks << 7;
  264. if (ctx->out_bytes == ctx->pkt_offset) {
  265. /* The DTS stream fits exactly into the output stream, so skip the
  266. * preamble as it would not fit in there. This is the case for dts
  267. * discs and dts-in-wav. */
  268. ctx->use_preamble = 0;
  269. } else if (ctx->out_bytes > ctx->pkt_offset - BURST_HEADER_SIZE) {
  270. avpriv_request_sample(s, "Unrecognized large DTS frame");
  271. /* This will fail with a "bitrate too high" in the caller */
  272. }
  273. return 0;
  274. }
  275. static const enum IEC61937DataType mpeg_data_type[2][3] = {
  276. // LAYER1 LAYER2 LAYER3
  277. { IEC61937_MPEG2_LAYER1_LSF, IEC61937_MPEG2_LAYER2_LSF, IEC61937_MPEG2_LAYER3_LSF }, // MPEG-2 LSF
  278. { IEC61937_MPEG1_LAYER1, IEC61937_MPEG1_LAYER23, IEC61937_MPEG1_LAYER23 }, // MPEG-1
  279. };
  280. static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
  281. {
  282. IEC61937Context *ctx = s->priv_data;
  283. int version = (pkt->data[1] >> 3) & 3;
  284. int layer = 3 - ((pkt->data[1] >> 1) & 3);
  285. int extension = pkt->data[2] & 1;
  286. if (layer == 3 || version == 1) {
  287. av_log(s, AV_LOG_ERROR, "Wrong MPEG file format\n");
  288. return AVERROR_INVALIDDATA;
  289. }
  290. av_log(s, AV_LOG_DEBUG, "version: %i layer: %i extension: %i\n", version, layer, extension);
  291. if (version == 2 && extension) {
  292. ctx->data_type = IEC61937_MPEG2_EXT;
  293. ctx->pkt_offset = 4608;
  294. } else {
  295. ctx->data_type = mpeg_data_type [version & 1][layer];
  296. ctx->pkt_offset = spdif_mpeg_pkt_offset[version & 1][layer];
  297. }
  298. // TODO Data type dependent info (normal/karaoke, dynamic range control)
  299. return 0;
  300. }
  301. static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
  302. {
  303. IEC61937Context *ctx = s->priv_data;
  304. AACADTSHeaderInfo hdr;
  305. GetBitContext gbc;
  306. int ret;
  307. init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);
  308. ret = avpriv_aac_parse_header(&gbc, &hdr);
  309. if (ret < 0) {
  310. av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n");
  311. return AVERROR_INVALIDDATA;
  312. }
  313. ctx->pkt_offset = hdr.samples << 2;
  314. switch (hdr.num_aac_frames) {
  315. case 1:
  316. ctx->data_type = IEC61937_MPEG2_AAC;
  317. break;
  318. case 2:
  319. ctx->data_type = IEC61937_MPEG2_AAC_LSF_2048;
  320. break;
  321. case 4:
  322. ctx->data_type = IEC61937_MPEG2_AAC_LSF_4096;
  323. break;
  324. default:
  325. av_log(s, AV_LOG_ERROR,
  326. "%"PRIu32" samples in AAC frame not supported\n", hdr.samples);
  327. return AVERROR(EINVAL);
  328. }
  329. //TODO Data type dependent info (LC profile/SBR)
  330. return 0;
  331. }
  332. /*
  333. * It seems Dolby TrueHD frames have to be encapsulated in MAT frames before
  334. * they can be encapsulated in IEC 61937.
  335. * Here we encapsulate 24 TrueHD frames in a single MAT frame, padding them
  336. * to achieve constant rate.
  337. * The actual format of a MAT frame is unknown, but the below seems to work.
  338. * However, it seems it is not actually necessary for the 24 TrueHD frames to
  339. * be in an exact alignment with the MAT frame.
  340. */
  341. #define MAT_FRAME_SIZE 61424
  342. #define TRUEHD_FRAME_OFFSET 2560
  343. #define MAT_MIDDLE_CODE_OFFSET -4
  344. static int spdif_header_truehd(AVFormatContext *s, AVPacket *pkt)
  345. {
  346. IEC61937Context *ctx = s->priv_data;
  347. int mat_code_length = 0;
  348. static const char mat_end_code[16] = { 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11 };
  349. if (!ctx->hd_buf_count) {
  350. static 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 };
  351. mat_code_length = sizeof(mat_start_code) + BURST_HEADER_SIZE;
  352. memcpy(ctx->hd_buf, mat_start_code, sizeof(mat_start_code));
  353. } else if (ctx->hd_buf_count == 12) {
  354. static const char mat_middle_code[12] = { 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0 };
  355. mat_code_length = sizeof(mat_middle_code) + MAT_MIDDLE_CODE_OFFSET;
  356. memcpy(&ctx->hd_buf[12 * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + MAT_MIDDLE_CODE_OFFSET],
  357. mat_middle_code, sizeof(mat_middle_code));
  358. }
  359. if (pkt->size > TRUEHD_FRAME_OFFSET - mat_code_length) {
  360. /* if such frames exist, we'd need some more complex logic to
  361. * distribute the TrueHD frames in the MAT frame */
  362. avpriv_request_sample(s, "Too large TrueHD frame of %d bytes",
  363. pkt->size);
  364. return AVERROR_PATCHWELCOME;
  365. }
  366. memcpy(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length],
  367. pkt->data, pkt->size);
  368. memset(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length + pkt->size],
  369. 0, TRUEHD_FRAME_OFFSET - pkt->size - mat_code_length);
  370. if (++ctx->hd_buf_count < 24){
  371. ctx->pkt_offset = 0;
  372. return 0;
  373. }
  374. memcpy(&ctx->hd_buf[MAT_FRAME_SIZE - sizeof(mat_end_code)], mat_end_code, sizeof(mat_end_code));
  375. ctx->hd_buf_count = 0;
  376. ctx->data_type = IEC61937_TRUEHD;
  377. ctx->pkt_offset = 61440;
  378. ctx->out_buf = ctx->hd_buf;
  379. ctx->out_bytes = MAT_FRAME_SIZE;
  380. ctx->length_code = MAT_FRAME_SIZE;
  381. return 0;
  382. }
  383. static int spdif_write_header(AVFormatContext *s)
  384. {
  385. IEC61937Context *ctx = s->priv_data;
  386. switch (s->streams[0]->codecpar->codec_id) {
  387. case AV_CODEC_ID_AC3:
  388. ctx->header_info = spdif_header_ac3;
  389. break;
  390. case AV_CODEC_ID_EAC3:
  391. ctx->header_info = spdif_header_eac3;
  392. break;
  393. case AV_CODEC_ID_MP1:
  394. case AV_CODEC_ID_MP2:
  395. case AV_CODEC_ID_MP3:
  396. ctx->header_info = spdif_header_mpeg;
  397. break;
  398. case AV_CODEC_ID_DTS:
  399. ctx->header_info = spdif_header_dts;
  400. break;
  401. case AV_CODEC_ID_AAC:
  402. ctx->header_info = spdif_header_aac;
  403. break;
  404. case AV_CODEC_ID_TRUEHD:
  405. ctx->header_info = spdif_header_truehd;
  406. ctx->hd_buf = av_malloc(MAT_FRAME_SIZE);
  407. if (!ctx->hd_buf)
  408. return AVERROR(ENOMEM);
  409. break;
  410. default:
  411. avpriv_report_missing_feature(s, "Codec %d",
  412. s->streams[0]->codecpar->codec_id);
  413. return AVERROR_PATCHWELCOME;
  414. }
  415. return 0;
  416. }
  417. static int spdif_write_trailer(AVFormatContext *s)
  418. {
  419. IEC61937Context *ctx = s->priv_data;
  420. av_freep(&ctx->buffer);
  421. av_freep(&ctx->hd_buf);
  422. return 0;
  423. }
  424. static av_always_inline void spdif_put_16(IEC61937Context *ctx,
  425. AVIOContext *pb, unsigned int val)
  426. {
  427. if (ctx->spdif_flags & SPDIF_FLAG_BIGENDIAN)
  428. avio_wb16(pb, val);
  429. else
  430. avio_wl16(pb, val);
  431. }
  432. static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  433. {
  434. IEC61937Context *ctx = s->priv_data;
  435. int ret, padding;
  436. ctx->out_buf = pkt->data;
  437. ctx->out_bytes = pkt->size;
  438. ctx->length_code = FFALIGN(pkt->size, 2) << 3;
  439. ctx->use_preamble = 1;
  440. ctx->extra_bswap = 0;
  441. ret = ctx->header_info(s, pkt);
  442. if (ret < 0)
  443. return ret;
  444. if (!ctx->pkt_offset)
  445. return 0;
  446. padding = (ctx->pkt_offset - ctx->use_preamble * BURST_HEADER_SIZE - ctx->out_bytes) & ~1;
  447. if (padding < 0) {
  448. av_log(s, AV_LOG_ERROR, "bitrate is too high\n");
  449. return AVERROR(EINVAL);
  450. }
  451. if (ctx->use_preamble) {
  452. spdif_put_16(ctx, s->pb, SYNCWORD1); //Pa
  453. spdif_put_16(ctx, s->pb, SYNCWORD2); //Pb
  454. spdif_put_16(ctx, s->pb, ctx->data_type); //Pc
  455. spdif_put_16(ctx, s->pb, ctx->length_code);//Pd
  456. }
  457. if (ctx->extra_bswap ^ (ctx->spdif_flags & SPDIF_FLAG_BIGENDIAN)) {
  458. avio_write(s->pb, ctx->out_buf, ctx->out_bytes & ~1);
  459. } else {
  460. av_fast_malloc(&ctx->buffer, &ctx->buffer_size, ctx->out_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
  461. if (!ctx->buffer)
  462. return AVERROR(ENOMEM);
  463. ff_spdif_bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)ctx->out_buf, ctx->out_bytes >> 1);
  464. avio_write(s->pb, ctx->buffer, ctx->out_bytes & ~1);
  465. }
  466. /* a final lone byte has to be MSB aligned */
  467. if (ctx->out_bytes & 1)
  468. spdif_put_16(ctx, s->pb, ctx->out_buf[ctx->out_bytes - 1] << 8);
  469. ffio_fill(s->pb, 0, padding);
  470. av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",
  471. ctx->data_type, ctx->out_bytes, ctx->pkt_offset);
  472. return 0;
  473. }
  474. AVOutputFormat ff_spdif_muxer = {
  475. .name = "spdif",
  476. .long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (used on S/PDIF - IEC958)"),
  477. .extensions = "spdif",
  478. .priv_data_size = sizeof(IEC61937Context),
  479. .audio_codec = AV_CODEC_ID_AC3,
  480. .video_codec = AV_CODEC_ID_NONE,
  481. .write_header = spdif_write_header,
  482. .write_packet = spdif_write_packet,
  483. .write_trailer = spdif_write_trailer,
  484. .flags = AVFMT_NOTIMESTAMPS,
  485. .priv_class = &class,
  486. };