Statistics
| Branch: | Revision:

ffmpeg / libavformat / dv.c @ d7fb5a18

History | View | Annotate | Download (14.5 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
 * Copyright (c) 2002 Fabrice Bellard.
10
 *
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 ddaae6a9 Roman Shaposhnik
#include "dv.h"
35
36
struct DVDemuxContext {
37 0b297700 Daniel Maas
    const DVprofile*  sys;    /* Current DV profile. E.g.: 525/60, 625/50 */
38 f94036f1 Diego Biurrun
    AVFormatContext*  fctx;
39
    AVStream*         vst;
40
    AVStream*         ast[4];
41
    AVPacket          audio_pkt[4];
42
    uint8_t           audio_buf[4][8192];
43
    int               ach;
44
    int               frames;
45
    uint64_t          abytes;
46 ddaae6a9 Roman Shaposhnik
};
47 f20dca40 Fabrice Bellard
48 7458ccbb Roman Shaposhnik
static inline uint16_t dv_audio_12to16(uint16_t sample)
49
{
50
    uint16_t shift, result;
51 115329f1 Diego Biurrun
52 7458ccbb Roman Shaposhnik
    sample = (sample < 0x800) ? sample : sample | 0xf000;
53 f94036f1 Diego Biurrun
    shift  = (sample & 0xf00) >> 8;
54 7458ccbb Roman Shaposhnik
55
    if (shift < 0x2 || shift > 0xd) {
56 bb270c08 Diego Biurrun
        result = sample;
57 7458ccbb Roman Shaposhnik
    } else if (shift < 0x8) {
58
        shift--;
59 bb270c08 Diego Biurrun
        result = (sample - (256 * shift)) << shift;
60 7458ccbb Roman Shaposhnik
    } else {
61 bb270c08 Diego Biurrun
        shift = 0xe - shift;
62
        result = ((sample + ((256 * shift) + 1)) << shift) - 1;
63 7458ccbb Roman Shaposhnik
    }
64
65
    return result;
66
}
67
68 115329f1 Diego Biurrun
/*
69 7458ccbb Roman Shaposhnik
 * 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.
72
 */
73
static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
74
{
75
    int offs;
76 115329f1 Diego Biurrun
77 7458ccbb Roman Shaposhnik
    switch (t) {
78
    case dv_audio_source:
79 f94036f1 Diego Biurrun
        offs = (80*6 + 80*16*3 + 3);
80
        break;
81 7458ccbb Roman Shaposhnik
    case dv_audio_control:
82 f94036f1 Diego Biurrun
        offs = (80*6 + 80*16*4 + 3);
83
        break;
84 7458ccbb Roman Shaposhnik
    case dv_video_control:
85 f94036f1 Diego Biurrun
        offs = (80*5 + 48 + 5);
86
        break;
87 7458ccbb Roman Shaposhnik
    default:
88 f94036f1 Diego Biurrun
        return NULL;
89 115329f1 Diego Biurrun
    }
90 7458ccbb Roman Shaposhnik
91 ccd425e7 Diego Biurrun
    return frame[offs] == t ? &frame[offs] : NULL;
92 7458ccbb Roman Shaposhnik
}
93
94 115329f1 Diego Biurrun
/*
95 7458ccbb Roman Shaposhnik
 * 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.
101
 */
102 f8007a14 Roman Shaposhnik
static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4],
103 0b297700 Daniel Maas
                            const DVprofile *sys)
