Statistics
| Branch: | Revision:

ffmpeg / libavformat / dvenc.c @ 4647c0ec

History | View | Annotate | Download (15.5 KB)

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) support
12
 * Copyright (c) 2006 Daniel Maas <dmaas@maasdigital.com>
13
 *
14
 * This file is part of FFmpeg.
15
 *
16
 * FFmpeg is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU Lesser General Public
18
 * License as published by the Free Software Foundation; either
19
 * version 2.1 of the License, or (at your option) any later version.
20
 *
21
 * FFmpeg is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24
 * Lesser General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Lesser General Public
27
 * License along with FFmpeg; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29
 */
30
#include <time.h>
31
#include "avformat.h"
32
#include "dvdata.h"
33
#include "dv.h"
34
#include "fifo.h"
35

    
36
struct DVMuxContext {
37
    const DVprofile*  sys;    /* Current DV profile. E.g.: 525/60, 625/50 */
38
    int         n_ast;        /* Number of stereo audio streams (up to 2) */
39
    AVStream   *ast[2];       /* Stereo audio streams */
40
    AVFifoBuffer  audio_data[2]; /* Fifo for storing excessive amounts of PCM */
41
    int         frames;       /* Number of a current frame */
42
    time_t      start_time;   /* Start time of recording */
43
    int         has_audio;    /* frame under contruction has audio */
44
    int         has_video;    /* frame under contruction has video */
45
    uint8_t     frame_buf[DV_MAX_FRAME_SIZE]; /* frame under contruction */
46
};
47

    
48
static const int dv_aaux_packs_dist[12][9] = {
49
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
50
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
51
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
52
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
53
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
54
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
55
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
56
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
57
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
58
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
59
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
60
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
61
};
62

    
63
static int dv_audio_frame_size(const DVprofile* sys, int frame)
64
{
65
    return sys->audio_samples_dist[frame % (sizeof(sys->audio_samples_dist)/
66
                                            sizeof(sys->audio_samples_dist[0]))];
67
}
68

    
69
static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf)
70
{
71
    struct tm tc;
72
    time_t ct;
73
    int ltc_frame;
74

    
75
    buf[0] = (uint8_t)pack_id;
76
    switch (pack_id) {
77
    case dv_timecode:
78
          ct = (time_t)(c->frames / ((float)c->sys->frame_rate /
79
                                     (float)c->sys->frame_rate_base));
80
          brktimegm(ct, &tc);
81
          /*
82
           * LTC drop-frame frame counter drops two frames (0 and 1) every
83
           * minute, unless it is exactly divisible by 10
84
           */
85
          ltc_frame = (c->frames + 2*ct/60 - 2*ct/600) % c->sys->ltc_divisor;
86
          buf[1] = (0 << 7) | /* Color fame: 0 - unsync; 1 - sync mode */
87
                   (1 << 6) | /* Drop frame timecode: 0 - nondrop; 1 - drop */
88
                   ((ltc_frame / 10) << 4) | /* Tens of frames */
89
                   (ltc_frame % 10);         /* Units of frames */
90
          buf[2] = (1 << 7) | /* Biphase mark polarity correction: 0 - even; 1 - odd */
91
                   ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
92
                   (tc.tm_sec % 10);         /* Units of seconds */
93
          buf[3] = (1 << 7) | /* Binary group flag BGF0 */
94
                   ((tc.tm_min / 10) << 4) | /* Tens of minutes */
95
                   (tc.tm_min % 10);         /* Units of minutes */
96
          buf[4] = (1 << 7) | /* Binary group flag BGF2 */
97
                   (1 << 6) | /* Binary group flag BGF1 */
98
                   ((tc.tm_hour / 10) << 4) | /* Tens of hours */
99
                   (tc.tm_hour % 10);         /* Units of hours */
100
          break;
101
    case dv_audio_source:  /* AAUX source pack */
102
          buf[1] = (0 << 7) | /* locked mode       */
103
                   (1 << 6) | /* reserved -- always 1 */
104
                   (dv_audio_frame_size(c->sys, c->frames) -
105
                    c->sys->audio_min_samples[0]);
106
                              /* # of samples      */
107
          buf[2] = (0 << 7) | /* multi-stereo      */
108
                   (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */
109
                   (0 << 4) | /* pair bit: 0 -- one pair of channels */
110
                    0;        /* audio mode        */
111
          buf[3] = (1 << 7) | /* res               */
112
                   (1 << 6) | /* multi-language flag */
113
                   (c->sys->dsf << 5) | /*  system: 60fields/50fields */
114
                   (c->sys->n_difchan & 2); /* definition: 0 -- 25Mbps, 2 -- 50Mbps */
115
          buf[4] = (1 << 7) | /* emphasis: 1 -- off */
116
                   (0 << 6) | /* emphasis time constant: 0 -- reserved */
117
                   (0 << 3) | /* frequency: 0 -- 48Khz, 1 -- 44,1Khz, 2 -- 32Khz */
118
                    0;        /* quantization: 0 -- 16bit linear, 1 -- 12bit nonlinear */
119
          break;
120
    case dv_audio_control:
121
          buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */
122
                   (1 << 4) | /* input source: 1 -- digital input */
123
                   (3 << 2) | /* compression: 3 -- no information */
124
                    0;        /* misc. info/SMPTE emphasis off */
125
          buf[2] = (1 << 7) | /* recording start point: 1 -- no */
126
                   (1 << 6) | /* recording end point: 1 -- no */
127
                   (1 << 3) | /* recording mode: 1 -- original */
128
                    7;
129
          buf[3] = (1 << 7) | /* direction: 1 -- forward */
130
                   (c->sys->pix_fmt == PIX_FMT_YUV420P ? 0x20 : /* speed */
131
                                                         c->sys->ltc_divisor*4);
132
          buf[4] = (1 << 7) | /* reserved -- always 1 */
133
                    0x7f;     /* genre category */
134
          break;
135
    case dv_audio_recdate:
136
    case dv_video_recdate:  /* VAUX recording date */
137
          ct = c->start_time + (time_t)(c->frames /
138
               ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
139
          brktimegm(ct, &tc);
140
          buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */
141
                         /* 0xff is very likely to be "unknown" */
142
          buf[2] = (3 << 6) | /* reserved -- always 1 */
143
                   ((tc.tm_mday / 10) << 4) | /* Tens of day */
144
                   (tc.tm_mday % 10);         /* Units of day */
145
          buf[3] = /* we set high 4 bits to 0, shouldn't we set them to week? */
146
                   ((tc.tm_mon / 10) << 4) |    /* Tens of month */
147
                   (tc.tm_mon  % 10);           /* Units of month */
148
          buf[4] = (((tc.tm_year % 100) / 10) << 4) | /* Tens of year */
149
                   (tc.tm_year % 10);                 /* Units of year */
150
          break;
151
    case dv_audio_rectime:  /* AAUX recording time */
152
    case dv_video_rectime:  /* VAUX recording time */
153
          ct = c->start_time + (time_t)(c->frames /
154
               ((float)c->sys->frame_rate / (float)c->sys->frame_rate_base));
155
          brktimegm(ct, &tc);
156
          buf[1] = (3 << 6) | /* reserved -- always 1 */
157
                   0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */
158
          buf[2] = (1 << 7) | /* reserved -- always 1 */
159
                   ((tc.tm_sec / 10) << 4) | /* Tens of seconds */
160
                   (tc.tm_sec % 10);         /* Units of seconds */
161
          buf[3] = (1 << 7) | /* reserved -- always 1 */
162
                   ((tc.tm_min / 10) << 4) | /* Tens of minutes */
163
                   (tc.tm_min % 10);         /* Units of minutes */
164
          buf[4] = (3 << 6) | /* reserved -- always 1 */
165
                   ((tc.tm_hour / 10) << 4) | /* Tens of hours */
166
                   (tc.tm_hour % 10);         /* Units of hours */
167
          break;
168
    default:
169
          buf[1] = buf[2] = buf[3] = buf[4] = 0xff;
170
    }
171
    return 5;
172
}
173

    
174
static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr)
175
{
176
    int i, j, d, of, size;
177
    size = 4 * dv_audio_frame_size(c->sys, c->frames);
178
    frame_ptr += channel * c->sys->difseg_size * 150 * 80;
179
    for (i = 0; i < c->sys->difseg_size; i++) {
180
       frame_ptr += 6 * 80; /* skip DIF segment header */
181
       for (j = 0; j < 9; j++) {
182
          dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3]);
183
          for (d = 8; d < 80; d+=2) {
184
             of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride;
185
             if (of*2 >= size)
186
                 continue;
187

    
188
             frame_ptr[d] = av_fifo_peek(&c->audio_data[channel], of*2+1); // FIXME: may be we have to admit
189
             frame_ptr[d+1] = av_fifo_peek(&c->audio_data[channel], of*2); //        that DV is a big endian PCM
190
          }
191
          frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
192
       }
193
    }
