Patch by Anssi Hannula, anssi d hannula a iki fi
Originally committed as revision 25834 to svn://svn.ffmpeg.org/ffmpeg/trunk
... | ... |
@@ -55,7 +55,7 @@ version <next>: |
55 | 55 |
- Win64 support for optimized asm functions |
56 | 56 |
- MJPEG/AVI1 to JPEG/JFIF bitstream filter |
57 | 57 |
- ASS subtitle encoder and decoder |
58 |
-- IEC 61937 encapsulation for E-AC3 (for HDMI passthrough) |
|
58 |
+- IEC 61937 encapsulation for E-AC3 and TrueHD (for HDMI passthrough) |
|
59 | 59 |
- overlay filter added |
60 | 60 |
- rename aspect filter to setdar, and pixelaspect to setsar |
61 | 61 |
- IEC 61937 demuxer |
... | ... |
@@ -211,6 +211,66 @@ static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt) |
211 | 211 |
return 0; |
212 | 212 |
} |
213 | 213 |
|
214 |
+ |
|
215 |
+/* |
|
216 |
+ * It seems Dolby TrueHD frames have to be encapsulated in MAT frames before |
|
217 |
+ * they can be encapsulated in IEC 61937. |
|
218 |
+ * Here we encapsulate 24 TrueHD frames in a single MAT frame, padding them |
|
219 |
+ * to achieve constant rate. |
|
220 |
+ * The actual format of a MAT frame is unknown, but the below seems to work. |
|
221 |
+ * However, it seems it is not actually necessary for the 24 TrueHD frames to |
|
222 |
+ * be in an exact alignment with the MAT frame. |
|
223 |
+ */ |
|
224 |
+#define MAT_FRAME_SIZE 61424 |
|
225 |
+#define TRUEHD_FRAME_OFFSET 2560 |
|
226 |
+#define MAT_MIDDLE_CODE_OFFSET -4 |
|
227 |
+ |
|
228 |
+static int spdif_header_truehd(AVFormatContext *s, AVPacket *pkt) |
|
229 |
+{ |
|
230 |
+ IEC958Context *ctx = s->priv_data; |
|
231 |
+ int mat_code_length = 0; |
|
232 |
+ const char mat_end_code[16] = { 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11 }; |
|
233 |
+ |
|
234 |
+ if (!ctx->hd_buf_count) { |
|
235 |
+ 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 }; |
|
236 |
+ mat_code_length = sizeof(mat_start_code) + BURST_HEADER_SIZE; |
|
237 |
+ memcpy(ctx->hd_buf, mat_start_code, sizeof(mat_start_code)); |
|
238 |
+ |
|
239 |
+ } else if (ctx->hd_buf_count == 12) { |
|
240 |
+ const char mat_middle_code[12] = { 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0 }; |
|
241 |
+ mat_code_length = sizeof(mat_middle_code) + MAT_MIDDLE_CODE_OFFSET; |
|
242 |
+ memcpy(&ctx->hd_buf[12 * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + MAT_MIDDLE_CODE_OFFSET], |
|
243 |
+ mat_middle_code, sizeof(mat_middle_code)); |
|
244 |
+ } |
|
245 |
+ |
|
246 |
+ if (pkt->size > TRUEHD_FRAME_OFFSET - mat_code_length) { |
|
247 |
+ /* if such frames exist, we'd need some more complex logic to |
|
248 |
+ * distribute the TrueHD frames in the MAT frame */ |
|
249 |
+ av_log(s, AV_LOG_ERROR, "TrueHD frame too big, %d bytes\n", pkt->size); |
|
250 |
+ av_log_ask_for_sample(s, NULL); |
|
251 |
+ return -1; |
|
252 |
+ } |
|
253 |
+ |
|
254 |
+ memcpy(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length], |
|
255 |
+ pkt->data, pkt->size); |
|
256 |
+ memset(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length + pkt->size], |
|
257 |
+ 0, TRUEHD_FRAME_OFFSET - pkt->size - mat_code_length); |
|
258 |
+ |
|
259 |
+ if (++ctx->hd_buf_count < 24){ |
|
260 |
+ ctx->pkt_offset = 0; |
|
261 |
+ return 0; |
|
262 |
+ } |
|
263 |
+ memcpy(&ctx->hd_buf[MAT_FRAME_SIZE - sizeof(mat_end_code)], mat_end_code, sizeof(mat_end_code)); |
|
264 |
+ ctx->hd_buf_count = 0; |
|
265 |
+ |
|
266 |
+ ctx->data_type = IEC958_TRUEHD; |
|
267 |
+ ctx->pkt_offset = 61440; |
|
268 |
+ ctx->out_buf = ctx->hd_buf; |
|
269 |
+ ctx->out_bytes = MAT_FRAME_SIZE; |
|
270 |
+ ctx->length_code = MAT_FRAME_SIZE; |
|
271 |
+ return 0; |
|
272 |
+} |
|
273 |
+ |
|
214 | 274 |
static int spdif_write_header(AVFormatContext *s) |
215 | 275 |
{ |
216 | 276 |
IEC958Context *ctx = s->priv_data; |
... | ... |
@@ -233,6 +293,12 @@ static int spdif_write_header(AVFormatContext *s) |
233 | 233 |
case CODEC_ID_AAC: |
234 | 234 |
ctx->header_info = spdif_header_aac; |
235 | 235 |
break; |
236 |
+ case CODEC_ID_TRUEHD: |
|
237 |
+ ctx->header_info = spdif_header_truehd; |
|
238 |
+ ctx->hd_buf = av_malloc(MAT_FRAME_SIZE); |
|
239 |
+ if (!ctx->hd_buf) |
|
240 |
+ return AVERROR(ENOMEM); |
|
241 |
+ break; |
|
236 | 242 |
default: |
237 | 243 |
av_log(s, AV_LOG_ERROR, "codec not supported\n"); |
238 | 244 |
return -1; |