104 7458ccbb Roman Shaposhnik
{
105 0b297700 Daniel Maas
    int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
106 7458ccbb Roman Shaposhnik
    uint16_t lc, rc;
107
    const uint8_t* as_pack;
108 f8007a14 Roman Shaposhnik
    uint8_t *pcm, ipcm;
109 115329f1 Diego Biurrun
110 7458ccbb Roman Shaposhnik
    as_pack = dv_extract_pack(frame, dv_audio_source);
111
    if (!as_pack)    /* No audio ? */
112 bb270c08 Diego Biurrun
        return 0;
113 115329f1 Diego Biurrun
114 f94036f1 Diego Biurrun
    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 */
117 115329f1 Diego Biurrun
118 7458ccbb Roman Shaposhnik
    if (quant > 1)
119 75f2c209 Diego Biurrun
        return -1; /* unsupported quantization */
120 7458ccbb Roman Shaposhnik
121
    size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
122 f94036f1 Diego Biurrun
    half_ch = sys->difseg_size / 2;
123 7458ccbb Roman Shaposhnik
124 75f2c209 Diego Biurrun
    /* We work with 720p frames split in half, thus even frames have
125 f94036f1 Diego Biurrun
     * channels 0,1 and odd 2,3. */
126
    ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;
127
    pcm  = ppcm[ipcm++];
128 f8007a14 Roman Shaposhnik
129 0b297700 Daniel Maas
    /* 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) */
136 f8007a14 Roman Shaposhnik
                pcm = ppcm[ipcm++];
137
                if (!pcm)
138 0b297700 Daniel Maas
                    break;
139
            }
140 bb270c08 Diego Biurrun
141 0b297700 Daniel Maas
            /* 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 f94036f1 Diego Biurrun
                        of = sys->audio_shuffle[i][j] + (d - 8) / 2 * sys->audio_stride;
146 0b297700 Daniel Maas
                        if (of*2 >= size)
147
                            continue;
148
149 910f02a0 Diego Biurrun
                        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 0b297700 Daniel Maas
                        if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
152
                            pcm[of*2+1] = 0;
153
                    } else {           /* 12bit quantization */
154 f94036f1 Diego Biurrun
                        lc = ((uint16_t)frame[d]   << 4) |
155 0b297700 Daniel Maas
                             ((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));
160
161 f94036f1 Diego Biurrun
                        of = sys->audio_shuffle[i%half_ch][j] + (d - 8) / 3 * sys->audio_stride;
162 0b297700 Daniel Maas
                        if (of*2 >= size)
163
                            continue;
164
165 910f02a0 Diego Biurrun
                        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 0b297700 Daniel Maas
                        of = sys->audio_shuffle[i%half_ch+half_ch][j] +
168 f94036f1 Diego Biurrun
                            (d - 8) / 3 * sys->audio_stride;
169 910f02a0 Diego Biurrun
                        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
171 0b297700 Daniel Maas
                        ++d;
172
                    }
173
                }
174
175
                frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
176
            }
177 7458ccbb Roman Shaposhnik
        }
178 0b297700 Daniel Maas
179 f8007a14 Roman Shaposhnik
        /* next stereo channel (50Mbps and 100Mbps only) */
180
        pcm = ppcm[ipcm++];
181
        if (!pcm)
182 0b297700 Daniel Maas
            break;
183 7458ccbb Roman Shaposhnik
    }
184
185
    return size;