194
}
195

    
196
static void dv_inject_metadata(DVMuxContext *c, uint8_t* frame)
197
{
198
    int j, k;
199
    uint8_t* buf;
200

    
201
    for (buf = frame; buf < frame + c->sys->frame_size; buf += 150 * 80) {
202
        /* DV subcode: 2nd and 3d DIFs */
203
        for (j = 80; j < 80 * 3; j += 80) {
204
            for (k = 6; k < 6 * 8; k += 8)
205
                dv_write_pack(dv_timecode, c, &buf[j+k]);
206

    
207
            if (((long)(buf-frame)/(c->sys->frame_size/(c->sys->difseg_size*c->sys->n_difchan))%c->sys->difseg_size) > 5) { /* FIXME: is this really needed ? */
208
                dv_write_pack(dv_video_recdate, c, &buf[j+14]);
209
                dv_write_pack(dv_video_rectime, c, &buf[j+22]);
210
                dv_write_pack(dv_video_recdate, c, &buf[j+38]);
211
                dv_write_pack(dv_video_rectime, c, &buf[j+46]);
212
            }
213
        }
214

    
215
        /* DV VAUX: 4th, 5th and 6th 3DIFs */
216
        for (j = 80*3 + 3; j < 80*6; j += 80) {
217
            dv_write_pack(dv_video_recdate, c, &buf[j+5*2]);
218
            dv_write_pack(dv_video_rectime, c, &buf[j+5*3]);
219
            dv_write_pack(dv_video_recdate, c, &buf[j+5*11]);
220
            dv_write_pack(dv_video_rectime, c, &buf[j+5*12]);
221
        }
222
    }
223
}
224

    
225
/*
226
 * The following 3 functions constitute our interface to the world
227
 */
