Statistics
| Branch: | Revision:

ffmpeg / libavformat / dv.c @ bd51c013

History | View | Annotate | Download (15.3 KB)

1 115329f1 Diego Biurrun
/*
2
 * General DV muxer/demuxer
3 064cf251 Diego Biurrun
 * Copyright (c) 2003 Roman Shaposhnik
4 7458ccbb Roman Shaposhnik
 *
5
 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
6
 * of DV technical info.
7
 *
8 f20dca40 Fabrice Bellard
 * Raw DV format
9 406792e7 Diego Biurrun
 * Copyright (c) 2002 Fabrice Bellard
10 f20dca40 Fabrice Bellard
 *
11 f8007a14 Roman Shaposhnik
 * 50 Mbps (DVCPRO50) and 100 Mbps (DVCPRO HD) support
12 0b297700 Daniel Maas
 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
13 f8007a14 Roman Shaposhnik
 * Funded by BBC Research & Development
14 0b297700 Daniel Maas
 *
15 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
16
 *
17
 * FFmpeg is free software; you can redistribute it and/or
18 f20dca40 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
19
 * License as published by the Free Software Foundation; either
20 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
21 f20dca40 Fabrice Bellard
 *
22 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
23 f20dca40 Fabrice Bellard
 * 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.
26
 *
27
 * You should have received a copy of the GNU Lesser General Public
28 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
29 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 f20dca40 Fabrice Bellard
 */
31 7458ccbb Roman Shaposhnik
#include <time.h>
32 f20dca40 Fabrice Bellard
#include "avformat.h"
33 245976da Diego Biurrun
#include "libavcodec/dvdata.h"
34 67d44b84 Reimar Döffinger
#include "libavutil/intreadwrite.h"
35 ddaae6a9 Roman Shaposhnik
#include "dv.h"
36
37
struct DVDemuxContext {
38 0b297700 Daniel Maas
    const DVprofile*  sys;    /* Current DV profile. E.g.: 525/60, 625/50 */
39 f94036f1 Diego Biurrun
    AVFormatContext*  fctx;
40
    AVStream*         vst;
41
    AVStream*         ast[4];
42
    AVPacket          audio_pkt[4];
43
    uint8_t           audio_buf[4][8192];
44
    int               ach;
45
    int               frames;
46
    uint64_t          abytes;
47 ddaae6a9 Roman Shaposhnik
};
48 f20dca40 Fabrice Bellard
49 7458ccbb Roman Shaposhnik
static inline uint16_t dv_audio_12to16(uint16_t sample)
50
{
51
    uint16_t shift, result;
52 115329f1 Diego Biurrun
53 7458ccbb Roman Shaposhnik
    sample = (sample < 0x800) ? sample : sample | 0xf000;
54 f94036f1 Diego Biurrun
    shift  = (sample & 0xf00) >> 8;
55 7458ccbb Roman Shaposhnik
56
    if (shift < 0x2 || shift > 0xd) {
57 bb270c08 Diego Biurrun
        result = sample;
58 7458ccbb Roman Shaposhnik
    } else if (shift < 0x8) {
59
        shift--;
60 bb270c08 Diego Biurrun
        result = (sample - (256 * shift)) << shift;
61 7458ccbb Roman Shaposhnik
    } else {
62 bb270c08 Diego Biurrun
        shift = 0xe - shift;
63
        result = ((sample + ((256 * shift) + 1)) << shift) - 1;
64 7458ccbb Roman Shaposhnik
    }
65
66
    return result;
67
}
68
69 115329f1 Diego Biurrun
/*
70 7458ccbb Roman Shaposhnik
 * This is the dumbest implementation of all -- it simply looks at
71
 * a fixed offset and if pack isn't there -- fails. We might want
72
 * to have a fallback mechanism for complete search of missing packs.
73
 */