186
}
187
188 ddaae6a9 Roman Shaposhnik
static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
189 7458ccbb Roman Shaposhnik
{
190
    const uint8_t* as_pack;
191 0b297700 Daniel Maas
    int freq, stype, smpls, quant, i, ach;
192 7458ccbb Roman Shaposhnik
193
    as_pack = dv_extract_pack(frame, dv_audio_source);
194 0b297700 Daniel Maas
    if (!as_pack || !c->sys) {    /* No audio ? */
195 ddaae6a9 Roman Shaposhnik
        c->ach = 0;
196 bb270c08 Diego Biurrun
        return 0;
197 ddaae6a9 Roman Shaposhnik
    }
198 115329f1 Diego Biurrun
199 f94036f1 Diego Biurrun
    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 */
203 0b297700 Daniel Maas
204
    /* note: ach counts PAIRS of channels (i.e. stereo channels) */
205 51f4155f Michael Niedermayer
    ach = ((int[4]){  1,  0,  2,  4})[stype];
206 a53621fc Roman Shaposhnik
    if (ach == 1 && quant && freq == 2)
207
        ach = 2;
208 50883632 Roman Shaposhnik
209
    /* Dynamic handling of the audio streams in DV */
210 f94036f1 Diego Biurrun
    for (i = 0; i < ach; i++) {
211 50883632 Roman Shaposhnik
       if (!c->ast[i]) {
212
           c->ast[i] = av_new_stream(c->fctx, 0);
213 bb270c08 Diego Biurrun
           if (!c->ast[i])
214
               break;
215
           av_set_pts_info(c->ast[i], 64, 1, 30000);
216
           c->ast[i]->codec->codec_type = CODEC_TYPE_AUDIO;
217 f94036f1 Diego Biurrun
           c->ast[i]->codec->codec_id   = CODEC_ID_PCM_S16LE;
218 664f5836 Roman Shaposhnik
219
           av_init_packet(&c->audio_pkt[i]);
220 f94036f1 Diego Biurrun
           c->audio_pkt[i].size         = 0;
221
           c->audio_pkt[i].data         = c->audio_buf[i];
222 664f5836 Roman Shaposhnik
           c->audio_pkt[i].stream_index = c->ast[i]->index;
223 f94036f1 Diego Biurrun
           c->audio_pkt[i].flags       |= PKT_FLAG_KEY;
224 50883632 Roman Shaposhnik
       }
225 01f4895c Michael Niedermayer
       c->ast[i]->codec->sample_rate = dv_audio_frequency[freq];
226 f94036f1 Diego Biurrun
       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;
229 ddaae6a9 Roman Shaposhnik
    }
230 50883632 Roman Shaposhnik
    c->ach = i;
231 115329f1 Diego Biurrun
232 0b297700 Daniel Maas
    return (c->sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
233 7458ccbb Roman Shaposhnik
}
234
235 ddaae6a9 Roman Shaposhnik
static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
236 7458ccbb Roman Shaposhnik
{
237
    const uint8_t* vsc_pack;
238 ddaae6a9 Roman Shaposhnik
    AVCodecContext* avctx;
239
    int apt, is16_9;
240 7458ccbb Roman Shaposhnik
    int size = 0;
241 115329f1 Diego Biurrun
242 0b297700 Daniel Maas
    if (c->sys) {
243 01f4895c Michael Niedermayer
        avctx = c->vst->codec;
244 115329f1 Diego Biurrun
245 f94036f1 Diego Biurrun
        av_set_pts_info(c->vst, 64, c->sys->time_base.num,
246
                        c->sys->time_base.den);
247 4e92dabf Roman Shaposhnik
        avctx->time_base= c->sys->time_base;
248 f94036f1 Diego Biurrun
        if (!avctx->width){
249 0b297700 Daniel Maas
            avctx->width = c->sys->width;
250
            avctx->height = c->sys->height;
251 4325ffd0 Michael Niedermayer
        }
252 0b297700 Daniel Maas
        avctx->pix_fmt = c->sys->pix_fmt;
253 115329f1 Diego Biurrun
254 bb270c08 Diego Biurrun
        /* finding out SAR is a little bit messy */
255
        vsc_pack = dv_extract_pack(frame, dv_video_control);
256 f94036f1 Diego Biurrun
        apt      = frame[4] & 0x07;
257
        is16_9   = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
258
                                (!apt && (vsc_pack[2] & 0x07) == 0x07)));
259 59729451 Aurelien Jacobs
        c->vst->sample_aspect_ratio = c->sys->sar[is16_9];
260 4e92dabf Roman Shaposhnik
        avctx->bit_rate = av_rescale_q(c->sys->frame_size, (AVRational){8,1},
261
                                       c->sys->time_base);
262 0b297700 Daniel Maas
        size = c->sys->frame_size;
263 7458ccbb Roman Shaposhnik
    }
264
    return size;
265
}
266
267 115329f1 Diego Biurrun
/*
268 d4fc91b4 Panagiotis Issaris
 * The following 3 functions constitute our interface to the world
269 7458ccbb Roman Shaposhnik
 */