228

    
229
int dv_assemble_frame(DVMuxContext *c, AVStream* st,
230
                      const uint8_t* data, int data_size, uint8_t** frame)
231
{
232
    int i, reqasize;
233

    
234
    *frame = &c->frame_buf[0];
235
    reqasize = 4 * dv_audio_frame_size(c->sys, c->frames);
236

    
237
    switch (st->codec->codec_type) {
238
    case CODEC_TYPE_VIDEO:
239
          /* FIXME: we have to have more sensible approach than this one */
240
          if (c->has_video)
241
              av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames);
242

    
243
          memcpy(*frame, data, c->sys->frame_size);
244
          c->has_video = 1;
245
          break;
246
    case CODEC_TYPE_AUDIO:
247
          for (i = 0; i < c->n_ast && st != c->ast[i]; i++);
248

    
249
          /* FIXME: we have to have more sensible approach than this one */
250
          if (av_fifo_size(&c->audio_data[i]) + data_size >= 100*AVCODEC_MAX_AUDIO_FRAME_SIZE)
251
              av_log(st->codec, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
252
          av_fifo_write(&c->audio_data[i], data, data_size);
253

    
254
          /* Lets see if we've got enough audio for one DV frame */
255
          c->has_audio |= ((reqasize <= av_fifo_size(&c->audio_data[i])) << i);
256

    
257
          break;
258
    default:
259
          break;
260
    }
261

    
262
    /* Lets see if we have enough data to construct one DV frame */
263
    if (c->has_video == 1 && c->has_audio + 1 == 1<<c->n_ast) {
264
        dv_inject_metadata(c, *frame);
265
        for (i=0; i<c->n_ast; i++) {
266
             dv_inject_audio(c, i, *frame);
267
             av_fifo_drain(&c->audio_data[i], reqasize);
268
        }
269

    
270
        c->has_video = 0;
271
        c->has_audio = 0;
272
        c->frames++;
273

    
274
        return c->sys->frame_size;
275
    }
276

    
277
    return 0;
278
}
279

    
280
DVMuxContext* dv_init_mux(AVFormatContext* s)
281
{
282
    DVMuxContext *c = (DVMuxContext *)s->priv_data;
283
    AVStream *vst = NULL;
284
    int i;
285

    
286
    /* we support at most 1 video and 2 audio streams */
287
    if (s->nb_streams > 3)
288
        return NULL;
289

    
290
    c->n_ast = 0;
291
    c->ast[0] = c->ast[1] = NULL;
292

    
293
    /* We have to sort out where audio and where video stream is */
294
    for (i=0; i<s->nb_streams; i++) {
295
         switch (s->streams[i]->codec->codec_type) {
296
         case CODEC_TYPE_VIDEO:
297
               vst = s->streams[i];
298
               break;
299
         case CODEC_TYPE_AUDIO:
300
             c->ast[c->n_ast++] = s->streams[i];
301
             break;
302
         default:
303
               goto bail_out;
304
         }
305
    }
306

    
307
    /* Some checks -- DV format is very picky about its incoming streams */
308
    if (!vst || vst->codec->codec_id != CODEC_ID_DVVIDEO)
309
        goto bail_out;
310
    for (i=0; i<c->n_ast; i++) {
311
        if (c->ast[i] && (c->ast[i]->codec->codec_id != CODEC_ID_PCM_S16LE ||
312
                          c->ast[i]->codec->sample_rate != 48000 ||
313
                          c->ast[i]->codec->channels != 2))
314
            goto bail_out;
315
    }
316
    c->sys = dv_codec_profile(vst->codec);
317
    if (!c->sys)
318
        goto bail_out;
319

    
320
    if((c->n_ast > 1) && (c->sys->n_difchan < 2)) {
321
        /* only 1 stereo pair is allowed in 25Mbps mode */
322
        goto bail_out;
323
    }
324

    
325
    /* Ok, everything seems to be in working order */
326
    c->frames = 0;
327
    c->has_audio = 0;
328
    c->has_video = 0;
329
    c->start_time = (time_t)s->timestamp;
330

    
331
    for (i=0; i<c->n_ast; i++) {
332
        if (c->ast[i] && av_fifo_init(&c->audio_data[i], 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) {
333
            while (i>0) {
334
                i--;
335
                av_fifo_free(&c->audio_data[i]);
336
            }
337
            goto bail_out;
338
        }
339
    }
340

    
341
    return c;
342

    
343
bail_out:
344
    return NULL;
345
}
346

    
347
void dv_delete_mux(DVMuxContext *c)
348
{
349
    int i;
350
    for (i=0; i < c->n_ast; i++)
351
        av_fifo_free(&c->audio_data[i]);
352
}
353

    
354
#ifdef CONFIG_MUXERS
355
static int dv_write_header(AVFormatContext *s)
356
{
357
    if (!dv_init_mux(s)) {
358
        av_log(s, AV_LOG_ERROR, "Can't initialize DV format!\n"
359
                    "Make sure that you supply exactly two streams:\n"
360
                    "     video: 25fps or 29.97fps, audio: 2ch/48Khz/PCM\n"
361
                    "     (50Mbps allows an optional second audio stream)\n");
362
        return -1;
363
    }
364
    return 0;
365
}
366

    
367
static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt)
368
{
369
    uint8_t* frame;
370
    int fsize;
371

    
372
    fsize = dv_assemble_frame((DVMuxContext *)s->priv_data, s->streams[pkt->stream_index],
373
                              pkt->data, pkt->size, &frame);
374
    if (fsize > 0) {
375
        put_buffer(&s->pb, frame, fsize);
376
        put_flush_packet(&s->pb);
377
    }
378
    return 0;
379
}
380

    
381
/*
382
 * We might end up with some extra A/V data without matching counterpart.
383
 * E.g. video data without enough audio to write the complete frame.
384
 * Currently we simply drop the last frame. I don't know whether this
385
 * is the best strategy of all
386
 */
387
static int dv_write_trailer(struct AVFormatContext *s)
388
{
389
    dv_delete_mux((DVMuxContext *)s->priv_data);
390
    return 0;
391
}
392
#endif /* CONFIG_MUXERS */
393

    
394
#ifdef CONFIG_DV_MUXER
395
AVOutputFormat dv_muxer = {
396
    "dv",
397
    "DV video format",
398
    NULL,
399
    "dv",
400
    sizeof(DVMuxContext),
401
    CODEC_ID_PCM_S16LE,
402
    CODEC_ID_DVVIDEO,
403
    dv_write_header,
404
    dv_write_packet,
405
    dv_write_trailer,
406
};
407
#endif