74
static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
75
{
76
    int offs;
77 115329f1 Diego Biurrun
78 7458ccbb Roman Shaposhnik
    switch (t) {
79
    case dv_audio_source:
80 f94036f1 Diego Biurrun
        offs = (80*6 + 80*16*3 + 3);
81
        break;
82 7458ccbb Roman Shaposhnik
    case dv_audio_control:
83 f94036f1 Diego Biurrun
        offs = (80*6 + 80*16*4 + 3);
84
        break;
85 7458ccbb Roman Shaposhnik
    case dv_video_control:
86 f94036f1 Diego Biurrun
        offs = (80*5 + 48 + 5);
87
        break;
88 7458ccbb Roman Shaposhnik
    default:
89 f94036f1 Diego Biurrun
        return NULL;
90 115329f1 Diego Biurrun
    }
91 7458ccbb Roman Shaposhnik
92 ccd425e7 Diego Biurrun
    return frame[offs] == t ? &frame[offs] : NULL;
93 7458ccbb Roman Shaposhnik
}
94
95 115329f1 Diego Biurrun
/*
96 7458ccbb Roman Shaposhnik
 * There's a couple of assumptions being made here:
97
 * 1. By default we silence erroneous (0x8000/16bit 0x800/12bit) audio samples.
98
 *    We can pass them upwards when ffmpeg will be ready to deal with them.
99
 * 2. We don't do software emphasis.
100
 * 3. Audio is always returned as 16bit linear samples: 12bit nonlinear samples
101
 *    are converted into 16bit linear ones.
102
 */
103 f8007a14 Roman Shaposhnik
static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4],
104 0b297700 Daniel Maas
                            const DVprofile *sys)
