32#include "config_components.h"
66 const AVDVProfile* sys;
67 AVFormatContext* fctx;
70 struct DVPacket audio_pkt[4];
71 uint8_t audio_buf[4][8192];
81 uint16_t
shift, result;
88 }
else if (
shift < 0x8) {
99static const uint8_t *dv_extract_pack(
const uint8_t *
frame,
enum DVPackType t)
104 for (
c = 0;
c < 10;
c++) {
107 if (
c&1) offs = (80 * 6 + 80 * 16 * 0 + 3 +
c*12000);
108 else offs = (80 * 6 + 80 * 16 * 3 + 3 +
c*12000);
111 if (
c&1) offs = (80 * 6 + 80 * 16 * 1 + 3 +
c*12000);
112 else offs = (80 * 6 + 80 * 16 * 4 + 3 +
c*12000);
115 if (
c&1) offs = (80 * 3 + 8 +
c*12000);
116 else offs = (80 * 5 + 48 + 5 +
c*12000);
119 offs = (80*1 + 3 + 3);
124 if (
frame[offs] == t)
131static const int dv_audio_frequency[3] = {
143static int dv_extract_audio(
const uint8_t *
frame, uint8_t **ppcm,
146 int size, chan,
i, j, d, of, smpls, freq,
quant, half_ch;
148 const uint8_t *as_pack;
155 smpls = as_pack[1] & 0x3f;
156 freq = as_pack[4] >> 3 & 0x07;
157 quant = as_pack[4] & 0x07;
170 ipcm = (sys->
height == 720 && !(
frame[1] & 0x0C)) ? 2 : 0;
178 for (chan = 0; chan < sys->
n_difchan; chan++) {
187 if (
quant == 1 &&
i == half_ch) {
196 for (j = 0; j < 9; j++) {
197 for (d = 8; d < 80; d += 2) {
206 pcm[of * 2] =
frame[d + 1];
207 pcm[of * 2 + 1] =
frame[d];
209 if (pcm[of * 2 + 1] == 0x80 && pcm[of * 2] == 0x00)
212 lc = ((uint16_t)
frame[d] << 4) |
213 ((uint16_t)
frame[d + 2] >> 4);
214 rc = ((uint16_t)
frame[d + 1] << 4) |
215 ((uint16_t)
frame[d + 2] & 0x0f);
226 pcm[of * 2] = lc & 0xff;
227 pcm[of * 2 + 1] = lc >> 8;
232 pcm[of * 2] = rc & 0xff;
233 pcm[of * 2 + 1] = rc >> 8;
248 const uint8_t *as_pack;
249 int freq, stype, smpls,
quant,
i, ach, sr;
252 if (!as_pack || !
c->sys) {
257 smpls = as_pack[1] & 0x3f;
258 freq = as_pack[4] >> 3 & 0x07;
259 stype = as_pack[3] & 0x1f;
260 quant = as_pack[4] & 0x07;
264 "Unrecognized audio sample rate index (%d)\n", freq);
267 sr = dv_audio_frequency[freq];
276 ach = ((
int[4]) { 1, 0, 2, 4 })[stype];
277 if (ach == 1 &&
quant && freq == 2)
282 for (
i = 0;
i < ach;
i++) {
292 c->ast[
i]->start_time = 0;
293 c->ast[
i]->codecpar->bit_rate = 2 * sr * 16;
295 c->ast[
i]->codecpar->sample_rate = sr;
296 c->audio_pkt[
i].last_sample_rate = sr;
298 c->audio_pkt[
i].size = 0;
299 c->audio_pkt[
i].data =
c->audio_buf[
i];
300 c->audio_pkt[
i].stream_index =
c->ast[
i]->index;
303 c->audio_pkt[
i].duration = 0;
304 c->audio_pkt[
i].pos = -1;
307 c->audio_pkt[
i].sample_rate = sr;
311 return (
c->sys->audio_min_samples[freq] + smpls) * 4;
316 const uint8_t *vsc_pack;
320 par =
c->vst->codecpar;
322 c->vst->avg_frame_rate =
av_inv_q(
c->vst->time_base);
326 apt =
frame[4] & 0x07;
327 is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
328 (!apt && (vsc_pack[2] & 0x07) == 0x07)));
329 c->vst->sample_aspect_ratio =
c->sys->sar[is16_9];
333 return c->sys->frame_size;
338 const uint8_t *tc_pack;
343 int prevent_df =
c->sys->ltc_divisor == 25 ||
c->sys->ltc_divisor == 50;
363 c->vst->start_time = 0;
381 if (dv_init_demux(
s,
c)) {
394 for (
i = 0;
i <
c->ach;
i++) {
395 if (
c->ast[
i] &&
c->audio_pkt[
i].size) {
398 pkt->size = dpkt->size;
399 pkt->data = dpkt->data;
400 pkt->stream_index = dpkt->stream_index;
401 pkt->flags = dpkt->flags;
402 pkt->pts = dpkt->pts;
403 pkt->duration = dpkt->duration;
404 pkt->pos = dpkt->pos;
409 if (dpkt->last_sample_rate != dpkt->sample_rate) {
413 dpkt->last_sample_rate = dpkt->sample_rate;
428 uint8_t *ppcm[5] = { 0 };
438 size = dv_extract_audio_info(
c,
buf);
444 c->ast[0]->time_base);
448 c->ast[0]->time_base);
454 next_pts_video :
c->next_pts_audio;
459 for (
i = 0;
i <
c->ach;
i++) {
467 ppcm[
i] =
c->audio_buf[
i];
470 dv_extract_audio(
buf, ppcm,
c->sys);
474 if (
c->sys->height == 720) {
476 c->audio_pkt[2].size =
c->audio_pkt[3].size = 0;
478 c->audio_pkt[0].size =
c->audio_pkt[1].size = 0;
484 size = dv_extract_video_info(
c,
buf);
490 pkt->stream_index =
c->vst->index;
491 pkt->pts =
c->next_pts_video;
494 c->next_pts_video +=
pkt->duration;
527 c->frames = !
c->sys ? 0 :
529 c->next_pts_video = ts;
533 c->audio_pkt[0].size =
c->audio_pkt[1].size = 0;
534 c->audio_pkt[2].size =
c->audio_pkt[3].size = 0;
541typedef struct RawDVContext {
552#define PARTIAL_FRAME_SIZE (3 * 80)
553 uint8_t partial_frame[PARTIAL_FRAME_SIZE];
554 RawDVContext *
c =
s->priv_data;
556 ret =
avio_read(
s->pb, partial_frame, PARTIAL_FRAME_SIZE);
560 if (ret < PARTIAL_FRAME_SIZE) {
565 ret = dv_extract_timecode(&
c->dv_demux, partial_frame, timecode);
578 unsigned state, marker_pos = 0;
579 RawDVContext *
c =
s->priv_data;
583 if ((ret = dv_init_demux(
s, &
c->dv_demux)) < 0)
587 while ((
state & 0xffffff7f) != 0x1f07003f) {
592 if (
state == 0x003f0700 ||
state == 0xff3f0700)
611 if (!
c->dv_demux.sys) {
613 "Can't determine profile of DV input stream.\n");
619 c->dv_demux.sys->time_base);
630 RawDVContext *
c =
s->priv_data;
637 if (!
c->dv_demux.sys)
639 size =
c->dv_demux.sys->frame_size;
643 }
else if (ret == 0) {
656 RawDVContext *
r =
s->priv_data;
661 if (stream_index !=
c->vst->index)
662 timestamp =
av_rescale_q(timestamp,
s->streams[stream_index]->time_base,
665 offset = dv_frame_offset(
s,
c, ×tamp);
676 unsigned marker_pos = 0;
680 int secondary_matches = 0;
685 for (
i = 0;
i <
p->buf_size-4;
i++) {
687 if ((
state & 0x0007f840) == 0x00070000) {
690 if ((
state & 0xff07ff7f) == 0x1f07003f) {
692 if ((
state & 0xffffff7f) == 0x1f07003f) {
698 if (
state == 0x003f0700 ||
state == 0xff3f0700)
700 if (
state == 0xff3f0701 &&
i - marker_pos == 80)
705 if (matches &&
p->buf_size / matches < 1024 * 1024) {
706 if (matches > 4 || firstmatch ||
707 (secondary_matches >= 10 &&
708 p->buf_size / secondary_matches < 24000))
719 .p.extensions =
"dv,dif",
720 .priv_data_size =
sizeof(RawDVContext),
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
int64_t avio_size(AVIOContext *s)
Get the filesize.
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
unsigned int avio_rb32(AVIOContext *s)
int avio_r8(AVIOContext *s)
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
static int read_probe(const AVProbeData *p)
Public libavutil channel layout APIs header.
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
int ff_add_param_change(AVPacket *pkt, int32_t channels, uint64_t channel_layout, int32_t sample_rate, int32_t width, int32_t height)
Add side data to a packet for changing parameters to the given values.
const AVDVProfile * av_dv_frame_profile(const AVDVProfile *sys, const uint8_t *frame, unsigned buf_size)
Get a DV profile for the provided compressed frame.
static uint16_t dv_audio_12to16(uint16_t sample)
static struct @346255127015250356166251341105367306144006377143 state
static int read_header(FFV1Context *f, RangeCoder *c)
static const uint8_t frame_size[4]
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_STEREO
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define AV_NOPTS_VALUE
Undefined timestamp value.
static int shift(int a, int b)
#define DV_TIMESCALE_AUDIO
#define DV_TIMESCALE_VIDEO
#define DV_MAX_FRAME_SIZE
largest possible DV frame, in bytes (1080i50)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Memory handling functions.
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
#define FF_ARRAY_ELEMS(a)
An AVChannelLayout holds information about the channel layout of audio data.
This struct describes the properties of an encoded stream.
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
const uint8_t(* audio_shuffle)[9]
This structure stores compressed data.
This structure contains the data a format has to probe a file.
Rational number (pair of numerator and denominator).
For DV, one packet corresponds exactly to one frame.
uint8_t * buf
actual buffer data
int64_t data_offset
offset of the first packet
char * av_timecode_make_smpte_tc_string2(char *buf, AVRational rate, uint32_t tcsmpte, int prevent_df, int skip_field)
Get the timecode string from the SMPTE timecode format.
#define AV_TIMECODE_STR_SIZE
static const uint8_t quant[64]