Statistics
| Branch: | Revision:

ffmpeg / libavformat / dvenc.c @ f5a478f6

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

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

    
46
static const int dv_aaux_packs_dist[12][9] = {
47
    { 0xff, 0xff, 0xff, 0x50, 0x51, 0x52, 0x53, 0xff, 0xff },
48
    { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff },
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
};
60

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

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

    
73
    /* Its hard to tell what SMPTE requires w.r.t. APT, but Quicktime needs it.
74
     * We set it based on pix_fmt value but it really should be per DV profile */
75
    int apt = (c->sys->pix_fmt == PIX_FMT_YUV422P ? 1 : 0);
76

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

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

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

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

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

    
208
            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 ? */
209
                dv_write_pack(dv_video_recdate, c, &buf[j+14]);
210
                dv_write_pack(dv_video_rectime, c, &buf[j+22]);
211
                dv_write_pack(dv_video_recdate, c, &buf[j+38]);
212
                dv_write_pack(dv_video_rectime, c, &buf[j+46]);
213
            }
214
        }
215

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

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

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

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

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

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

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

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

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

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

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

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

    
278
    return 0;
279
}
280

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

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

    
291
    c = av_mallocz(sizeof(DVMuxContext));
292
    if (!c)
293
        return NULL;
294

    
295
    c->n_ast = 0;
296
    c->ast[0] = c->ast[1] = NULL;
297

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

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

    
325
    if((c->n_ast > 1) && (c->sys->n_difchan < 2)) {
326
        /* only 1 stereo pair is allowed in 25Mbps mode */
327
        goto bail_out;
328
    }
329

    
330
    /* Ok, everything seems to be in working order */
331
    c->frames = 0;
332
    c->has_audio = 0;
333
    c->has_video = 0;
334
    c->start_time = (time_t)s->timestamp;
335

    
336
    for (i=0; i<c->n_ast; i++) {
337
        if (c->ast[i] && av_fifo_init(&c->audio_data[i], 100*AVCODEC_MAX_AUDIO_FRAME_SIZE) < 0) {
338
            while (i>0) {
339
                i--;
340
                av_fifo_free(&c->audio_data[i]);
341
            }
342
            goto bail_out;
343
        }
344
    }
345

    
346
    return c;
347

    
348
bail_out:
349
    av_free(c);
350
    return NULL;
351
}
352

    
353
void dv_delete_mux(DVMuxContext *c)
354
{
355
    int i;
356
    for (i=0; i < c->n_ast; i++)
357
        av_fifo_free(&c->audio_data[i]);
358
}
359

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

    
374
static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt)
375
{
376
    uint8_t* frame;
377
    int fsize;
378

    
379
    fsize = dv_assemble_frame((DVMuxContext *)s->priv_data, s->streams[pkt->stream_index],
380
                              pkt->data, pkt->size, &frame);
381
    if (fsize > 0) {
382
        put_buffer(&s->pb, frame, fsize);
383
        put_flush_packet(&s->pb);
384
    }
385
    return 0;
386
}
387

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

    
401
#ifdef CONFIG_DV_MUXER
402
AVOutputFormat dv_muxer = {
403
    "dv",
404
    "DV video format",
405
    NULL,
406
    "dv",
407
    sizeof(DVMuxContext),
408
    CODEC_ID_PCM_S16LE,
409
    CODEC_ID_DVVIDEO,
410
    dv_write_header,
411
    dv_write_packet,
412
    dv_write_trailer,
413
};
414
#endif