105 7458ccbb Roman Shaposhnik
{
106 0b297700 Daniel Maas
    int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
107 7458ccbb Roman Shaposhnik
    uint16_t lc, rc;
108
    const uint8_t* as_pack;
109 f8007a14 Roman Shaposhnik
    uint8_t *pcm, ipcm;
110 115329f1 Diego Biurrun
111 7458ccbb Roman Shaposhnik
    as_pack = dv_extract_pack(frame, dv_audio_source);
112
    if (!as_pack)    /* No audio ? */
113 bb270c08 Diego Biurrun
        return 0;
114 115329f1 Diego Biurrun
115 f94036f1 Diego Biurrun
    smpls =  as_pack[1] & 0x3f;       /* samples in this frame - min. samples */
116
    freq  = (as_pack[4] >> 3) & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */
117
    quant =  as_pack[4] & 0x07;       /* 0 - 16bit linear, 1 - 12bit nonlinear */
118 115329f1 Diego Biurrun
119 7458ccbb Roman Shaposhnik
    if (quant > 1)
120 75f2c209 Diego Biurrun
        return -1; /* unsupported quantization */
121 7458ccbb Roman Shaposhnik
122
    size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
123 f94036f1 Diego Biurrun
    half_ch = sys->difseg_size / 2;
124 7458ccbb Roman Shaposhnik
125 75f2c209 Diego Biurrun
    /* We work with 720p frames split in half, thus even frames have
126 f94036f1 Diego Biurrun
     * channels 0,1 and odd 2,3. */
127
    ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;
128
    pcm  = ppcm[ipcm++];
129 f8007a14 Roman Shaposhnik
130 0b297700 Daniel Maas
    /* for each DIF channel */
131
    for (chan = 0; chan < sys->n_difchan; chan++) {
132
        /* for each DIF segment */
133
        for (i = 0; i < sys->difseg_size; i++) {
134
            frame += 6 * 80; /* skip DIF segment header */
135
            if (quant == 1 && i == half_ch) {
136
                /* next stereo channel (12bit mode only) */
137 f8007a14 Roman Shaposhnik
                pcm = ppcm[ipcm++];
138
                if (!pcm)
139 0b297700 Daniel Maas
                    break;
140
            }
141 bb270c08 Diego Biurrun
142 0b297700 Daniel Maas
            /* for each AV sequence */
143
            for (j = 0; j < 9; j++) {
144
                for (d = 8; d < 80; d += 2) {
145
                    if (quant == 0) {  /* 16bit quantization */
146 f94036f1 Diego Biurrun
                        of = sys->audio_shuffle[i][j] + (d - 8) / 2 * sys->audio_stride;
147 0b297700 Daniel Maas
                        if (of*2 >= size)
148
                            continue;
149
150 910f02a0 Diego Biurrun
                        pcm[of*2]   = frame[d+1]; // FIXME: maybe we have to admit
151
                        pcm[of*2+1] = frame[d];   //        that DV is a big-endian PCM
152 0b297700 Daniel Maas
                        if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
153
                            pcm[of*2+1] = 0;
154
                    } else {           /* 12bit quantization */
155 f94036f1 Diego Biurrun
                        lc = ((uint16_t)frame[d]   << 4) |
156 0b297700 Daniel Maas
                             ((uint16_t)frame[d+2] >> 4);
157
                        rc = ((uint16_t)frame[d+1] << 4) |
158
                             ((uint16_t)frame[d+2] & 0x0f);
159
                        lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
160
                        rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
161
162 f94036f1 Diego Biurrun
                        of = sys->audio_shuffle[i%half_ch][j] + (d - 8) / 3 * sys->audio_stride;
163 0b297700 Daniel Maas
                        if (of*2 >= size)
164
                            continue;
165
166 910f02a0 Diego Biurrun
                        pcm[of*2]   = lc & 0xff; // FIXME: maybe we have to admit
167
                        pcm[of*2+1] = lc >> 8;   //        that DV is a big-endian PCM
168 0b297700 Daniel Maas
                        of = sys->audio_shuffle[i%half_ch+half_ch][j] +
169 f94036f1 Diego Biurrun
                            (d - 8) / 3 * sys->audio_stride;
170 910f02a0 Diego Biurrun
                        pcm[of*2]   = rc & 0xff; // FIXME: maybe we have to admit
171
                        pcm[of*2+1] = rc >> 8;   //        that DV is a big-endian PCM
172 0b297700 Daniel Maas
                        ++d;
173
                    }
174
                }
175
176
                frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
177
            }
178 7458ccbb Roman Shaposhnik
        }
179 0b297700 Daniel Maas
180 f8007a14 Roman Shaposhnik
        /* next stereo channel (50Mbps and 100Mbps only) */
181
        pcm = ppcm[ipcm++];
182
        if (!pcm)
183 0b297700 Daniel Maas
            break;
184 7458ccbb Roman Shaposhnik
    }
185
186
    return size;
187
}
188
189 ddaae6a9 Roman Shaposhnik
static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
190 7458ccbb Roman Shaposhnik
{
191
    const uint8_t* as_pack;
192 0b297700 Daniel Maas
    int freq, stype, smpls, quant, i, ach;
193 7458ccbb Roman Shaposhnik
194
    as_pack = dv_extract_pack(frame, dv_audio_source);
195 0b297700 Daniel Maas
    if (!as_pack || !c->sys) {    /* No audio ? */
196 ddaae6a9 Roman Shaposhnik
        c->ach = 0;
197 bb270c08 Diego Biurrun
        return 0;
198 ddaae6a9 Roman Shaposhnik
    }
199 115329f1 Diego Biurrun
200 f94036f1 Diego Biurrun
    smpls =  as_pack[1] & 0x3f;       /* samples in this frame - min. samples */
201
    freq  = (as_pack[4] >> 3) & 0x07; /* 0 - 48kHz, 1 - 44,1kHz, 2 - 32kHz */
202
    stype = (as_pack[3] & 0x1f);      /* 0 - 2CH, 2 - 4CH, 3 - 8CH */
203
    quant =  as_pack[4] & 0x07;       /* 0 - 16bit linear, 1 - 12bit nonlinear */
204 0b297700 Daniel Maas
205
    /* note: ach counts PAIRS of channels (i.e. stereo channels) */
206 51f4155f Michael Niedermayer
    ach = ((int[4]){  1,  0,  2,  4})[stype];
207 a53621fc Roman Shaposhnik
    if (ach == 1 && quant && freq == 2)
208
        ach = 2;
209 50883632 Roman Shaposhnik
210
    /* Dynamic handling of the audio streams in DV */
211 f94036f1 Diego Biurrun
    for (i = 0; i < ach; i++) {
212 50883632 Roman Shaposhnik
       if (!c->ast[i]) {
213
           c->ast[i] = av_new_stream(c->fctx, 0);
214 bb270c08 Diego Biurrun
           if (!c->ast[i])
215
               break;
216
           av_set_pts_info(c->ast[i], 64, 1, 30000);
217
           c->ast[i]->codec->codec_type = CODEC_TYPE_AUDIO;
218 f94036f1 Diego Biurrun
           c->ast[i]->codec->codec_id   = CODEC_ID_PCM_S16LE;
219 664f5836 Roman Shaposhnik
220
           av_init_packet(&c->audio_pkt[i]);
221 f94036f1 Diego Biurrun
           c->audio_pkt[i].size         = 0;
222
           c->audio_pkt[i].data         = c->audio_buf[i];
223 664f5836 Roman Shaposhnik
           c->audio_pkt[i].stream_index = c->ast[i]->index;
224 f94036f1 Diego Biurrun
           c->audio_pkt[i].flags       |= PKT_FLAG_KEY;
225 50883632 Roman Shaposhnik
       }
226 01f4895c Michael Niedermayer
       c->ast[i]->codec->sample_rate = dv_audio_frequency[freq];
227 f94036f1 Diego Biurrun
       c->ast[i]->codec->channels    = 2;
228
       c->ast[i]->codec->bit_rate    = 2 * dv_audio_frequency[freq] * 16;
229
       c->ast[i]->start_time         = 0;
230 ddaae6a9 Roman Shaposhnik
    }
231 50883632 Roman Shaposhnik
    c->ach = i;
232 115329f1 Diego Biurrun
233 0b297700 Daniel Maas
    return (c->sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
234 7458ccbb Roman Shaposhnik
}
235
236 ddaae6a9 Roman Shaposhnik
static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
237 7458ccbb Roman Shaposhnik
{
238
    const uint8_t* vsc_pack;
239 ddaae6a9 Roman Shaposhnik
    AVCodecContext* avctx;
240
    int apt, is16_9;
241 7458ccbb Roman Shaposhnik
    int size = 0;
242 115329f1 Diego Biurrun
243 0b297700 Daniel Maas
    if (c->sys) {
244 01f4895c Michael Niedermayer
        avctx = c->vst->codec;
245 115329f1 Diego Biurrun
246 f94036f1 Diego Biurrun
        av_set_pts_info(c->vst, 64, c->sys->time_base.num,
247
                        c->sys->time_base.den);
248 4e92dabf Roman Shaposhnik
        avctx->time_base= c->sys->time_base;
249 f94036f1 Diego Biurrun
        if (!avctx->width){
250 0b297700 Daniel Maas
            avctx->width = c->sys->width;
251
            avctx->height = c->sys->height;
252 4325ffd0 Michael Niedermayer
        }
253 0b297700 Daniel Maas
        avctx->pix_fmt = c->sys->pix_fmt;
254 115329f1 Diego Biurrun
255 bb270c08 Diego Biurrun
        /* finding out SAR is a little bit messy */
256
        vsc_pack = dv_extract_pack(frame, dv_video_control);
257 f94036f1 Diego Biurrun
        apt      = frame[4] & 0x07;
258
        is16_9   = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
259
                                (!apt && (vsc_pack[2] & 0x07) == 0x07)));
260 59729451 Aurelien Jacobs
        c->vst->sample_aspect_ratio = c->sys->sar[is16_9];
261 4e92dabf Roman Shaposhnik
        avctx->bit_rate = av_rescale_q(c->sys->frame_size, (AVRational){8,1},
262
                                       c->sys->time_base);
263 0b297700 Daniel Maas
        size = c->sys->frame_size;
264 7458ccbb Roman Shaposhnik
    }