270
271 ddaae6a9 Roman Shaposhnik
DVDemuxContext* dv_init_demux(AVFormatContext *s)
272 7458ccbb Roman Shaposhnik
{
273
    DVDemuxContext *c;
274
275
    c = av_mallocz(sizeof(DVDemuxContext));
276
    if (!c)
277
        return NULL;
278
279 ddaae6a9 Roman Shaposhnik
    c->vst = av_new_stream(s, 0);
280 50883632 Roman Shaposhnik
    if (!c->vst) {
281 115329f1 Diego Biurrun
        av_free(c);
282 50883632 Roman Shaposhnik
        return NULL;
283
    }
284 9ee91c2f Michael Niedermayer
285 f94036f1 Diego Biurrun
    c->sys  = NULL;
286 ddaae6a9 Roman Shaposhnik
    c->fctx = s;
287 34fab90f Roman Shaposhnik
    memset(c->ast, 0, sizeof(c->ast));
288 f94036f1 Diego Biurrun
    c->ach    = 0;
289 8066e59f Roman Shaposhnik
    c->frames = 0;
290
    c->abytes = 0;
291 664f5836 Roman Shaposhnik
292 01f4895c Michael Niedermayer
    c->vst->codec->codec_type = CODEC_TYPE_VIDEO;
293 f94036f1 Diego Biurrun
    c->vst->codec->codec_id   = CODEC_ID_DVVIDEO;
294
    c->vst->codec->bit_rate   = 25000000;
295
    c->vst->start_time        = 0;
296 115329f1 Diego Biurrun
297 7458ccbb Roman Shaposhnik
    return c;
298 f20dca40 Fabrice Bellard
}
299
300 7458ccbb Roman Shaposhnik
int dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
301 f20dca40 Fabrice Bellard
{
302 7458ccbb Roman Shaposhnik
    int size = -1;
303 ddaae6a9 Roman Shaposhnik
    int i;
304
305 f94036f1 Diego Biurrun
    for (i = 0; i < c->ach; i++) {
306 ddaae6a9 Roman Shaposhnik
       if (c->ast[i] && c->audio_pkt[i].size) {
307
           *pkt = c->audio_pkt[i];
308 bb270c08 Diego Biurrun
           c->audio_pkt[i].size = 0;
309
           size = pkt->size;
310
           break;
311 ddaae6a9 Roman Shaposhnik
       }
312 425ed6e2 Fabrice Bellard
    }
313 7458ccbb Roman Shaposhnik
314
    return size;
315
}
316
317 115329f1 Diego Biurrun
int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
318 7458ccbb Roman Shaposhnik
                      uint8_t* buf, int buf_size)
319
{
320 ddaae6a9 Roman Shaposhnik
    int size, i;
321 f8007a14 Roman Shaposhnik
    uint8_t *ppcm[4] = {0};
322 115329f1 Diego Biurrun
323 c4e7baa8 Roman Shaposhnik
    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 */
327
    }
328 7458ccbb Roman Shaposhnik
329
    /* Queueing audio packet */
330
    /* FIXME: in case of no audio/bad audio we have to do something */
331 ddaae6a9 Roman Shaposhnik
    size = dv_extract_audio_info(c, buf);
332 f94036f1 Diego Biurrun
    for (i = 0; i < c->ach; i++) {
333 664f5836 Roman Shaposhnik
       c->audio_pkt[i].size = size;
334 01f4895c Michael Niedermayer
       c->audio_pkt[i].pts  = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;
335 f8007a14 Roman Shaposhnik
       ppcm[i] = c->audio_buf[i];
336 ddaae6a9 Roman Shaposhnik
    }
337 f8007a14 Roman Shaposhnik
    dv_extract_audio(buf, ppcm, c->sys);
338 8066e59f Roman Shaposhnik
    c->abytes += size;
