2 * General DV muxer/demuxer
3 * Copyright (c) 2003 Roman Shaposhnik
5 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
6 * of DV technical info.
9 * Copyright (c) 2002 Fabrice Bellard.
11 * 50 Mbps (DVCPRO50) and 100 Mbps (DVCPRO HD) support
12 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
13 * Funded by BBC Research & Development
15 * This file is part of FFmpeg.
17 * FFmpeg is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
22 * FFmpeg is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with FFmpeg; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "libavcodec/dvdata.h"
36 struct DVDemuxContext
{
37 const DVprofile
* sys
; /* Current DV profile. E.g.: 525/60, 625/50 */
38 AVFormatContext
* fctx
;
41 AVPacket audio_pkt
[4];
42 uint8_t audio_buf
[4][8192];
48 static inline uint16_t dv_audio_12to16(uint16_t sample
)
50 uint16_t shift
, result
;
52 sample
= (sample
< 0x800) ? sample
: sample
| 0xf000;
53 shift
= (sample
& 0xf00) >> 8;
55 if (shift
< 0x2 || shift
> 0xd) {
57 } else if (shift
< 0x8) {
59 result
= (sample
- (256 * shift
)) << shift
;
62 result
= ((sample
+ ((256 * shift
) + 1)) << shift
) - 1;
69 * This is the dumbest implementation of all -- it simply looks at
70 * a fixed offset and if pack isn't there -- fails. We might want
71 * to have a fallback mechanism for complete search of missing packs.
73 static const uint8_t* dv_extract_pack(uint8_t* frame
, enum dv_pack_type t
)
79 offs
= (80*6 + 80*16*3 + 3);
81 case dv_audio_control
:
82 offs
= (80*6 + 80*16*4 + 3);
84 case dv_video_control
:
85 offs
= (80*5 + 48 + 5);
91 return frame
[offs
] == t
? &frame
[offs
] : NULL
;
95 * There's a couple of assumptions being made here:
96 * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples.
97 * We can pass them upwards when ffmpeg will be ready to deal with them.
98 * 2. We don't do software emphasis.
99 * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples
100 * are converted into 16bit linear ones.
102 static int dv_extract_audio(uint8_t* frame
, uint8_t* ppcm
[4],
103 const DVprofile
*sys
)
105 int size
, chan
, i
, j
, d
, of
, smpls
, freq
, quant
, half_ch
;
107 const uint8_t* as_pack
;
110 as_pack
= dv_extract_pack(frame
, dv_audio_source
);
111 if (!as_pack
) /* No audio ? */
114 smpls
= as_pack
[1] & 0x3f; /* samples in this frame - min. samples */
115 freq
= (as_pack
[4] >> 3) & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */
116 quant
= as_pack
[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
119 return -1; /* unsupported quantization */
121 size
= (sys
->audio_min_samples
[freq
] + smpls
) * 4; /* 2ch, 2bytes */
122 half_ch
= sys
->difseg_size
/ 2;
124 /* We work with 720p frames split in half, thus even frames have
125 * channels 0,1 and odd 2,3. */
126 ipcm
= (sys
->height
== 720 && !(frame
[1] & 0x0C)) ? 2 : 0;
129 /* for each DIF channel */
130 for (chan
= 0; chan
< sys
->n_difchan
; chan
++) {
131 /* for each DIF segment */
132 for (i
= 0; i
< sys
->difseg_size
; i
++) {
133 frame
+= 6 * 80; /* skip DIF segment header */
134 if (quant
== 1 && i
== half_ch
) {
135 /* next stereo channel (12bit mode only) */
141 /* for each AV sequence */
142 for (j
= 0; j
< 9; j
++) {
143 for (d
= 8; d
< 80; d
+= 2) {
144 if (quant
== 0) { /* 16bit quantization */
145 of
= sys
->audio_shuffle
[i
][j
] + (d
- 8) / 2 * sys
->audio_stride
;
149 pcm
[of
*2] = frame
[d
+1]; // FIXME: maybe we have to admit
150 pcm
[of
*2+1] = frame
[d
]; // that DV is a big-endian PCM
151 if (pcm
[of
*2+1] == 0x80 && pcm
[of
*2] == 0x00)
153 } else { /* 12bit quantization */
154 lc
= ((uint16_t)frame
[d
] << 4) |
155 ((uint16_t)frame
[d
+2] >> 4);
156 rc
= ((uint16_t)frame
[d
+1] << 4) |
157 ((uint16_t)frame
[d
+2] & 0x0f);
158 lc
= (lc
== 0x800 ? 0 : dv_audio_12to16(lc
));
159 rc
= (rc
== 0x800 ? 0 : dv_audio_12to16(rc
));
161 of
= sys
->audio_shuffle
[i
%half_ch
][j
] + (d
- 8) / 3 * sys
->audio_stride
;
165 pcm
[of
*2] = lc
& 0xff; // FIXME: maybe we have to admit
166 pcm
[of
*2+1] = lc
>> 8; // that DV is a big-endian PCM
167 of
= sys
->audio_shuffle
[i
%half_ch
+half_ch
][j
] +
168 (d
- 8) / 3 * sys
->audio_stride
;
169 pcm
[of
*2] = rc
& 0xff; // FIXME: maybe we have to admit
170 pcm
[of
*2+1] = rc
>> 8; // that DV is a big-endian PCM
175 frame
+= 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
179 /* next stereo channel (50Mbps and 100Mbps only) */
188 static int dv_extract_audio_info(DVDemuxContext
* c
, uint8_t* frame
)
190 const uint8_t* as_pack
;
191 int freq
, stype
, smpls
, quant
, i
, ach
;
193 as_pack
= dv_extract_pack(frame
, dv_audio_source
);
194 if (!as_pack
|| !c
->sys
) { /* No audio ? */
199 smpls
= as_pack
[1] & 0x3f; /* samples in this frame - min. samples */
200 freq
= (as_pack
[4] >> 3) & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */
201 stype
= (as_pack
[3] & 0x1f); /* 0 - 2CH, 2 - 4CH, 3 - 8CH */
202 quant
= as_pack
[4] & 0x07; /* 0 - 16bit linear, 1 - 12bit nonlinear */
204 /* note: ach counts PAIRS of channels (i.e. stereo channels) */
205 ach
= ((int[4]){ 1, 0, 2, 4})[stype
];
206 if (ach
== 1 && quant
&& freq
== 2)
209 /* Dynamic handling of the audio streams in DV */
210 for (i
= 0; i
< ach
; i
++) {
212 c
->ast
[i
] = av_new_stream(c
->fctx
, 0);
215 av_set_pts_info(c
->ast
[i
], 64, 1, 30000);
216 c
->ast
[i
]->codec
->codec_type
= CODEC_TYPE_AUDIO
;
217 c
->ast
[i
]->codec
->codec_id
= CODEC_ID_PCM_S16LE
;
219 av_init_packet(&c
->audio_pkt
[i
]);
220 c
->audio_pkt
[i
].size
= 0;
221 c
->audio_pkt
[i
].data
= c
->audio_buf
[i
];
222 c
->audio_pkt
[i
].stream_index
= c
->ast
[i
]->index
;
223 c
->audio_pkt
[i
].flags
|= PKT_FLAG_KEY
;
225 c
->ast
[i
]->codec
->sample_rate
= dv_audio_frequency
[freq
];
226 c
->ast
[i
]->codec
->channels
= 2;
227 c
->ast
[i
]->codec
->bit_rate
= 2 * dv_audio_frequency
[freq
] * 16;
228 c
->ast
[i
]->start_time
= 0;
232 return (c
->sys
->audio_min_samples
[freq
] + smpls
) * 4; /* 2ch, 2bytes */;
235 static int dv_extract_video_info(DVDemuxContext
*c
, uint8_t* frame
)
237 const uint8_t* vsc_pack
;
238 AVCodecContext
* avctx
;
243 avctx
= c
->vst
->codec
;
245 av_set_pts_info(c
->vst
, 64, c
->sys
->time_base
.num
,
246 c
->sys
->time_base
.den
);
247 avctx
->time_base
= c
->sys
->time_base
;
249 avctx
->width
= c
->sys
->width
;
250 avctx
->height
= c
->sys
->height
;
252 avctx
->pix_fmt
= c
->sys
->pix_fmt
;
254 /* finding out SAR is a little bit messy */
255 vsc_pack
= dv_extract_pack(frame
, dv_video_control
);
256 apt
= frame
[4] & 0x07;
257 is16_9
= (vsc_pack
&& ((vsc_pack
[2] & 0x07) == 0x02 ||
258 (!apt
&& (vsc_pack
[2] & 0x07) == 0x07)));
259 c
->vst
->sample_aspect_ratio
= c
->sys
->sar
[is16_9
];
260 avctx
->bit_rate
= av_rescale_q(c
->sys
->frame_size
, (AVRational
){8,1},
262 size
= c
->sys
->frame_size
;
268 * The following 3 functions constitute our interface to the world
271 DVDemuxContext
* dv_init_demux(AVFormatContext
*s
)
275 c
= av_mallocz(sizeof(DVDemuxContext
));
279 c
->vst
= av_new_stream(s
, 0);
287 memset(c
->ast
, 0, sizeof(c
->ast
));
292 c
->vst
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
293 c
->vst
->codec
->codec_id
= CODEC_ID_DVVIDEO
;
294 c
->vst
->codec
->bit_rate
= 25000000;
295 c
->vst
->start_time
= 0;
300 int dv_get_packet(DVDemuxContext
*c
, AVPacket
*pkt
)
305 for (i
= 0; i
< c
->ach
; i
++) {
306 if (c
->ast
[i
] && c
->audio_pkt
[i
].size
) {
307 *pkt
= c
->audio_pkt
[i
];
308 c
->audio_pkt
[i
].size
= 0;
317 int dv_produce_packet(DVDemuxContext
*c
, AVPacket
*pkt
,
318 uint8_t* buf
, int buf_size
)
321 uint8_t *ppcm
[4] = {0};
323 if (buf_size
< DV_PROFILE_BYTES
||
324 !(c
->sys
= dv_frame_profile(buf
)) ||
325 buf_size
< c
->sys
->frame_size
) {
326 return -1; /* Broken frame, or not enough data */
329 /* Queueing audio packet */
330 /* FIXME: in case of no audio/bad audio we have to do something */
331 size
= dv_extract_audio_info(c
, buf
);
332 for (i
= 0; i
< c
->ach
; i
++) {
333 c
->audio_pkt
[i
].size
= size
;
334 c
->audio_pkt
[i
].pts
= c
->abytes
* 30000*8 / c
->ast
[i
]->codec
->bit_rate
;
335 ppcm
[i
] = c
->audio_buf
[i
];
337 dv_extract_audio(buf
, ppcm
, c
->sys
);
340 /* We work with 720p frames split in half, thus even frames have
341 * channels 0,1 and odd 2,3. */
342 if (c
->sys
->height
== 720) {
344 c
->audio_pkt
[2].size
= c
->audio_pkt
[3].size
= 0;
346 c
->audio_pkt
[0].size
= c
->audio_pkt
[1].size
= 0;
349 /* Now it's time to return video packet */
350 size
= dv_extract_video_info(c
, buf
);
354 pkt
->flags
|= PKT_FLAG_KEY
;
355 pkt
->stream_index
= c
->vst
->id
;
356 pkt
->pts
= c
->frames
;
363 static int64_t dv_frame_offset(AVFormatContext
*s
, DVDemuxContext
*c
,
364 int64_t timestamp
, int flags
)
366 // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
367 const DVprofile
* sys
= dv_codec_profile(c
->vst
->codec
);
369 int64_t size
= url_fsize(s
->pb
);
370 int64_t max_offset
= ((size
-1) / sys
->frame_size
) * sys
->frame_size
;
372 offset
= sys
->frame_size
* timestamp
;
374 if (offset
> max_offset
) offset
= max_offset
;
375 else if (offset
< 0) offset
= 0;
380 void dv_offset_reset(DVDemuxContext
*c
, int64_t frame_offset
)
382 c
->frames
= frame_offset
;
384 c
->abytes
= av_rescale_q(c
->frames
, c
->sys
->time_base
,
385 (AVRational
){8, c
->ast
[0]->codec
->bit_rate
});
386 c
->audio_pkt
[0].size
= c
->audio_pkt
[1].size
= 0;
387 c
->audio_pkt
[2].size
= c
->audio_pkt
[3].size
= 0;
390 /************************************************************
391 * Implementation of the easiest DV storage of all -- raw DV.
392 ************************************************************/
394 typedef struct RawDVContext
{
395 DVDemuxContext
* dv_demux
;
396 uint8_t buf
[DV_MAX_FRAME_SIZE
];
399 static int dv_read_header(AVFormatContext
*s
,
400 AVFormatParameters
*ap
)
402 RawDVContext
*c
= s
->priv_data
;
404 c
->dv_demux
= dv_init_demux(s
);
408 if (get_buffer(s
->pb
, c
->buf
, DV_PROFILE_BYTES
) <= 0 ||
409 url_fseek(s
->pb
, -DV_PROFILE_BYTES
, SEEK_CUR
) < 0)
412 c
->dv_demux
->sys
= dv_frame_profile(c
->buf
);
413 if (!c
->dv_demux
->sys
) {
414 av_log(s
, AV_LOG_ERROR
, "Can't determine profile of DV input stream.\n");
418 s
->bit_rate
= av_rescale_q(c
->dv_demux
->sys
->frame_size
, (AVRational
){8,1},
419 c
->dv_demux
->sys
->time_base
);
425 static int dv_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
428 RawDVContext
*c
= s
->priv_data
;
430 size
= dv_get_packet(c
->dv_demux
, pkt
);
433 size
= c
->dv_demux
->sys
->frame_size
;
434 if (get_buffer(s
->pb
, c
->buf
, size
) <= 0)
437 size
= dv_produce_packet(c
->dv_demux
, pkt
, c
->buf
, size
);
443 static int dv_read_seek(AVFormatContext
*s
, int stream_index
,
444 int64_t timestamp
, int flags
)
446 RawDVContext
*r
= s
->priv_data
;
447 DVDemuxContext
*c
= r
->dv_demux
;
448 int64_t offset
= dv_frame_offset(s
, c
, timestamp
, flags
);
450 dv_offset_reset(c
, offset
/ c
->sys
->frame_size
);
452 offset
= url_fseek(s
->pb
, offset
, SEEK_SET
);
453 return (offset
< 0) ? offset
: 0;
456 static int dv_read_close(AVFormatContext
*s
)
458 RawDVContext
*c
= s
->priv_data
;
459 av_free(c
->dv_demux
);
463 #ifdef CONFIG_DV_DEMUXER
464 AVInputFormat dv_demuxer
= {
466 NULL_IF_CONFIG_SMALL("DV video format"),
467 sizeof(RawDVContext
),
473 .extensions
= "dv,dif",