265
    return size;
266
}
267
268 115329f1 Diego Biurrun
/*
269 d4fc91b4 Panagiotis Issaris
 * The following 3 functions constitute our interface to the world
270 7458ccbb Roman Shaposhnik
 */
271
272 ddaae6a9 Roman Shaposhnik
DVDemuxContext* dv_init_demux(AVFormatContext *s)
273 7458ccbb Roman Shaposhnik
{
274
    DVDemuxContext *c;
275
276
    c = av_mallocz(sizeof(DVDemuxContext));
277
    if (!c)
278
        return NULL;
279
280 ddaae6a9 Roman Shaposhnik
    c->vst = av_new_stream(s, 0);
281 50883632 Roman Shaposhnik
    if (!c->vst) {
282 115329f1 Diego Biurrun
        av_free(c);
283 50883632 Roman Shaposhnik
        return NULL;
284
    }
285 9ee91c2f Michael Niedermayer
286 f94036f1 Diego Biurrun
    c->sys  = NULL;
287 ddaae6a9 Roman Shaposhnik
    c->fctx = s;
288 34fab90f Roman Shaposhnik
    memset(c->ast, 0, sizeof(c->ast));
289 f94036f1 Diego Biurrun
    c->ach    = 0;
290 8066e59f Roman Shaposhnik
    c->frames = 0;
291
    c->abytes = 0;
292 664f5836 Roman Shaposhnik
293 01f4895c Michael Niedermayer
    c->vst->codec->codec_type = CODEC_TYPE_VIDEO;
294 f94036f1 Diego Biurrun
    c->vst->codec->codec_id   = CODEC_ID_DVVIDEO;
295
    c->vst->codec->bit_rate   = 25000000;
296
    c->vst->start_time        = 0;
297 115329f1 Diego Biurrun
298 7458ccbb Roman Shaposhnik
    return c;
299 f20dca40 Fabrice Bellard
}
300
301 7458ccbb Roman Shaposhnik
int dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
302 f20dca40 Fabrice Bellard
{
303 7458ccbb Roman Shaposhnik
    int size = -1;
304 ddaae6a9 Roman Shaposhnik
    int i;
305
306 f94036f1 Diego Biurrun
    for (i = 0; i < c->ach; i++) {
307 ddaae6a9 Roman Shaposhnik
       if (c->ast[i] && c->audio_pkt[i].size) {
308
           *pkt = c->audio_pkt[i];
309 bb270c08 Diego Biurrun
           c->audio_pkt[i].size = 0;
310
           size = pkt->size;
311
           break;
312 ddaae6a9 Roman Shaposhnik
       }
313 425ed6e2 Fabrice Bellard
    }
314 7458ccbb Roman Shaposhnik
315
    return size;
316
}
317
318 115329f1 Diego Biurrun
int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
319 7458ccbb Roman Shaposhnik
                      uint8_t* buf, int buf_size)