339 115329f1 Diego Biurrun
340 75f2c209 Diego Biurrun
    /* We work with 720p frames split in half, thus even frames have
341
     * channels 0,1 and odd 2,3. */
342 f8007a14 Roman Shaposhnik
    if (c->sys->height == 720) {
343 f94036f1 Diego Biurrun
        if (buf[1] & 0x0C)
344 f8007a14 Roman Shaposhnik
            c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
345
        else
346
            c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
347
    }
348
349 7458ccbb Roman Shaposhnik
    /* Now it's time to return video packet */
350 ddaae6a9 Roman Shaposhnik
    size = dv_extract_video_info(c, buf);
351 ec820113 Fabrice Bellard
    av_init_packet(pkt);
352 f94036f1 Diego Biurrun
    pkt->data         = buf;
353
    pkt->size         = size;
354
    pkt->flags       |= PKT_FLAG_KEY;
355 7458ccbb Roman Shaposhnik
    pkt->stream_index = c->vst->id;
356 f94036f1 Diego Biurrun
    pkt->pts          = c->frames;
357 115329f1 Diego Biurrun
358 8066e59f Roman Shaposhnik
    c->frames++;
359 ddaae6a9 Roman Shaposhnik
360 7458ccbb Roman Shaposhnik
    return size;
361
}
362 115329f1 Diego Biurrun
363 83582de4 Michael Niedermayer
static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
364
                              int64_t timestamp, int flags)
365 8066e59f Roman Shaposhnik
{
366 0bf4e2b8 Nathan Kurz
    // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
367 01f4895c Michael Niedermayer
    const DVprofile* sys = dv_codec_profile(c->vst->codec);
368 eb1bde3e Michael Niedermayer
    int64_t offset;
369 899681cd Björn Axelsson
    int64_t size = url_fsize(s->pb);
370 83582de4 Michael Niedermayer
    int64_t max_offset = ((size-1) / sys->frame_size) * sys->frame_size;
371 0bf4e2b8 Nathan Kurz
372 eb1bde3e Michael Niedermayer
    offset = sys->frame_size * timestamp;
373 115329f1 Diego Biurrun
374 83582de4 Michael Niedermayer
    if (offset > max_offset) offset = max_offset;
375
    else if (offset < 0) offset = 0;
376 8066e59f Roman Shaposhnik
377 0bf4e2b8 Nathan Kurz
    return offset;
378 8066e59f Roman Shaposhnik
}
379
380 6eb2de74 Roman Shaposhnik
void dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
381 664f5836 Roman Shaposhnik
{
382 6eb2de74 Roman Shaposhnik
    c->frames= frame_offset;
383
    if (c->ach)
384 4e92dabf Roman Shaposhnik
        c->abytes= av_rescale_q(c->frames, c->sys->time_base,
385
                                (AVRational){8, c->ast[0]->codec->bit_rate});
386 664f5836 Roman Shaposhnik
    c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
387 f8007a14 Roman Shaposhnik
    c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
388 664f5836 Roman Shaposhnik
}
389
390 7458ccbb Roman Shaposhnik
/************************************************************
391
 * Implementation of the easiest DV storage of all -- raw DV.
392
 ************************************************************/
393 115329f1 Diego Biurrun
394 7458ccbb Roman Shaposhnik
typedef struct RawDVContext {
395 ddaae6a9 Roman Shaposhnik
    DVDemuxContext* dv_demux;
396 0b297700 Daniel Maas
    uint8_t         buf[DV_MAX_FRAME_SIZE];
397 7458ccbb Roman Shaposhnik
} RawDVContext;
398
399
static int dv_read_header(AVFormatContext *s,
400
                          AVFormatParameters *ap)
