comparison dv.c @ 995:c459e0d4b0b9 libavformat

DVCPRO50 support. Patch by Daniel Maas dmaas at maasdigital dot com
author romansh
date Mon, 06 Mar 2006 08:54:33 +0000
parents 2b60e57286eb
children da22d8928247
comparison
equal deleted inserted replaced
994:41b4111a021e 995:c459e0d4b0b9
5 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth 5 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
6 * of DV technical info. 6 * of DV technical info.
7 * 7 *
8 * Raw DV format 8 * Raw DV format
9 * Copyright (c) 2002 Fabrice Bellard. 9 * Copyright (c) 2002 Fabrice Bellard.
10 *
11 * 50 Mbps (DVCPRO50) support
12 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
10 * 13 *
11 * This library is free software; you can redistribute it and/or 14 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public 15 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either 16 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version. 17 * version 2 of the License, or (at your option) any later version.
26 #include "avformat.h" 29 #include "avformat.h"
27 #include "dvdata.h" 30 #include "dvdata.h"
28 #include "dv.h" 31 #include "dv.h"
29 32
30 struct DVDemuxContext { 33 struct DVDemuxContext {
34 const DVprofile* sys; /* Current DV profile. E.g.: 525/60, 625/50 */
31 AVFormatContext* fctx; 35 AVFormatContext* fctx;
32 AVStream* vst; 36 AVStream* vst;
33 AVStream* ast[2]; 37 AVStream* ast[2];
34 AVPacket audio_pkt[2]; 38 AVPacket audio_pkt[2];
35 uint8_t audio_buf[2][8192]; 39 uint8_t audio_buf[2][8192];
38 uint64_t abytes; 42 uint64_t abytes;
39 }; 43 };
40 44
41 struct DVMuxContext { 45 struct DVMuxContext {
42 const DVprofile* sys; /* Current DV profile. E.g.: 525/60, 625/50 */ 46 const DVprofile* sys; /* Current DV profile. E.g.: 525/60, 625/50 */
43 uint8_t frame_buf[144000]; /* frame under contruction */ 47 int n_ast; /* Number of stereo audio streams (up to 2) */
44 FifoBuffer audio_data; /* Fifo for storing excessive amounts of PCM */ 48 AVStream *ast[2]; /* Stereo audio streams */
49 FifoBuffer audio_data[2]; /* Fifo for storing excessive amounts of PCM */
45 int frames; /* Number of a current frame */ 50 int frames; /* Number of a current frame */
46 time_t start_time; /* Start time of recording */ 51 time_t start_time; /* Start time of recording */
47 uint8_t aspect; /* Aspect ID 0 - 4:3, 7 - 16:9 */ 52 uint8_t aspect; /* Aspect ID 0 - 4:3, 7 - 16:9 */
48 int has_audio; /* frame under contruction has audio */ 53 int has_audio; /* frame under contruction has audio */
49 int has_video; /* frame under contruction has video */ 54 int has_video; /* frame under contruction has video */
55 uint8_t frame_buf[DV_MAX_FRAME_SIZE]; /* frame under contruction */
50 }; 56 };
51 57
52 enum dv_section_type { 58 enum dv_section_type {
53 dv_sect_header = 0x1f, 59 dv_sect_header = 0x1f,
54 dv_sect_subcode = 0x3f, 60 dv_sect_subcode = 0x3f,
175 { 181 {
176 struct tm tc; 182 struct tm tc;
177 time_t ct; 183 time_t ct;
178 int ltc_frame; 184 int ltc_frame;
179 185
186 /* Its hard to tell what SMPTE requires w.r.t. APT, but Quicktime needs it.
187 * We set it based on pix_fmt value but it really should be per DV profile */
188 int apt = (c->sys->pix_fmt == PIX_FMT_YUV422P ? 1 : 0);
189
180 buf[0] = (uint8_t)pack_id; 190 buf[0] = (uint8_t)pack_id;
181 switch (pack_id) { 191 switch (pack_id) {
182 case dv_header525: /* I can't imagine why these two weren't defined as real */ 192 case dv_header525: /* I can't imagine why these two weren't defined as real */
183 case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */ 193 case dv_header625: /* packs in SMPTE314M -- they definitely look like ones */
184 buf[1] = 0xf8 | /* reserved -- always 1 */ 194 buf[1] = 0xf8 | /* reserved -- always 1 */
185 (0 & 0x07); /* APT: Track application ID */ 195 (apt & 0x07); /* APT: Track application ID */
186 buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */ 196 buf[2] = (0 << 7) | /* TF1: audio data is 0 - valid; 1 - invalid */
187 (0x0f << 3) | /* reserved -- always 1 */ 197 (0x0f << 3) | /* reserved -- always 1 */
188 (0 & 0x07); /* AP1: Audio application ID */ 198 (apt & 0x07); /* AP1: Audio application ID */
189 buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */ 199 buf[3] = (0 << 7) | /* TF2: video data is 0 - valid; 1 - invalid */
190 (0x0f << 3) | /* reserved -- always 1 */ 200 (0x0f << 3) | /* reserved -- always 1 */
191 (0 & 0x07); /* AP2: Video application ID */ 201 (apt & 0x07); /* AP2: Video application ID */
192 buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */ 202 buf[4] = (0 << 7) | /* TF3: subcode(SSYB) is 0 - valid; 1 - invalid */
193 (0x0f << 3) | /* reserved -- always 1 */ 203 (0x0f << 3) | /* reserved -- always 1 */
194 (0 & 0x07); /* AP3: Subcode application ID */ 204 (apt & 0x07); /* AP3: Subcode application ID */
195 break; 205 break;
196 case dv_timecode: 206 case dv_timecode:
197 ct = (time_t)(c->frames / ((float)c->sys->frame_rate / 207 ct = (time_t)(c->frames / ((float)c->sys->frame_rate /
198 (float)c->sys->frame_rate_base)); 208 (float)c->sys->frame_rate_base));
199 brktimegm(ct, &tc); 209 brktimegm(ct, &tc);
228 (0 << 4) | /* pair bit: 0 -- one pair of channels */ 238 (0 << 4) | /* pair bit: 0 -- one pair of channels */
229 0; /* audio mode */ 239 0; /* audio mode */
230 buf[3] = (1 << 7) | /* res */ 240 buf[3] = (1 << 7) | /* res */
231 (1 << 6) | /* multi-language flag */ 241 (1 << 6) | /* multi-language flag */
232 (c->sys->dsf << 5) | /* system: 60fields/50fields */ 242 (c->sys->dsf << 5) | /* system: 60fields/50fields */
233 0; /* definition: 0 -- SD (525/625) */ 243 (apt << 1);/* definition: 0 -- 25Mbps, 2 -- 50Mbps */
234 buf[4] = (1 << 7) | /* emphasis: 1 -- off */ 244 buf[4] = (1 << 7) | /* emphasis: 1 -- off */
235 (0 << 6) | /* emphasis time constant: 0 -- reserved */ 245 (0 << 6) | /* emphasis time constant: 0 -- reserved */
236 (0 << 3) | /* frequency: 0 -- 48Khz, 1 -- 44,1Khz, 2 -- 32Khz */ 246 (0 << 3) | /* frequency: 0 -- 48Khz, 1 -- 44,1Khz, 2 -- 32Khz */
237 0; /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */ 247 0; /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */
238 break; 248 break;
289 (1 << 6) | /* following CLF is valid - 0, invalid - 1 */ 299 (1 << 6) | /* following CLF is valid - 0, invalid - 1 */
290 (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */ 300 (3 << 4) | /* CLF: color frames id (see ITU-R BT.470-4) */
291 0xf; /* reserved -- always 1 */ 301 0xf; /* reserved -- always 1 */
292 buf[3] = (3 << 6) | /* reserved -- always 1 */ 302 buf[3] = (3 << 6) | /* reserved -- always 1 */
293 (c->sys->dsf << 5) | /* system: 60fields/50fields */ 303 (c->sys->dsf << 5) | /* system: 60fields/50fields */
294 0; /* signal type video compression */ 304 (apt << 2); /* signal type video compression */
295 buf[4] = 0xff; /* VISC: 0xff -- no information */ 305 buf[4] = 0xff; /* VISC: 0xff -- no information */
296 break; 306 break;
297 case dv_video_control: 307 case dv_video_control:
298 buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */ 308 buf[1] = (0 << 6) | /* Copy generation management (CGMS) 0 -- free */
299 0x3f; /* reserved -- always 1 */ 309 0x3f; /* reserved -- always 1 */
310 buf[1] = buf[2] = buf[3] = buf[4] = 0xff; 320 buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
311 } 321 }
312 return 5; 322 return 5;
313 } 323 }
314 324
315 static inline int dv_write_dif_id(enum dv_section_type t, uint8_t seq_num, 325 static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num, uint8_t seq_num,
316 uint8_t dif_num, uint8_t* buf) 326 uint8_t dif_num, uint8_t* buf)
317 { 327 {
318 buf[0] = (uint8_t)t; /* Section type */ 328 buf[0] = (uint8_t)t; /* Section type */
319 buf[1] = (seq_num<<4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */ 329 buf[1] = (seq_num<<4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
320 (0 << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */ 330 (chan_num << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */
321 7; /* reserved -- always 1 */ 331 7; /* reserved -- always 1 */
322 buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */ 332 buf[2] = dif_num; /* DIF block number Video: 0-134, Audio: 0-8 */
323 return 3; 333 return 3;
324 } 334 }
325 335
345 return 3; 355 return 3;
346 } 356 }
347 357
348 static void dv_format_frame(DVMuxContext *c, uint8_t* buf) 358 static void dv_format_frame(DVMuxContext *c, uint8_t* buf)
349 { 359 {
350 int i, j, k; 360 int chan, i, j, k;
351 361
352 for (i = 0; i < c->sys->difseg_size; i++) { 362 for (chan = 0; chan < c->sys->n_difchan; chan++) {
353 memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */ 363 for (i = 0; i < c->sys->difseg_size; i++) {
354 364 memset(buf, 0xff, 80 * 6); /* First 6 DIF blocks are for control data */
355 /* DV header: 1DIF */ 365
356 buf += dv_write_dif_id(dv_sect_header, i, 0, buf); 366 /* DV header: 1DIF */
357 buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf); 367 buf += dv_write_dif_id(dv_sect_header, chan, i, 0, buf);
358 buf += 72; /* unused bytes */ 368 buf += dv_write_pack((c->sys->dsf ? dv_header625 : dv_header525), c, buf);
359 369 buf += 72; /* unused bytes */
360 /* DV subcode: 2DIFs */ 370
361 for (j = 0; j < 2; j++) { 371 /* DV subcode: 2DIFs */
362 buf += dv_write_dif_id( dv_sect_subcode, i, j, buf); 372 for (j = 0; j < 2; j++) {
363 for (k = 0; k < 6; k++) { 373 buf += dv_write_dif_id(dv_sect_subcode, chan, i, j, buf);
364 buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf); 374 for (k = 0; k < 6; k++) {
365 buf += dv_write_pack(dv_ssyb_packs_dist[i][k], c, buf); 375 buf += dv_write_ssyb_id(k, (i < c->sys->difseg_size/2), buf);
366 } 376 buf += dv_write_pack(dv_ssyb_packs_dist[i][k], c, buf);
367 buf += 29; /* unused bytes */ 377 }
368 } 378 buf += 29; /* unused bytes */
369
370 /* DV VAUX: 3DIFs */
371 for (j = 0; j < 3; j++) {
372 buf += dv_write_dif_id(dv_sect_vaux, i, j, buf);
373 for (k = 0; k < 15 ; k++)
374 buf += dv_write_pack(dv_vaux_packs_dist[i][k], c, buf);
375 buf += 2; /* unused bytes */
376 }
377
378 /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
379 for (j = 0; j < 135; j++) {
380 if (j%15 == 0) {
381 memset(buf, 0xff, 80);
382 buf += dv_write_dif_id(dv_sect_audio, i, j/15, buf);
383 buf += 77; /* audio control & shuffled PCM audio */
384 } 379 }
385 buf += dv_write_dif_id(dv_sect_video, i, j, buf); 380
386 buf += 77; /* 1 video macro block: 1 bytes control 381 /* DV VAUX: 3DIFs */
387 4 * 14 bytes Y 8x8 data 382 for (j = 0; j < 3; j++) {
388 10 bytes Cr 8x8 data 383 buf += dv_write_dif_id(dv_sect_vaux, chan, i, j, buf);
389 10 bytes Cb 8x8 data */ 384 for (k = 0; k < 15 ; k++)
390 } 385 buf += dv_write_pack(dv_vaux_packs_dist[i][k], c, buf);
391 } 386 buf += 2; /* unused bytes */
392 } 387 }
393 388
394 static void dv_inject_audio(DVMuxContext *c, const uint8_t* pcm, uint8_t* frame_ptr) 389 /* DV Audio/Video: 135 Video DIFs + 9 Audio DIFs */
390 for (j = 0; j < 135; j++) {
391 if (j%15 == 0) {
392 memset(buf, 0xff, 80);
393 buf += dv_write_dif_id(dv_sect_audio, chan, i, j/15, buf);
394 buf += 77; /* audio control & shuffled PCM audio */
395 }
396 buf += dv_write_dif_id(dv_sect_video, chan, i, j, buf);
397 buf += 77; /* 1 video macro block: 1 bytes control
398 4 * 14 bytes Y 8x8 data
399 10 bytes Cr 8x8 data
400 10 bytes Cb 8x8 data */
401 }
402 }
403 }
404 }
405
406 static void dv_inject_audio(DVMuxContext *c, const uint8_t* pcm, int channel, uint8_t* frame_ptr)
395 { 407 {
396 int i, j, d, of, size; 408 int i, j, d, of, size;
397 size = 4 * dv_audio_frame_size(c->sys, c->frames); 409 size = 4 * dv_audio_frame_size(c->sys, c->frames);
410 frame_ptr += channel * c->sys->difseg_size * 150 * 80;
398 for (i = 0; i < c->sys->difseg_size; i++) { 411 for (i = 0; i < c->sys->difseg_size; i++) {
399 frame_ptr += 6 * 80; /* skip DIF segment header */ 412 frame_ptr += 6 * 80; /* skip DIF segment header */
400 for (j = 0; j < 9; j++) { 413 for (j = 0; j < 9; j++) {
401 dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3]); 414 dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3]);
402 for (d = 8; d < 80; d+=2) { 415 for (d = 8; d < 80; d+=2) {
412 } 425 }
413 } 426 }
414 427
415 static void dv_inject_video(DVMuxContext *c, const uint8_t* video_data, uint8_t* frame_ptr) 428 static void dv_inject_video(DVMuxContext *c, const uint8_t* video_data, uint8_t* frame_ptr)
416 { 429 {
417 int i, j; 430 int chan, i, j;
418 int ptr = 0; 431 int ptr = 0;
419 432
420 for (i = 0; i < c->sys->difseg_size; i++) { 433 for (chan = 0; chan < c->sys->n_difchan; chan++) {
421 ptr += 6 * 80; /* skip DIF segment header */ 434 for (i = 0; i < c->sys->difseg_size; i++) {
422 for (j = 0; j < 135; j++) { 435 ptr += 6 * 80; /* skip DIF segment header */
423 if (j%15 == 0) 436 for (j = 0; j < 135; j++) {
424 ptr += 80; /* skip Audio DIF */ 437 if (j%15 == 0)
425 ptr += 3; 438 ptr += 80; /* skip Audio DIF */
426 memcpy(frame_ptr + ptr, video_data + ptr, 77); 439 ptr += 3;
427 ptr += 77; 440 memcpy(frame_ptr + ptr, video_data + ptr, 77);
428 } 441 ptr += 77;
442 }
443 }
429 } 444 }
430 } 445 }
431 446
432 /* 447 /*
433 * This is the dumbest implementation of all -- it simply looks at 448 * This is the dumbest implementation of all -- it simply looks at
461 * We can pass them upwards when ffmpeg will be ready to deal with them. 476 * We can pass them upwards when ffmpeg will be ready to deal with them.
462 * 2. We don't do software emphasis. 477 * 2. We don't do software emphasis.
463 * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples 478 * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples
464 * are converted into 16bit linear ones. 479 * are converted into 16bit linear ones.
465 */ 480 */
466 static int dv_extract_audio(uint8_t* frame, uint8_t* pcm, uint8_t* pcm2) 481 static int dv_extract_audio(uint8_t* frame, uint8_t* pcm, uint8_t* pcm2,
467 { 482 const DVprofile *sys)
468 int size, i, j, d, of, smpls, freq, quant, half_ch; 483 {
484 int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
469 uint16_t lc, rc; 485 uint16_t lc, rc;
470 const DVprofile* sys;
471 const uint8_t* as_pack; 486 const uint8_t* as_pack;
472 487
473 as_pack = dv_extract_pack(frame, dv_audio_source); 488 as_pack = dv_extract_pack(frame, dv_audio_source);
474 if (!as_pack) /* No audio ? */ 489 if (!as_pack) /* No audio ? */
475 return 0; 490 return 0;
476 491
477 sys = dv_frame_profile(frame);
478 smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */ 492 smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
479 freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */ 493 freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
480 quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */ 494 quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
481 495
482 if (quant > 1) 496 if (quant > 1)
483 return -1; /* Unsupported quantization */ 497 return -1; /* Unsupported quantization */
484 498
485 size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */ 499 size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
486 half_ch = sys->difseg_size/2; 500 half_ch = sys->difseg_size/2;
487 501
488 /* for each DIF segment */ 502 /* for each DIF channel */
489 for (i = 0; i < sys->difseg_size; i++) { 503 for (chan = 0; chan < sys->n_difchan; chan++) {
490 frame += 6 * 80; /* skip DIF segment header */ 504 /* for each DIF segment */
491 if (quant == 1 && i == half_ch) { 505 for (i = 0; i < sys->difseg_size; i++) {
492 if (!pcm2) 506 frame += 6 * 80; /* skip DIF segment header */
493 break; 507 if (quant == 1 && i == half_ch) {
494 else 508 /* next stereo channel (12bit mode only) */
495 pcm = pcm2; 509 if (!pcm2)
496 } 510 break;
497 511 else
498 for (j = 0; j < 9; j++) { 512 pcm = pcm2;
499 for (d = 8; d < 80; d += 2) { 513 }
500 if (quant == 0) { /* 16bit quantization */ 514
501 of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride; 515 /* for each AV sequence */
502 if (of*2 >= size) 516 for (j = 0; j < 9; j++) {
503 continue; 517 for (d = 8; d < 80; d += 2) {
504 518 if (quant == 0) { /* 16bit quantization */
505 pcm[of*2] = frame[d+1]; // FIXME: may be we have to admit 519 of = sys->audio_shuffle[i][j] + (d - 8)/2 * sys->audio_stride;
506 pcm[of*2+1] = frame[d]; // that DV is a big endian PCM 520 if (of*2 >= size)
507 if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00) 521 continue;
508 pcm[of*2+1] = 0; 522
509 } else { /* 12bit quantization */ 523 pcm[of*2] = frame[d+1]; // FIXME: may be we have to admit
510 lc = ((uint16_t)frame[d] << 4) | 524 pcm[of*2+1] = frame[d]; // that DV is a big endian PCM
511 ((uint16_t)frame[d+2] >> 4); 525 if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
512 rc = ((uint16_t)frame[d+1] << 4) | 526 pcm[of*2+1] = 0;
513 ((uint16_t)frame[d+2] & 0x0f); 527 } else { /* 12bit quantization */
514 lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc)); 528 lc = ((uint16_t)frame[d] << 4) |
515 rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc)); 529 ((uint16_t)frame[d+2] >> 4);
516 530 rc = ((uint16_t)frame[d+1] << 4) |
517 of = sys->audio_shuffle[i%half_ch][j] + (d - 8)/3 * sys->audio_stride; 531 ((uint16_t)frame[d+2] & 0x0f);
518 if (of*2 >= size) 532 lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
519 continue; 533 rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
520 534
521 pcm[of*2] = lc & 0xff; // FIXME: may be we have to admit 535 of = sys->audio_shuffle[i%half_ch][j] + (d - 8)/3 * sys->audio_stride;
522 pcm[of*2+1] = lc >> 8; // that DV is a big endian PCM 536 if (of*2 >= size)
523 of = sys->audio_shuffle[i%half_ch+half_ch][j] + 537 continue;
524 (d - 8)/3 * sys->audio_stride; 538
525 pcm[of*2] = rc & 0xff; // FIXME: may be we have to admit 539 pcm[of*2] = lc & 0xff; // FIXME: may be we have to admit
526 pcm[of*2+1] = rc >> 8; // that DV is a big endian PCM 540 pcm[of*2+1] = lc >> 8; // that DV is a big endian PCM
527 ++d; 541 of = sys->audio_shuffle[i%half_ch+half_ch][j] +
528 } 542 (d - 8)/3 * sys->audio_stride;
529 } 543 pcm[of*2] = rc & 0xff; // FIXME: may be we have to admit
530 544 pcm[of*2+1] = rc >> 8; // that DV is a big endian PCM
531 frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */ 545 ++d;
546 }
547 }
548
549 frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
550 }
532 } 551 }
552
553 /* next stereo channel (50Mbps only) */
554 if(!pcm2)
555 break;
556 pcm = pcm2;
533 } 557 }
534 558
535 return size; 559 return size;
536 } 560 }
537 561
538 static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame) 562 static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
539 { 563 {
540 const uint8_t* as_pack; 564 const uint8_t* as_pack;
541 const DVprofile* sys; 565 int freq, stype, smpls, quant, i, ach;
542 int freq, smpls, quant, i, ach; 566
543
544 sys = dv_frame_profile(frame);
545 as_pack = dv_extract_pack(frame, dv_audio_source); 567 as_pack = dv_extract_pack(frame, dv_audio_source);
546 if (!as_pack || !sys) { /* No audio ? */ 568 if (!as_pack || !c->sys) { /* No audio ? */
547 c->ach = 0; 569 c->ach = 0;
548 return 0; 570 return 0;
549 } 571 }
550 572
551 smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */ 573 smpls = as_pack[1] & 0x3f; /* samples in this frame - min. samples */
552 freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */ 574 freq = (as_pack[4] >> 3) & 0x07; /* 0 - 48KHz, 1 - 44,1kHz, 2 - 32 kHz */
575 stype = (as_pack[3] & 0x1f); /* 0 - 2CH, 2 - 4CH */
553 quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */ 576 quant = as_pack[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
554 ach = (quant && freq == 2) ? 2 : 1; 577
578 /* note: ach counts PAIRS of channels (i.e. stereo channels) */
579 ach = (stype == 2 || (quant && (freq == 2))) ? 2 : 1;
555 580
556 /* Dynamic handling of the audio streams in DV */ 581 /* Dynamic handling of the audio streams in DV */
557 for (i=0; i<ach; i++) { 582 for (i=0; i<ach; i++) {
558 if (!c->ast[i]) { 583 if (!c->ast[i]) {
559 c->ast[i] = av_new_stream(c->fctx, 0); 584 c->ast[i] = av_new_stream(c->fctx, 0);
574 c->ast[i]->codec->bit_rate = 2 * dv_audio_frequency[freq] * 16; 599 c->ast[i]->codec->bit_rate = 2 * dv_audio_frequency[freq] * 16;
575 c->ast[i]->start_time = 0; 600 c->ast[i]->start_time = 0;
576 } 601 }
577 c->ach = i; 602 c->ach = i;
578 603
579 return (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */; 604 return (c->sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
580 } 605 }
581 606
582 static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame) 607 static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
583 { 608 {
584 const DVprofile* sys;
585 const uint8_t* vsc_pack; 609 const uint8_t* vsc_pack;
586 AVCodecContext* avctx; 610 AVCodecContext* avctx;
587 int apt, is16_9; 611 int apt, is16_9;
588 int size = 0; 612 int size = 0;
589 613
590 sys = dv_frame_profile(frame); 614 if (c->sys) {
591 if (sys) {
592 avctx = c->vst->codec; 615 avctx = c->vst->codec;
593 616
594 av_set_pts_info(c->vst, 64, sys->frame_rate_base, sys->frame_rate); 617 av_set_pts_info(c->vst, 64, c->sys->frame_rate_base, c->sys->frame_rate);
595 avctx->time_base= (AVRational){sys->frame_rate_base, sys->frame_rate}; 618 avctx->time_base= (AVRational){c->sys->frame_rate_base, c->sys->frame_rate};
596 if(!avctx->width){ 619 if(!avctx->width){
597 avctx->width = sys->width; 620 avctx->width = c->sys->width;
598 avctx->height = sys->height; 621 avctx->height = c->sys->height;
599 } 622 }
600 avctx->pix_fmt = sys->pix_fmt; 623 avctx->pix_fmt = c->sys->pix_fmt;
601 624
602 /* finding out SAR is a little bit messy */ 625 /* finding out SAR is a little bit messy */
603 vsc_pack = dv_extract_pack(frame, dv_video_control); 626 vsc_pack = dv_extract_pack(frame, dv_video_control);
604 apt = frame[4] & 0x07; 627 apt = frame[4] & 0x07;
605 is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 || 628 is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
606 (!apt && (vsc_pack[2] & 0x07) == 0x07))); 629 (!apt && (vsc_pack[2] & 0x07) == 0x07)));
607 avctx->sample_aspect_ratio = sys->sar[is16_9]; 630 avctx->sample_aspect_ratio = c->sys->sar[is16_9];
608 631 avctx->bit_rate = av_rescale(c->sys->frame_size * 8,
609 size = sys->frame_size; 632 c->sys->frame_rate,
633 c->sys->frame_rate_base);
634 size = c->sys->frame_size;
610 } 635 }
611 return size; 636 return size;
612 } 637 }
613 638
614 /* 639 /*
617 642
618 int dv_assemble_frame(DVMuxContext *c, AVStream* st, 643 int dv_assemble_frame(DVMuxContext *c, AVStream* st,
619 const uint8_t* data, int data_size, uint8_t** frame) 644 const uint8_t* data, int data_size, uint8_t** frame)
620 { 645 {
621 uint8_t pcm[8192]; 646 uint8_t pcm[8192];
622 int fsize, reqasize; 647 int i;
623 648
624 *frame = &c->frame_buf[0]; 649 *frame = &c->frame_buf[0];
625 if (c->has_audio && c->has_video) { /* must be a stale frame */ 650 if (c->has_audio && c->has_video &&
651 (c->has_audio == -1 || c->has_audio == c->n_ast)) {
652 /* must be a stale frame */
626 dv_format_frame(c, *frame); 653 dv_format_frame(c, *frame);
627 c->frames++; 654 c->frames++;
628 if (c->has_audio > 0) 655 if (c->has_audio > 0)
629 c->has_audio = 0; 656 c->has_audio = 0;
630 c->has_video = 0; 657 c->has_video = 0;
640 data_size = 0; 667 data_size = 0;
641 if (c->has_audio < 0) 668 if (c->has_audio < 0)
642 goto out; 669 goto out;
643 } 670 }
644 671
645 reqasize = 4 * dv_audio_frame_size(c->sys, c->frames); 672 for (i = 0; i < c->n_ast; i++) {
646 fsize = fifo_size(&c->audio_data, c->audio_data.rptr); 673 int reqasize, fsize;
647 if (st->codec->codec_type == CODEC_TYPE_AUDIO || (c->has_video && fsize >= reqasize)) { 674 if (st != c->ast[i])
648 if (fsize + data_size >= reqasize && !c->has_audio) { 675 continue;
649 if (fsize >= reqasize) { 676 reqasize = 4 * dv_audio_frame_size(c->sys, c->frames);
650 fifo_read(&c->audio_data, &pcm[0], reqasize, &c->audio_data.rptr); 677 fsize = fifo_size(&c->audio_data[i], c->audio_data[i].rptr);
651 } else { 678 if (st->codec->codec_type == CODEC_TYPE_AUDIO ||
652 fifo_read(&c->audio_data, &pcm[0], fsize, &c->audio_data.rptr); 679 (c->has_video && fsize >= reqasize)) {
653 memcpy(&pcm[fsize], &data[0], reqasize - fsize); 680 if (fsize + data_size >= reqasize && (c->has_audio < c->n_ast)) {
654 data += reqasize - fsize; 681 if (fsize >= reqasize) {
655 data_size -= reqasize - fsize; 682 fifo_read(&c->audio_data[i], &pcm[0], reqasize, &c->audio_data[i].rptr);
683 } else {
684 fifo_read(&c->audio_data[i], &pcm[0], fsize, &c->audio_data[i].rptr);
685 memcpy(&pcm[fsize], &data[0], reqasize - fsize);
686 data += reqasize - fsize;
687 data_size -= reqasize - fsize;
688 }
689 dv_inject_audio(c, &pcm[0], i, *frame);
690 c->has_audio += 1;
656 } 691 }
657 dv_inject_audio(c, &pcm[0], *frame); 692 /* FIXME: we have to have more sensible approach than this one */
658 c->has_audio = 1; 693 if (fifo_size(&c->audio_data[i], c->audio_data[i].rptr) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE)
694 av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
695 fifo_write(&c->audio_data[i], (uint8_t *)data, data_size, &c->audio_data[i].wptr);
659 } 696 }
660 697 }
661 /* FIXME: we have to have more sensible approach than this one */
662 if (fifo_size(&c->audio_data, c->audio_data.rptr) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE)
663 av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
664 fifo_write(&c->audio_data, (uint8_t *)data, data_size, &c->audio_data.wptr);
665 }
666
667 out: 698 out:
668 return (c->has_audio && c->has_video) ? c->sys->frame_size : 0; 699 return ((c->has_audio == -1 || c->has_audio == c->n_ast) && c->has_video) ? c->sys->frame_size : 0;
669 } 700 }
670 701
671 DVMuxContext* dv_init_mux(AVFormatContext* s) 702 DVMuxContext* dv_init_mux(AVFormatContext* s)
672 { 703 {
673 DVMuxContext *c; 704 DVMuxContext *c;
674 AVStream *vst = NULL; 705 AVStream *vst = NULL;
675 AVStream *ast = NULL;
676 int i; 706 int i;
677 707
678 if (s->nb_streams > 2) 708 /* we support at most 1 video and 2 audio streams */
709 if (s->nb_streams > 3)
679 return NULL; 710 return NULL;
680 711
681 c = av_mallocz(sizeof(DVMuxContext)); 712 c = av_mallocz(sizeof(DVMuxContext));
682 if (!c) 713 if (!c)
683 return NULL; 714 return NULL;
715
716 c->n_ast = 0;
717 c->ast[0] = c->ast[1] = NULL;
684 718
685 /* We have to sort out where audio and where video stream is */ 719 /* We have to sort out where audio and where video stream is */
686 for (i=0; i<s->nb_streams; i++) { 720 for (i=0; i<s->nb_streams; i++) {
687 switch (s->streams[i]->codec->codec_type) { 721 switch (s->streams[i]->codec->codec_type) {
688 case CODEC_TYPE_VIDEO: 722 case CODEC_TYPE_VIDEO:
689 vst = s->streams[i]; 723 vst = s->streams[i];
690 break; 724 break;
691 case CODEC_TYPE_AUDIO: 725 case CODEC_TYPE_AUDIO:
692 ast = s->streams[i]; 726 c->ast[c->n_ast++] = s->streams[i];
693 break; 727 break;
694 default: 728 default:
695 goto bail_out; 729 goto bail_out;
696 } 730 }
697 } 731 }
698 732
699 /* Some checks -- DV format is very picky about its incoming streams */ 733 /* Some checks -- DV format is very picky about its incoming streams */
700 if (!vst || vst->codec->codec_id != CODEC_ID_DVVIDEO) 734 if (!vst || vst->codec->codec_id != CODEC_ID_DVVIDEO)
701 goto bail_out; 735 goto bail_out;
702 if (ast && (ast->codec->codec_id != CODEC_ID_PCM_S16LE || 736 for (i=0; i<c->n_ast; i++) {
703 ast->codec->sample_rate != 48000 || 737 if (c->ast[i] && (c->ast[i]->codec->codec_id != CODEC_ID_PCM_S16LE ||
704 ast->codec->channels != 2)) 738 c->ast[i]->codec->sample_rate != 48000 ||
705 goto bail_out; 739 c->ast[i]->codec->channels != 2))
740 goto bail_out;
741 }
706 c->sys = dv_codec_profile(vst->codec); 742 c->sys = dv_codec_profile(vst->codec);
707 if (!c->sys) 743 if (!c->sys)
708 goto bail_out; 744 goto bail_out;
709 745
746 if((c->n_ast > 1) && (c->sys->n_difchan < 2)) {
747 /* only 1 stereo pair is allowed in 25Mbps mode */
748 goto bail_out;
749 }
750
710 /* Ok, everything seems to be in working order */ 751 /* Ok, everything seems to be in working order */
711 c->frames = 0; 752 c->frames = 0;
712 c->has_audio = ast ? 0 : -1; 753 c->has_audio = c->n_ast ? 0 : -1;
713 c->has_video = 0; 754 c->has_video = 0;
714 c->start_time = (time_t)s->timestamp; 755 c->start_time = (time_t)s->timestamp;
715 c->aspect = 0; /* 4:3 is the default */ 756 c->aspect = 0; /* 4:3 is the default */
716 if ((int)(av_q2d(vst->codec->sample_aspect_ratio) * vst->codec->width / vst->codec->height * 10) == 17) /* 16:9 */ 757 if ((int)(av_q2d(vst->codec->sample_aspect_ratio) * vst->codec->width / vst->codec->height * 10) == 17) /* 16:9 */
717 c->aspect = 0x07; 758 c->aspect = 0x07;
718 759
719 if (ast && fifo_init(&c->audio_data, 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) 760 for (i=0; i<c->n_ast; i++) {
720 goto bail_out; 761 if (c->ast[i] && fifo_init(&c->audio_data[i], 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) {
762 while (i>0) {
763 i--;
764 fifo_free(&c->audio_data[i]);
765 }
766 goto bail_out;
767 }
768 }
721 769
722 dv_format_frame(c, &c->frame_buf[0]); 770 dv_format_frame(c, &c->frame_buf[0]);
723 771
724 return c; 772 return c;
725 773
728 return NULL; 776 return NULL;
729 } 777 }
730 778
731 void dv_delete_mux(DVMuxContext *c) 779 void dv_delete_mux(DVMuxContext *c)
732 { 780 {
733 fifo_free(&c->audio_data); 781 int i;
782 for (i=0; i < c->n_ast; i++)
783 fifo_free(&c->audio_data[i]);
734 } 784 }
735 785
736 DVDemuxContext* dv_init_demux(AVFormatContext *s) 786 DVDemuxContext* dv_init_demux(AVFormatContext *s)
737 { 787 {
738 DVDemuxContext *c; 788 DVDemuxContext *c;
745 if (!c->vst) { 795 if (!c->vst) {
746 av_free(c); 796 av_free(c);
747 return NULL; 797 return NULL;
748 } 798 }
749 799
800 c->sys = NULL;
750 c->fctx = s; 801 c->fctx = s;
751 c->ast[0] = c->ast[1] = NULL; 802 c->ast[0] = c->ast[1] = NULL;
752 c->ach = 0; 803 c->ach = 0;
753 c->frames = 0; 804 c->frames = 0;
754 c->abytes = 0; 805 c->abytes = 0;
780 831
781 int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, 832 int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
782 uint8_t* buf, int buf_size) 833 uint8_t* buf, int buf_size)
783 { 834 {
784 int size, i; 835 int size, i;
785 const DVprofile* sys = dv_frame_profile(buf); 836
786 837 if (buf_size < 4 || buf_size < c->sys->frame_size)
787 if (buf_size < 4 || buf_size < sys->frame_size)
788 return -1; /* Broken frame, or not enough data */ 838 return -1; /* Broken frame, or not enough data */
789 839
790 /* Queueing audio packet */ 840 /* Queueing audio packet */
791 /* FIXME: in case of no audio/bad audio we have to do something */ 841 /* FIXME: in case of no audio/bad audio we have to do something */
792 size = dv_extract_audio_info(c, buf); 842 size = dv_extract_audio_info(c, buf);
793 for (i=0; i<c->ach; i++) { 843 for (i=0; i<c->ach; i++) {
794 c->audio_pkt[i].size = size; 844 c->audio_pkt[i].size = size;
795 c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate; 845 c->audio_pkt[i].pts = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;
796 } 846 }
797 dv_extract_audio(buf, c->audio_buf[0], c->audio_buf[1]); 847 dv_extract_audio(buf, c->audio_buf[0], c->audio_buf[1], c->sys);
798 c->abytes += size; 848 c->abytes += size;
799 849
800 /* Now it's time to return video packet */ 850 /* Now it's time to return video packet */
801 size = dv_extract_video_info(c, buf); 851 size = dv_extract_video_info(c, buf);
802 av_init_packet(pkt); 852 av_init_packet(pkt);
836 /************************************************************ 886 /************************************************************
837 * Implementation of the easiest DV storage of all -- raw DV. 887 * Implementation of the easiest DV storage of all -- raw DV.
838 ************************************************************/ 888 ************************************************************/
839 889
840 typedef struct RawDVContext { 890 typedef struct RawDVContext {
841 uint8_t buf[144000];
842 DVDemuxContext* dv_demux; 891 DVDemuxContext* dv_demux;
892 uint8_t buf[DV_MAX_FRAME_SIZE];
843 } RawDVContext; 893 } RawDVContext;
844 894
845 static int dv_read_header(AVFormatContext *s, 895 static int dv_read_header(AVFormatContext *s,
846 AVFormatParameters *ap) 896 AVFormatParameters *ap)
847 { 897 {
848 RawDVContext *c = s->priv_data; 898 RawDVContext *c = s->priv_data;
849 const DVprofile* sys; 899
850 c->dv_demux = dv_init_demux(s); 900 c->dv_demux = dv_init_demux(s);
851 if (!c->dv_demux) 901 if (!c->dv_demux)
852 return -1; 902 return -1;
853 903
854 if (get_buffer(&s->pb, c->buf, 4) <= 0 || url_fseek(&s->pb, -4, SEEK_CUR) < 0) 904 if (get_buffer(&s->pb, c->buf, DV_PROFILE_BYTES) <= 0 ||
905 url_fseek(&s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0)
855 return AVERROR_IO; 906 return AVERROR_IO;
856 907
857 sys = dv_frame_profile(c->buf); 908 c->dv_demux->sys = dv_frame_profile(c->buf);
858 s->bit_rate = av_rescale(sys->frame_size * 8, sys->frame_rate, sys->frame_rate_base); 909 s->bit_rate = av_rescale(c->dv_demux->sys->frame_size * 8,
910 c->dv_demux->sys->frame_rate,
911 c->dv_demux->sys->frame_rate_base);
859 912
860 return 0; 913 return 0;
861 } 914 }
862 915
863 916
867 RawDVContext *c = s->priv_data; 920 RawDVContext *c = s->priv_data;
868 921
869 size = dv_get_packet(c->dv_demux, pkt); 922 size = dv_get_packet(c->dv_demux, pkt);
870 923
871 if (size < 0) { 924 if (size < 0) {
872 if (get_buffer(&s->pb, c->buf, 4) <= 0) 925 size = c->dv_demux->sys->frame_size;
873 return AVERROR_IO; 926 if (get_buffer(&s->pb, c->buf, size) <= 0)
874
875 size = dv_frame_profile(c->buf)->frame_size;
876 if (get_buffer(&s->pb, c->buf + 4, size - 4) <= 0)
877 return AVERROR_IO; 927 return AVERROR_IO;
878 928
879 size = dv_produce_packet(c->dv_demux, pkt, c->buf, size); 929 size = dv_produce_packet(c->dv_demux, pkt, c->buf, size);
880 } 930 }
881 931
886 int64_t timestamp, int flags) 936 int64_t timestamp, int flags)
887 { 937 {
888 RawDVContext *r = s->priv_data; 938 RawDVContext *r = s->priv_data;
889 DVDemuxContext *c = r->dv_demux; 939 DVDemuxContext *c = r->dv_demux;
890 int64_t offset= dv_frame_offset(s, c, timestamp, flags); 940 int64_t offset= dv_frame_offset(s, c, timestamp, flags);
891 const DVprofile* sys = dv_codec_profile(c->vst->codec); 941
892 942 c->frames= offset / c->sys->frame_size;
893 c->frames= offset / sys->frame_size;
894 if (c->ach) 943 if (c->ach)
895 c->abytes= av_rescale(c->frames, 944 c->abytes= av_rescale(c->frames,
896 c->ast[0]->codec->bit_rate * (int64_t)sys->frame_rate_base, 945 c->ast[0]->codec->bit_rate * (int64_t)c->sys->frame_rate_base,
897 8*sys->frame_rate); 946 8*c->sys->frame_rate);
898 947
899 dv_flush_audio_packets(c); 948 dv_flush_audio_packets(c);
900 return url_fseek(&s->pb, offset, SEEK_SET); 949 return url_fseek(&s->pb, offset, SEEK_SET);
901 } 950 }
902 951
912 { 961 {
913 s->priv_data = dv_init_mux(s); 962 s->priv_data = dv_init_mux(s);
914 if (!s->priv_data) { 963 if (!s->priv_data) {
915 av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n" 964 av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n"
916 "Make sure that you supply exactly two streams:\n" 965 "Make sure that you supply exactly two streams:\n"
917 " video: 25fps or 29.97fps, audio: 2ch/48Khz/PCM\n"); 966 " video: 25fps or 29.97fps, audio: 2ch/48Khz/PCM\n"
967 " (50Mbps allows an optional second audio stream)\n");
918 return -1; 968 return -1;
919 } 969 }
920 return 0; 970 return 0;
921 } 971 }
922 972