320
{
321 ddaae6a9 Roman Shaposhnik
    int size, i;
322 f8007a14 Roman Shaposhnik
    uint8_t *ppcm[4] = {0};
323 115329f1 Diego Biurrun
324 c4e7baa8 Roman Shaposhnik
    if (buf_size < DV_PROFILE_BYTES ||
325
        !(c->sys = dv_frame_profile(buf)) ||
326
        buf_size < c->sys->frame_size) {
327
          return -1;   /* Broken frame, or not enough data */
328
    }
329 7458ccbb Roman Shaposhnik
330
    /* Queueing audio packet */
331
    /* FIXME: in case of no audio/bad audio we have to do something */
332 ddaae6a9 Roman Shaposhnik
    size = dv_extract_audio_info(c, buf);
333 f94036f1 Diego Biurrun
    for (i = 0; i < c->ach; i++) {
334 664f5836 Roman Shaposhnik
       c->audio_pkt[i].size = size;
335 01f4895c Michael Niedermayer
       c->audio_pkt[i].pts  = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;
336 f8007a14 Roman Shaposhnik
       ppcm[i] = c->audio_buf[i];
337 ddaae6a9 Roman Shaposhnik
    }
338 f8007a14 Roman Shaposhnik
    dv_extract_audio(buf, ppcm, c->sys);
339 8066e59f Roman Shaposhnik
    c->abytes += size;
340 115329f1 Diego Biurrun
341 75f2c209 Diego Biurrun
    /* We work with 720p frames split in half, thus even frames have
342
     * channels 0,1 and odd 2,3. */
343 f8007a14 Roman Shaposhnik
    if (c->sys->height == 720) {
344 f94036f1 Diego Biurrun
        if (buf[1] & 0x0C)
345 f8007a14 Roman Shaposhnik
            c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
346
        else
347
            c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
348
    }
349
350 7458ccbb Roman Shaposhnik
    /* Now it's time to return video packet */
351 ddaae6a9 Roman Shaposhnik
    size = dv_extract_video_info(c, buf);
352 ec820113 Fabrice Bellard
    av_init_packet(pkt);
353 f94036f1 Diego Biurrun
    pkt->data         = buf;
354
    pkt->size         = size;
355
    pkt->flags       |= PKT_FLAG_KEY;
356 7458ccbb Roman Shaposhnik
    pkt->stream_index = c->vst->id;
357 f94036f1 Diego Biurrun
    pkt->pts          = c->frames;
358 115329f1 Diego Biurrun
359 8066e59f Roman Shaposhnik
    c->frames++;
360 ddaae6a9 Roman Shaposhnik
361 7458ccbb Roman Shaposhnik
    return size;
362
}
363 115329f1 Diego Biurrun
364 83582de4 Michael Niedermayer
static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
365
                              int64_t timestamp, int flags)