401
{
402
    RawDVContext *c = s->priv_data;
403 0b297700 Daniel Maas
404 ddaae6a9 Roman Shaposhnik
    c->dv_demux = dv_init_demux(s);
405 c4e55470 Roman Shaposhnik
    if (!c->dv_demux)
406
        return -1;
407 115329f1 Diego Biurrun
408 899681cd Björn Axelsson
    if (get_buffer(s->pb, c->buf, DV_PROFILE_BYTES) <= 0 ||
409
        url_fseek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0)
410 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
411 c4e55470 Roman Shaposhnik
412 0b297700 Daniel Maas
    c->dv_demux->sys = dv_frame_profile(c->buf);
413 f8007a14 Roman Shaposhnik
    if (!c->dv_demux->sys) {
414
        av_log(s, AV_LOG_ERROR, "Can't determine profile of DV input stream.\n");
415
        return -1;
416
    }
417
418 4e92dabf Roman Shaposhnik
    s->bit_rate = av_rescale_q(c->dv_demux->sys->frame_size, (AVRational){8,1},
419
                               c->dv_demux->sys->time_base);
420 115329f1 Diego Biurrun
421 c4e55470 Roman Shaposhnik
    return 0;
422 7458ccbb Roman Shaposhnik
}
423
424
425
static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
426
{
427
    int size;
428 115329f1 Diego Biurrun
    RawDVContext *c = s->priv_data;
429
430 7458ccbb Roman Shaposhnik
    size = dv_get_packet(c->dv_demux, pkt);
431 115329f1 Diego Biurrun
432 7458ccbb Roman Shaposhnik
    if (size < 0) {
433 0b297700 Daniel Maas
        size = c->dv_demux->sys->frame_size;
434 899681cd Björn Axelsson
        if (get_buffer(s->pb, c->buf, size) <= 0)
435 6f3e0b21 Panagiotis Issaris
            return AVERROR(EIO);
436 7458ccbb Roman Shaposhnik
437 bb270c08 Diego Biurrun
        size = dv_produce_packet(c->dv_demux, pkt, c->buf, size);
438 115329f1 Diego Biurrun
    }
439
440 7458ccbb Roman Shaposhnik
    return size;
441 f20dca40 Fabrice Bellard
}
442
443 115329f1 Diego Biurrun
static int dv_read_seek(AVFormatContext *s, int stream_index,
444 83582de4 Michael Niedermayer
                       int64_t timestamp, int flags)
445 8066e59f Roman Shaposhnik
{
446 f94036f1 Diego Biurrun
    RawDVContext *r   = s->priv_data;
447 83582de4 Michael Niedermayer
    DVDemuxContext *c = r->dv_demux;
448 f94036f1 Diego Biurrun
    int64_t offset    = dv_frame_offset(s, c, timestamp, flags);
449 83582de4 Michael Niedermayer
450 6eb2de74 Roman Shaposhnik
    dv_offset_reset(c, offset / c->sys->frame_size);
451 115329f1 Diego Biurrun
452 899681cd Björn Axelsson
    offset = url_fseek(s->pb, offset, SEEK_SET);
453 f94036f1 Diego Biurrun
    return (offset < 0) ? offset : 0;
454 8066e59f Roman Shaposhnik
}
455
456 c18a2692 Zdenek Kabelac
static int dv_read_close(AVFormatContext *s)
457 f20dca40 Fabrice Bellard
{
458 7458ccbb Roman Shaposhnik
    RawDVContext *c = s->priv_data;
459
    av_free(c->dv_demux);
460 f20dca40 Fabrice Bellard
    return 0;
461
}
462
463 ff70e601 Måns Rullgård
#ifdef CONFIG_DV_DEMUXER
464
AVInputFormat dv_demuxer = {
465 118e91f3 Roman Shaposhnik
    "dv",
466 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("DV video format"),
467 7458ccbb Roman Shaposhnik
    sizeof(RawDVContext),
468 118e91f3 Roman Shaposhnik
    NULL,
469
    dv_read_header,
470
    dv_read_packet,
471
    dv_read_close,
472 8066e59f Roman Shaposhnik
    dv_read_seek,
473 490c735d Roman Shaposhnik
    .extensions = "dv,dif",
474 118e91f3 Roman Shaposhnik
};
475 ff70e601 Måns Rullgård
#endif