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.

567 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 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-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/adts_parser.h"
  50. #include "libavcodec/dca.h"
  51. #include "libavcodec/dca_syncwords.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 spdif_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. int bsid = pkt->data[5] >> 3;
  105. if (bsid > 10 && (pkt->data[4] & 0xc0) != 0xc0) /* fscod */
  106. repeat = eac3_repeat[(pkt->data[4] & 0x30) >> 4]; /* numblkscod */
  107. ctx->hd_buf = av_fast_realloc(ctx->hd_buf, &ctx->hd_buf_size, ctx->hd_buf_filled + pkt->size);
  108. if (!ctx->hd_buf)
  109. return AVERROR(ENOMEM);
  110. memcpy(&ctx->hd_buf[ctx->hd_buf_filled], pkt->data, pkt->size);
  111. ctx->hd_buf_filled += pkt->size;
  112. if (++ctx->hd_buf_count < repeat){
  113. ctx->pkt_offset = 0;
  114. return 0;
  115. }
  116. ctx->data_type = IEC61937_EAC3;
  117. ctx->pkt_offset = 24576;
  118. ctx->out_buf = ctx->hd_buf;
  119. ctx->out_bytes = ctx->hd_buf_filled;
  120. ctx->length_code = ctx->hd_buf_filled;
  121. ctx->hd_buf_count = 0;
  122. ctx->hd_buf_filled = 0;
  123. return 0;
  124. }
  125. /*
  126. * DTS type IV (DTS-HD) can be transmitted with various frame repetition
  127. * periods; longer repetition periods allow for longer packets and therefore
  128. * higher bitrate. Longer repetition periods mean that the constant bitrate of
  129. * the output IEC 61937 stream is higher.
  130. * The repetition period is measured in IEC 60958 frames (4 bytes).
  131. */
  132. static int spdif_dts4_subtype(int period)
  133. {
  134. switch (period) {
  135. case 512: return 0x0;
  136. case 1024: return 0x1;
  137. case 2048: return 0x2;
  138. case 4096: return 0x3;
  139. case 8192: return 0x4;
  140. case 16384: return 0x5;
  141. }
  142. return -1;
  143. }
  144. static int spdif_header_dts4(AVFormatContext *s, AVPacket *pkt, int core_size,
  145. int sample_rate, int blocks)
  146. {
  147. IEC61937Context *ctx = s->priv_data;
  148. static const char dtshd_start_code[10] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe };
  149. int pkt_size = pkt->size;
  150. int period;
  151. int subtype;
  152. if (!core_size) {
  153. av_log(s, AV_LOG_ERROR, "HD mode not supported for this format\n");
  154. return AVERROR(EINVAL);
  155. }
  156. if (!sample_rate) {
  157. av_log(s, AV_LOG_ERROR, "Unknown DTS sample rate for HD\n");
  158. return AVERROR_INVALIDDATA;
  159. }
  160. period = ctx->dtshd_rate * (blocks << 5) / sample_rate;
  161. subtype = spdif_dts4_subtype(period);
  162. if (subtype < 0) {
  163. av_log(s, AV_LOG_ERROR, "Specified HD rate of %d Hz would require an "
  164. "impossible repetition period of %d for the current DTS stream"
  165. " (blocks = %d, sample rate = %d)\n", ctx->dtshd_rate, period,
  166. blocks << 5, sample_rate);
  167. return AVERROR(EINVAL);
  168. }
  169. /* set pkt_offset and DTS IV subtype according to the requested output
  170. * rate */
  171. ctx->pkt_offset = period * 4;
  172. ctx->data_type = IEC61937_DTSHD | subtype << 8;
  173. /* If the bitrate is too high for transmitting at the selected
  174. * repetition period setting, strip DTS-HD until a good amount
  175. * of consecutive non-overflowing HD frames have been observed.
  176. * This generally only happens if the caller is cramming a Master
  177. * Audio stream into 192kHz IEC 60958 (which may or may not fit). */
  178. if (sizeof(dtshd_start_code) + 2 + pkt_size
  179. > ctx->pkt_offset - BURST_HEADER_SIZE && core_size) {
  180. if (!ctx->dtshd_skip)
  181. av_log(s, AV_LOG_WARNING, "DTS-HD bitrate too high, "
  182. "temporarily sending core only\n");
  183. if (ctx->dtshd_fallback > 0)
  184. ctx->dtshd_skip = sample_rate * ctx->dtshd_fallback / (blocks << 5);
  185. else
  186. /* skip permanently (dtshd_fallback == -1) or just once
  187. * (dtshd_fallback == 0) */
  188. ctx->dtshd_skip = 1;
  189. }
  190. if (ctx->dtshd_skip && core_size) {
  191. pkt_size = core_size;
  192. if (ctx->dtshd_fallback >= 0)
  193. --ctx->dtshd_skip;
  194. }
  195. ctx->out_bytes = sizeof(dtshd_start_code) + 2 + pkt_size;
  196. /* Align so that (length_code & 0xf) == 0x8. This is reportedly needed
  197. * with some receivers, but the exact requirement is unconfirmed. */
  198. ctx->length_code = FFALIGN(ctx->out_bytes + 0x8, 0x10) - 0x8;
  199. av_fast_malloc(&ctx->hd_buf, &ctx->hd_buf_size, ctx->out_bytes);
  200. if (!ctx->hd_buf)
  201. return AVERROR(ENOMEM);
  202. ctx->out_buf = ctx->hd_buf;
  203. memcpy(ctx->hd_buf, dtshd_start_code, sizeof(dtshd_start_code));
  204. AV_WB16(ctx->hd_buf + sizeof(dtshd_start_code), pkt_size);
  205. memcpy(ctx->hd_buf + sizeof(dtshd_start_code) + 2, pkt->data, pkt_size);
  206. return 0;
  207. }
  208. static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
  209. {
  210. IEC61937Context *ctx = s->priv_data;
  211. uint32_t syncword_dts = AV_RB32(pkt->data);
  212. int blocks;
  213. int sample_rate = 0;
  214. int core_size = 0;
  215. if (pkt->size < 9)
  216. return AVERROR_INVALIDDATA;
  217. switch (syncword_dts) {
  218. case DCA_SYNCWORD_CORE_BE:
  219. blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
  220. core_size = ((AV_RB24(pkt->data + 5) >> 4) & 0x3fff) + 1;
  221. sample_rate = avpriv_dca_sample_rates[(pkt->data[8] >> 2) & 0x0f];
  222. break;
  223. case DCA_SYNCWORD_CORE_LE:
  224. blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;
  225. ctx->extra_bswap = 1;
  226. break;
  227. case DCA_SYNCWORD_CORE_14B_BE:
  228. blocks =
  229. (((pkt->data[5] & 0x07) << 4) | ((pkt->data[6] & 0x3f) >> 2));
  230. break;
  231. case DCA_SYNCWORD_CORE_14B_LE:
  232. blocks =
  233. (((pkt->data[4] & 0x07) << 4) | ((pkt->data[7] & 0x3f) >> 2));
  234. ctx->extra_bswap = 1;
  235. break;
  236. case DCA_SYNCWORD_SUBSTREAM:
  237. /* We only handle HD frames that are paired with core. However,
  238. sometimes DTS-HD streams with core have a stray HD frame without
  239. core in the beginning of the stream. */
  240. av_log(s, AV_LOG_ERROR, "stray DTS-HD frame\n");
  241. return AVERROR_INVALIDDATA;
  242. default:
  243. av_log(s, AV_LOG_ERROR, "bad DTS syncword 0x%"PRIx32"\n", syncword_dts);
  244. return AVERROR_INVALIDDATA;
  245. }
  246. blocks++;
  247. if (ctx->dtshd_rate)
  248. /* DTS type IV output requested */
  249. return spdif_header_dts4(s, pkt, core_size, sample_rate, blocks);
  250. switch (blocks) {
  251. case 512 >> 5: ctx->data_type = IEC61937_DTS1; break;
  252. case 1024 >> 5: ctx->data_type = IEC61937_DTS2; break;
  253. case 2048 >> 5: ctx->data_type = IEC61937_DTS3; break;
  254. default:
  255. av_log(s, AV_LOG_ERROR, "%i samples in DTS frame not supported\n",
  256. blocks << 5);
  257. return AVERROR(ENOSYS);
  258. }
  259. /* discard extraneous data by default */
  260. if (core_size && core_size < pkt->size) {
  261. ctx->out_bytes = core_size;
  262. ctx->length_code = core_size << 3;
  263. }
  264. ctx->pkt_offset = blocks << 7;
  265. if (ctx->out_bytes == ctx->pkt_offset) {
  266. /* The DTS stream fits exactly into the output stream, so skip the
  267. * preamble as it would not fit in there. This is the case for dts
  268. * discs and dts-in-wav. */
  269. ctx->use_preamble = 0;
  270. } else if (ctx->out_bytes > ctx->pkt_offset - BURST_HEADER_SIZE) {
  271. avpriv_request_sample(s, "Unrecognized large DTS frame");
  272. /* This will fail with a "bitrate too high" in the caller */
  273. }
  274. return 0;
  275. }
  276. static const enum IEC61937DataType mpeg_data_type[2][3] = {
  277. // LAYER1 LAYER2 LAYER3
  278. { IEC61937_MPEG2_LAYER1_LSF, IEC61937_MPEG2_LAYER2_LSF, IEC61937_MPEG2_LAYER3_LSF }, // MPEG-2 LSF
  279. { IEC61937_MPEG1_LAYER1, IEC61937_MPEG1_LAYER23, IEC61937_MPEG1_LAYER23 }, // MPEG-1
  280. };
  281. static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
  282. {
  283. IEC61937Context *ctx = s->priv_data;
  284. int version = (pkt->data[1] >> 3) & 3;
  285. int layer = 3 - ((pkt->data[1] >> 1) & 3);
  286. int extension = pkt->data[2] & 1;
  287. if (layer == 3 || version == 1) {
  288. av_log(s, AV_LOG_ERROR, "Wrong MPEG file format\n");
  289. return AVERROR_INVALIDDATA;
  290. }
  291. av_log(s, AV_LOG_DEBUG, "version: %i layer: %i extension: %i\n", version, layer, extension);
  292. if (version == 2 && extension) {
  293. ctx->data_type = IEC61937_MPEG2_EXT;
  294. ctx->pkt_offset = 4608;
  295. } else {
  296. ctx->data_type = mpeg_data_type [version & 1][layer];
  297. ctx->pkt_offset = spdif_mpeg_pkt_offset[version & 1][layer];
  298. }
  299. // TODO Data type dependent info (normal/karaoke, dynamic range control)
  300. return 0;
  301. }
  302. static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
  303. {
  304. IEC61937Context *ctx = s->priv_data;
  305. uint32_t samples;
  306. uint8_t frames;
  307. int ret;
  308. ret = av_adts_header_parse(pkt->data, &samples, &frames);
  309. if (ret < 0) {
  310. av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n");
  311. return ret;
  312. }
  313. ctx->pkt_offset = samples << 2;
  314. switch (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", 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. if (ctx->hd_buf_count < 23) {
  369. memset(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length + pkt->size],
  370. 0, TRUEHD_FRAME_OFFSET - pkt->size - mat_code_length);
  371. } else {
  372. size_t padding = MAT_FRAME_SIZE - (ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + pkt->size);
  373. memset(&ctx->hd_buf[MAT_FRAME_SIZE - padding], 0, padding);
  374. }
  375. if (++ctx->hd_buf_count < 24){
  376. ctx->pkt_offset = 0;
  377. return 0;
  378. }
  379. memcpy(&ctx->hd_buf[MAT_FRAME_SIZE - sizeof(mat_end_code)], mat_end_code, sizeof(mat_end_code));
  380. ctx->hd_buf_count = 0;
  381. ctx->data_type = IEC61937_TRUEHD;
  382. ctx->pkt_offset = 61440;
  383. ctx->out_buf = ctx->hd_buf;
  384. ctx->out_bytes = MAT_FRAME_SIZE;
  385. ctx->length_code = MAT_FRAME_SIZE;
  386. return 0;
  387. }
  388. static int spdif_write_header(AVFormatContext *s)
  389. {
  390. IEC61937Context *ctx = s->priv_data;
  391. switch (s->streams[0]->codecpar->codec_id) {
  392. case AV_CODEC_ID_AC3:
  393. ctx->header_info = spdif_header_ac3;
  394. break;
  395. case AV_CODEC_ID_EAC3:
  396. ctx->header_info = spdif_header_eac3;
  397. break;
  398. case AV_CODEC_ID_MP1:
  399. case AV_CODEC_ID_MP2:
  400. case AV_CODEC_ID_MP3:
  401. ctx->header_info = spdif_header_mpeg;
  402. break;
  403. case AV_CODEC_ID_DTS:
  404. ctx->header_info = spdif_header_dts;
  405. break;
  406. case AV_CODEC_ID_AAC:
  407. ctx->header_info = spdif_header_aac;
  408. break;
  409. case AV_CODEC_ID_TRUEHD:
  410. case AV_CODEC_ID_MLP:
  411. ctx->header_info = spdif_header_truehd;
  412. ctx->hd_buf = av_malloc(MAT_FRAME_SIZE);
  413. if (!ctx->hd_buf)
  414. return AVERROR(ENOMEM);
  415. break;
  416. default:
  417. avpriv_report_missing_feature(s, "Codec %d",
  418. s->streams[0]->codecpar->codec_id);
  419. return AVERROR_PATCHWELCOME;
  420. }
  421. return 0;
  422. }
  423. static int spdif_write_trailer(AVFormatContext *s)
  424. {
  425. IEC61937Context *ctx = s->priv_data;
  426. av_freep(&ctx->buffer);
  427. av_freep(&ctx->hd_buf);
  428. return 0;
  429. }
  430. static av_always_inline void spdif_put_16(IEC61937Context *ctx,
  431. AVIOContext *pb, unsigned int val)
  432. {
  433. if (ctx->spdif_flags & SPDIF_FLAG_BIGENDIAN)
  434. avio_wb16(pb, val);
  435. else
  436. avio_wl16(pb, val);
  437. }
  438. static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
  439. {
  440. IEC61937Context *ctx = s->priv_data;
  441. int ret, padding;
  442. ctx->out_buf = pkt->data;
  443. ctx->out_bytes = pkt->size;
  444. ctx->length_code = FFALIGN(pkt->size, 2) << 3;
  445. ctx->use_preamble = 1;
  446. ctx->extra_bswap = 0;
  447. ret = ctx->header_info(s, pkt);
  448. if (ret < 0)
  449. return ret;
  450. if (!ctx->pkt_offset)
  451. return 0;
  452. padding = (ctx->pkt_offset - ctx->use_preamble * BURST_HEADER_SIZE - ctx->out_bytes) & ~1;
  453. if (padding < 0) {
  454. av_log(s, AV_LOG_ERROR, "bitrate is too high\n");
  455. return AVERROR(EINVAL);
  456. }
  457. if (ctx->use_preamble) {
  458. spdif_put_16(ctx, s->pb, SYNCWORD1); //Pa
  459. spdif_put_16(ctx, s->pb, SYNCWORD2); //Pb
  460. spdif_put_16(ctx, s->pb, ctx->data_type); //Pc
  461. spdif_put_16(ctx, s->pb, ctx->length_code);//Pd
  462. }
  463. if (ctx->extra_bswap ^ (ctx->spdif_flags & SPDIF_FLAG_BIGENDIAN)) {
  464. avio_write(s->pb, ctx->out_buf, ctx->out_bytes & ~1);
  465. } else {
  466. av_fast_malloc(&ctx->buffer, &ctx->buffer_size, ctx->out_bytes + AV_INPUT_BUFFER_PADDING_SIZE);
  467. if (!ctx->buffer)
  468. return AVERROR(ENOMEM);
  469. ff_spdif_bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)ctx->out_buf, ctx->out_bytes >> 1);
  470. avio_write(s->pb, ctx->buffer, ctx->out_bytes & ~1);
  471. }
  472. /* a final lone byte has to be MSB aligned */
  473. if (ctx->out_bytes & 1)
  474. spdif_put_16(ctx, s->pb, ctx->out_buf[ctx->out_bytes - 1] << 8);
  475. ffio_fill(s->pb, 0, padding);
  476. av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",
  477. ctx->data_type, ctx->out_bytes, ctx->pkt_offset);
  478. return 0;
  479. }
  480. AVOutputFormat ff_spdif_muxer = {
  481. .name = "spdif",
  482. .long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (used on S/PDIF - IEC958)"),
  483. .extensions = "spdif",
  484. .priv_data_size = sizeof(IEC61937Context),
  485. .audio_codec = AV_CODEC_ID_AC3,
  486. .video_codec = AV_CODEC_ID_NONE,
  487. .write_header = spdif_write_header,
  488. .write_packet = spdif_write_packet,
  489. .write_trailer = spdif_write_trailer,
  490. .flags = AVFMT_NOTIMESTAMPS,
  491. .priv_class = &spdif_class,
  492. };