366 8066e59f Roman Shaposhnik
{
367 0bf4e2b8 Nathan Kurz
    // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
368 01f4895c Michael Niedermayer
    const DVprofile* sys = dv_codec_profile(c->vst->codec);
369 eb1bde3e Michael Niedermayer
    int64_t offset;
370 899681cd Björn Axelsson
    int64_t size = url_fsize(s->pb);
371 83582de4 Michael Niedermayer
    int64_t max_offset = ((size-1) / sys->frame_size) * sys->frame_size;
372 0bf4e2b8 Nathan Kurz
373 eb1bde3e Michael Niedermayer
    offset = sys->frame_size * timestamp;
374 115329f1 Diego Biurrun
375 83582de4 Michael Niedermayer
    if (offset > max_offset) offset = max_offset;
376
    else if (offset < 0) offset = 0;
377 8066e59f Roman Shaposhnik
378 0bf4e2b8 Nathan Kurz
    return offset;
379 8066e59f Roman Shaposhnik
}
380
381 6eb2de74 Roman Shaposhnik
void dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
382 664f5836 Roman Shaposhnik
{
383 6eb2de74 Roman Shaposhnik
    c->frames= frame_offset;
384
    if (c->ach)
385 4e92dabf Roman Shaposhnik
        c->abytes= av_rescale_q(c->frames, c->sys->time_base,
386
                                (AVRational){8, c->ast[0]->codec->bit_rate});
387 664f5836 Roman Shaposhnik
    c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
388 f8007a14 Roman Shaposhnik
    c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
389 664f5836 Roman Shaposhnik
}
390
391 7458ccbb Roman Shaposhnik
/************************************************************
392
 * Implementation of the easiest DV storage of all -- raw DV.
393
 ************************************************************/
394 115329f1 Diego Biurrun
395 7458ccbb Roman Shaposhnik
typedef struct RawDVContext {
396 ddaae6a9 Roman Shaposhnik
    DVDemuxContext* dv_demux;
397 0b297700 Daniel Maas
    uint8_t         buf[DV_MAX_FRAME_SIZE];
398 7458ccbb Roman Shaposhnik
} RawDVContext;
399
400
static int dv_read_header(AVFormatContext *s,
401
                          AVFormatParameters *ap)
