1/*
2 * General DV muxer/demuxer
3 * Copyright (c) 2003 Roman Shaposhnik
4 *
5 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
6 * of DV technical info.
7 *
8 * Raw DV format
9 * Copyright (c) 2002 Fabrice Bellard
10 *
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
14 *
15 * This file is part of FFmpeg.
16 *
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.
21 *
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.
26 *
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
30 */
31#include <time.h>
32#include "avformat.h"
33#include "libavcodec/dvdata.h"
34#include "libavutil/intreadwrite.h"
35#include "dv.h"
36
37struct DVDemuxContext {
38    const DVprofile*  sys;    /* Current DV profile. E.g.: 525/60, 625/50 */
39    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};
48
49static inline uint16_t dv_audio_12to16(uint16_t sample)
50{
51    uint16_t shift, result;
52
53    sample = (sample < 0x800) ? sample : sample | 0xf000;
54    shift  = (sample & 0xf00) >> 8;
55
56    if (shift < 0x2 || shift > 0xd) {
57        result = sample;
58    } else if (shift < 0x8) {
59        shift--;
60        result = (sample - (256 * shift)) << shift;
61    } else {
62        shift = 0xe - shift;
63        result = ((sample + ((256 * shift) + 1)) << shift) - 1;
64    }
65
66    return result;
67}
68
69/*
70 * 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 */
74static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
75{
76    int offs;
77
78    switch (t) {
79    case dv_audio_source:
80        offs = (80*6 + 80*16*3 + 3);
81        break;
82    case dv_audio_control:
83        offs = (80*6 + 80*16*4 + 3);
84        break;
85    case dv_video_control:
86        offs = (80*5 + 48 + 5);
87        break;
88    default:
89        return NULL;
90    }
91
92    return frame[offs] == t ? &frame[offs] : NULL;
93}
94
95/*
96 * 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 */
103static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4],
104                            const DVprofile *sys)
105{
106    int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
107    uint16_t lc, rc;
108    const uint8_t* as_pack;
109    uint8_t *pcm, ipcm;
110
111    as_pack = dv_extract_pack(frame, dv_audio_source);
112    if (!as_pack)    /* No audio ? */
113        return 0;
114
115    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
119    if (quant > 1)
120        return -1; /* unsupported quantization */
121
122    size = (sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */
123    half_ch = sys->difseg_size / 2;
124
125    /* We work with 720p frames split in half, thus even frames have
126     * channels 0,1 and odd 2,3. */
127    ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;
128    pcm  = ppcm[ipcm++];
129
130    /* 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                pcm = ppcm[ipcm++];
138                if (!pcm)
139                    break;
140            }
141
142            /* 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                        of = sys->audio_shuffle[i][j] + (d - 8) / 2 * sys->audio_stride;
147                        if (of*2 >= size)
148                            continue;
149
150                        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                        if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
153                            pcm[of*2+1] = 0;
154                    } else {           /* 12bit quantization */
155                        lc = ((uint16_t)frame[d]   << 4) |
156                             ((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                        of = sys->audio_shuffle[i%half_ch][j] + (d - 8) / 3 * sys->audio_stride;
163                        if (of*2 >= size)
164                            continue;
165
166                        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                        of = sys->audio_shuffle[i%half_ch+half_ch][j] +
169                            (d - 8) / 3 * sys->audio_stride;
170                        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                        ++d;
173                    }
174                }
175
176                frame += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
177            }
178        }
179
180        /* next stereo channel (50Mbps and 100Mbps only) */
181        pcm = ppcm[ipcm++];
182        if (!pcm)
183            break;
184    }
185
186    return size;
187}
188
189static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
190{
191    const uint8_t* as_pack;
192    int freq, stype, smpls, quant, i, ach;
193
194    as_pack = dv_extract_pack(frame, dv_audio_source);
195    if (!as_pack || !c->sys) {    /* No audio ? */
196        c->ach = 0;
197        return 0;
198    }
199
200    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
205    /* note: ach counts PAIRS of channels (i.e. stereo channels) */
206    ach = ((int[4]){  1,  0,  2,  4})[stype];
207    if (ach == 1 && quant && freq == 2)
208        ach = 2;
209
210    /* Dynamic handling of the audio streams in DV */
211    for (i = 0; i < ach; i++) {
212       if (!c->ast[i]) {
213           c->ast[i] = av_new_stream(c->fctx, 0);
214           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           c->ast[i]->codec->codec_id   = CODEC_ID_PCM_S16LE;
219
220           av_init_packet(&c->audio_pkt[i]);
221           c->audio_pkt[i].size         = 0;
222           c->audio_pkt[i].data         = c->audio_buf[i];
223           c->audio_pkt[i].stream_index = c->ast[i]->index;
224           c->audio_pkt[i].flags       |= PKT_FLAG_KEY;
225       }
226       c->ast[i]->codec->sample_rate = dv_audio_frequency[freq];
227       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    }
231    c->ach = i;
232
233    return (c->sys->audio_min_samples[freq] + smpls) * 4; /* 2ch, 2bytes */;
234}
235
236static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
237{
238    const uint8_t* vsc_pack;
239    AVCodecContext* avctx;
240    int apt, is16_9;
241    int size = 0;
242
243    if (c->sys) {
244        avctx = c->vst->codec;
245
246        av_set_pts_info(c->vst, 64, c->sys->time_base.num,
247                        c->sys->time_base.den);
248        avctx->time_base= c->sys->time_base;
249        if (!avctx->width){
250            avctx->width = c->sys->width;
251            avctx->height = c->sys->height;
252        }
253        avctx->pix_fmt = c->sys->pix_fmt;
254
255        /* finding out SAR is a little bit messy */
256        vsc_pack = dv_extract_pack(frame, dv_video_control);
257        apt      = frame[4] & 0x07;
258        is16_9   = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
259                                (!apt && (vsc_pack[2] & 0x07) == 0x07)));
260        c->vst->sample_aspect_ratio = c->sys->sar[is16_9];
261        avctx->bit_rate = av_rescale_q(c->sys->frame_size, (AVRational){8,1},
262                                       c->sys->time_base);
263        size = c->sys->frame_size;
264    }
265    return size;
266}
267
268/*
269 * The following 3 functions constitute our interface to the world
270 */
271
272DVDemuxContext* dv_init_demux(AVFormatContext *s)
273{
274    DVDemuxContext *c;
275
276    c = av_mallocz(sizeof(DVDemuxContext));
277    if (!c)
278        return NULL;
279
280    c->vst = av_new_stream(s, 0);
281    if (!c->vst) {
282        av_free(c);
283        return NULL;
284    }
285
286    c->sys  = NULL;
287    c->fctx = s;
288    memset(c->ast, 0, sizeof(c->ast));
289    c->ach    = 0;
290    c->frames = 0;
291    c->abytes = 0;
292
293    c->vst->codec->codec_type = CODEC_TYPE_VIDEO;
294    c->vst->codec->codec_id   = CODEC_ID_DVVIDEO;
295    c->vst->codec->bit_rate   = 25000000;
296    c->vst->start_time        = 0;
297
298    return c;
299}
300
301int dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
302{
303    int size = -1;
304    int i;
305
306    for (i = 0; i < c->ach; i++) {
307       if (c->ast[i] && c->audio_pkt[i].size) {
308           *pkt = c->audio_pkt[i];
309           c->audio_pkt[i].size = 0;
310           size = pkt->size;
311           break;
312       }
313    }
314
315    return size;
316}
317
318int dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
319                      uint8_t* buf, int buf_size)
320{
321    int size, i;
322    uint8_t *ppcm[4] = {0};
323
324    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
330    /* Queueing audio packet */
331    /* FIXME: in case of no audio/bad audio we have to do something */
332    size = dv_extract_audio_info(c, buf);
333    for (i = 0; i < c->ach; i++) {
334       c->audio_pkt[i].size = size;
335       c->audio_pkt[i].pts  = c->abytes * 30000*8 / c->ast[i]->codec->bit_rate;
336       ppcm[i] = c->audio_buf[i];
337    }
338    dv_extract_audio(buf, ppcm, c->sys);
339    c->abytes += size;
340
341    /* We work with 720p frames split in half, thus even frames have
342     * channels 0,1 and odd 2,3. */
343    if (c->sys->height == 720) {
344        if (buf[1] & 0x0C)
345            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    /* Now it's time to return video packet */
351    size = dv_extract_video_info(c, buf);
352    av_init_packet(pkt);
353    pkt->data         = buf;
354    pkt->size         = size;
355    pkt->flags       |= PKT_FLAG_KEY;
356    pkt->stream_index = c->vst->id;
357    pkt->pts          = c->frames;
358
359    c->frames++;
360
361    return size;
362}
363
364static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
365                              int64_t timestamp, int flags)
366{
367    // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
368    const DVprofile* sys = dv_codec_profile(c->vst->codec);
369    int64_t offset;
370    int64_t size = url_fsize(s->pb);
371    int64_t max_offset = ((size-1) / sys->frame_size) * sys->frame_size;
372
373    offset = sys->frame_size * timestamp;
374
375    if (offset > max_offset) offset = max_offset;
376    else if (offset < 0) offset = 0;
377
378    return offset;
379}
380
381void dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
382{
383    c->frames= frame_offset;
384    if (c->ach)
385        c->abytes= av_rescale_q(c->frames, c->sys->time_base,
386                                (AVRational){8, c->ast[0]->codec->bit_rate});
387    c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
388    c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
389}
390
391/************************************************************
392 * Implementation of the easiest DV storage of all -- raw DV.
393 ************************************************************/
394
395typedef struct RawDVContext {
396    DVDemuxContext* dv_demux;
397    uint8_t         buf[DV_MAX_FRAME_SIZE];
398} RawDVContext;
399
400static int dv_read_header(AVFormatContext *s,
401                          AVFormatParameters *ap)
402{
403    unsigned state;
404    RawDVContext *c = s->priv_data;
405
406    c->dv_demux = dv_init_demux(s);
407    if (!c->dv_demux)
408        return -1;
409
410    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        url_fseek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0)
422        return AVERROR(EIO);
423
424    c->dv_demux->sys = dv_frame_profile(c->buf);
425    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    s->bit_rate = av_rescale_q(c->dv_demux->sys->frame_size, (AVRational){8,1},
431                               c->dv_demux->sys->time_base);
432
433    return 0;
434}
435
436
437static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
438{
439    int size;
440    RawDVContext *c = s->priv_data;
441
442    size = dv_get_packet(c->dv_demux, pkt);
443
444    if (size < 0) {
445        if (!c->dv_demux->sys)
446            return AVERROR(EIO);
447        size = c->dv_demux->sys->frame_size;
448        if (get_buffer(s->pb, c->buf, size) <= 0)
449            return AVERROR(EIO);
450
451        size = dv_produce_packet(c->dv_demux, pkt, c->buf, size);
452    }
453
454    return size;
455}
456
457static int dv_read_seek(AVFormatContext *s, int stream_index,
458                       int64_t timestamp, int flags)
459{
460    RawDVContext *r   = s->priv_data;
461    DVDemuxContext *c = r->dv_demux;
462    int64_t offset    = dv_frame_offset(s, c, timestamp, flags);
463
464    dv_offset_reset(c, offset / c->sys->frame_size);
465
466    offset = url_fseek(s->pb, offset, SEEK_SET);
467    return (offset < 0) ? offset : 0;
468}
469
470static int dv_read_close(AVFormatContext *s)
471{
472    RawDVContext *c = s->priv_data;
473    av_free(c->dv_demux);
474    return 0;
475}
476
477static 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#if CONFIG_DV_DEMUXER
496AVInputFormat dv_demuxer = {
497    "dv",
498    NULL_IF_CONFIG_SMALL("DV video format"),
499    sizeof(RawDVContext),
500    dv_probe,
501    dv_read_header,
502    dv_read_packet,
503    dv_read_close,
504    dv_read_seek,
505    .extensions = "dv,dif",
506};
507#endif
508