402
{
403 67d44b84 Reimar Döffinger
    unsigned state;
404 7458ccbb Roman Shaposhnik
    RawDVContext *c = s->priv_data;
405 0b297700 Daniel Maas
406 ddaae6a9 Roman Shaposhnik
    c->dv_demux = dv_init_demux(s);
407 c4e55470 Roman Shaposhnik
    if (!c->dv_demux)
408
        return -1;
409 115329f1 Diego Biurrun
410 67d44b84 Reimar Döffinger
    state = get_be32(s->pb);
411
    while ((state & 0xffffff7f) != 0x1f07003f) {
412
        if (url_feof(s->pb)) {
413
            av_log(s, AV_LOG_ERROR, "Cannot find DV header.\n");
414
            return -1;
415
        }
416
        state = (state << 8) | get_byte(s->pb);
417
    }
418
    AV_WB32(c->buf, state);
419
420
    if (get_buffer(s->pb, c->buf + 4, DV_PROFILE_BYTES - 4) <= 0 ||
421 899681cd Björn Axelsson
        url_fseek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0)
422 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
423 c4e55470 Roman Shaposhnik
424 0b297700 Daniel Maas
    c->dv_demux->sys = dv_frame_profile(c->buf);
425 f8007a14 Roman Shaposhnik
    if (!c->dv_demux->sys) {
426
        av_log(s, AV_LOG_ERROR, "Can't determine profile of DV input stream.\n");
427
        return -1;
428
    }
429
430 4e92dabf Roman Shaposhnik
    s->bit_rate = av_rescale_q(c->dv_demux->sys->frame_size, (AVRational){8,1},
431
                               c->dv_demux->sys->time_base);
432 115329f1 Diego Biurrun
433 c4e55470 Roman Shaposhnik
    return 0;
434 7458ccbb Roman Shaposhnik
}
435
436
437
static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
438
{
439
    int size;
440 115329f1 Diego Biurrun
    RawDVContext *c = s->priv_data;
441
442 7458ccbb Roman Shaposhnik
    size = dv_get_packet(c->dv_demux, pkt);
443 115329f1 Diego Biurrun
444 7458ccbb Roman Shaposhnik
    if (size < 0) {
445 6d8b21f3 Roman Shaposhnik
        if (!c->dv_demux->sys)
446
            return AVERROR(EIO);
447 0b297700 Daniel Maas
        size = c->dv_demux->sys->frame_size;
448 899681cd Björn Axelsson
        if (get_buffer(s->pb, c->buf, size) <= 0)
449 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
450 7458ccbb Roman Shaposhnik
451 bb270c08 Diego Biurrun
        size = dv_produce_packet(c->dv_demux, pkt, c->buf, size);
452 115329f1 Diego Biurrun
    }
453
454 7458ccbb Roman Shaposhnik
    return size;
455 f20dca40 Fabrice Bellard
}
456
457 115329f1 Diego Biurrun
static int dv_read_seek(AVFormatContext *s, int stream_index,
458 83582de4 Michael Niedermayer
                       int64_t timestamp, int flags)
459 8066e59f Roman Shaposhnik
{
460 f94036f1 Diego Biurrun
    RawDVContext *r   = s->priv_data;
461 83582de4 Michael Niedermayer
    DVDemuxContext *c = r->dv_demux;
462 f94036f1 Diego Biurrun
    int64_t offset    = dv_frame_offset(s, c, timestamp, flags);
463 83582de4 Michael Niedermayer
464 6eb2de74 Roman Shaposhnik
    dv_offset_reset(c, offset / c->sys->frame_size);
465 115329f1 Diego Biurrun
466 899681cd Björn Axelsson
    offset = url_fseek(s->pb, offset, SEEK_SET);
467 f94036f1 Diego Biurrun
    return (offset < 0) ? offset : 0;
468 8066e59f Roman Shaposhnik
}
469
470 c18a2692 Zdenek Kabelac
static int dv_read_close(AVFormatContext *s)
471 f20dca40 Fabrice Bellard
{
472 7458ccbb Roman Shaposhnik
    RawDVContext *c = s->priv_data;
473
    av_free(c->dv_demux);
474 f20dca40 Fabrice Bellard
    return 0;
475
}
476
477 e34a5996 Baptiste Coudurier
static int dv_probe(AVProbeData *p)
478
{
479
    unsigned state;
480
    int i;
481
482
    if (p->buf_size < 5)
483
        return 0;
484
485
    state = AV_RB32(p->buf);
486
    for (i = 4; i < p->buf_size; i++) {
487
        if ((state & 0xffffff7f) == 0x1f07003f)
488
            return AVPROBE_SCORE_MAX*3/4; // not max to avoid dv in mov to match
489
        state = (state << 8) | p->buf[i];
490
    }
491
492
    return 0;
493
}
494
495 b250f9c6 Aurelien Jacobs
#if CONFIG_DV_DEMUXER
496 ff70e601 Måns Rullgård
AVInputFormat dv_demuxer = {
497 118e91f3 Roman Shaposhnik
    "dv",
498 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("DV video format"),
499 7458ccbb Roman Shaposhnik
    sizeof(RawDVContext),
500 e34a5996 Baptiste Coudurier
    dv_probe,
501 118e91f3 Roman Shaposhnik
    dv_read_header,
502
    dv_read_packet,
503
    dv_read_close,
504 8066e59f Roman Shaposhnik
    dv_read_seek,
505 490c735d Roman Shaposhnik
    .extensions = "dv,dif",
506 118e91f3 Roman Shaposhnik
};
507 ff70e601 Måns Rullgård
#endif