X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fdvenc.c;h=9a853ba7ceeb5ec8981bcfa3adad667a8fe1ac5e;hb=bc70684e74a185d7b80c8b80bdedda659cb581b8;hp=93c103b31655fc47fee238aa9cc6a68904f013aa;hpb=493240a522fca34882601fbeeda4e17aa40a0303;p=ffmpeg diff --git a/libavformat/dvenc.c b/libavformat/dvenc.c index 93c103b3165..9a853ba7cee 100644 --- a/libavformat/dvenc.c +++ b/libavformat/dvenc.c @@ -28,8 +28,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include -#include +#include "libavutil/time_internal.h" #include "avformat.h" #include "internal.h" #include "libavcodec/dv_profile.h" @@ -48,8 +48,8 @@ struct DVMuxContext { AVClass *av_class; const AVDVProfile* sys; /* current DV profile, e.g.: 525/60, 625/50 */ int n_ast; /* number of stereo audio streams (up to 2) */ - AVStream *ast[2]; /* stereo audio streams */ - AVFifoBuffer *audio_data[2]; /* FIFO for storing excessive amounts of PCM */ + AVStream *ast[4]; /* stereo audio streams */ + AVFifoBuffer *audio_data[4]; /* FIFO for storing excessive amounts of PCM */ int frames; /* current frame number */ int64_t start_time; /* recording start time */ int has_audio; /* frame under construction has audio */ @@ -73,6 +73,14 @@ static const int dv_aaux_packs_dist[12][9] = { { 0x50, 0x51, 0x52, 0x53, 0xff, 0xff, 0xff, 0xff, 0xff }, }; +static void brktimegm(time_t secs, struct tm *tm) +{ + tm = gmtime_r(&secs, tm); + + tm->tm_year += 1900; /* unlike gmtime_r we store complete year here */ + tm->tm_mon += 1; /* unlike gmtime_r tm_mon is from 1 to 12 */ +} + static int dv_audio_frame_size(const AVDVProfile* sys, int frame, int sample_rate) { if ((sys->time_base.den == 25 || sys->time_base.den == 50) && sys->time_base.num == 1) { @@ -87,14 +95,12 @@ static int dv_audio_frame_size(const AVDVProfile* sys, int frame, int sample_rat sizeof(sys->audio_samples_dist[0]))]; } -static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf, ...) +static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* buf, int channel, int seq) { struct tm tc; time_t ct; uint32_t timecode; - va_list ap; int audio_type = 0; - int channel; buf[0] = (uint8_t)pack_id; switch (pack_id) { @@ -104,8 +110,6 @@ static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* bu AV_WB32(buf + 1, timecode); break; case dv_audio_source: /* AAUX source pack */ - va_start(ap, buf); - channel = va_arg(ap, int); if (c->ast[channel]->codecpar->sample_rate == 44100) { audio_type = 1; } else if (c->ast[channel]->codecpar->sample_rate == 32000) @@ -118,17 +122,16 @@ static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* bu buf[2] = (0 << 7) | /* multi-stereo */ (0 << 5) | /* #of audio channels per block: 0 -- 1 channel */ (0 << 4) | /* pair bit: 0 -- one pair of channels */ - !!va_arg(ap, int); /* audio mode */ + (seq >= c->sys->difseg_size/2); /* audio mode (1st or 2nd channel) */ buf[3] = (1 << 7) | /* res */ (1 << 6) | /* multi-language flag */ (c->sys->dsf << 5) | /* system: 60fields/50fields */ - (c->sys->n_difchan & 2); /* definition: 0 -- 25Mbps, 2 -- 50Mbps */ + (DV_PROFILE_IS_HD(c->sys) ? 0x3 : c->sys->video_stype ? 2 : 0); /* stype */ buf[4] = (1 << 7) | /* emphasis: 1 -- off */ (0 << 6) | /* emphasis time constant: 0 -- reserved */ (audio_type << 3) | /* frequency: 0 -- 48kHz, 1 -- 44,1kHz, 2 -- 32kHz */ 0; /* quantization: 0 -- 16-bit linear, 1 -- 12-bit nonlinear */ - va_end(ap); break; case dv_audio_control: buf[1] = (0 << 6) | /* copy protection: 0 -- unrestricted */ @@ -149,7 +152,7 @@ static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* bu case dv_video_recdate: /* VAUX recording date */ ct = c->start_time + av_rescale_rnd(c->frames, c->sys->time_base.num, c->sys->time_base.den, AV_ROUND_DOWN); - ff_brktimegm(ct, &tc); + brktimegm(ct, &tc); buf[1] = 0xff; /* ds, tm, tens of time zone, units of time zone */ /* 0xff is very likely to be "unknown" */ buf[2] = (3 << 6) | /* reserved -- always 1 */ @@ -165,7 +168,7 @@ static int dv_write_pack(enum dv_pack_type pack_id, DVMuxContext *c, uint8_t* bu case dv_video_rectime: /* VAUX recording time */ ct = c->start_time + av_rescale_rnd(c->frames, c->sys->time_base.num, c->sys->time_base.den, AV_ROUND_DOWN); - ff_brktimegm(ct, &tc); + brktimegm(ct, &tc); buf[1] = (3 << 6) | /* reserved -- always 1 */ 0x3f; /* tens of frame, units of frame: 0x3f - "unknown" ? */ buf[2] = (1 << 7) | /* reserved -- always 1 */ @@ -192,7 +195,7 @@ static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr) for (i = 0; i < c->sys->difseg_size; i++) { frame_ptr += 6 * 80; /* skip DIF segment header */ for (j = 0; j < 9; j++) { - dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3], channel, i >= c->sys->difseg_size/2); + dv_write_pack(dv_aaux_packs_dist[i][j], c, &frame_ptr[3], channel, i); for (d = 8; d < 80; d+=2) { of = c->sys->audio_shuffle[i][j] + (d - 8)/2 * c->sys->audio_stride; if (of*2 >= size) @@ -210,27 +213,28 @@ static void dv_inject_metadata(DVMuxContext *c, uint8_t* frame) { int j, k; uint8_t* buf; + int seq = 0; - for (buf = frame; buf < frame + c->sys->frame_size; buf += 150 * 80) { + for (buf = frame; buf < frame + c->sys->frame_size; buf += 150 * 80, seq++) { /* DV subcode: 2nd and 3d DIFs */ for (j = 80; j < 80 * 3; j += 80) { for (k = 6; k < 6 * 8; k += 8) - dv_write_pack(dv_timecode, c, &buf[j+k]); + dv_write_pack(dv_timecode, c, &buf[j+k], 0, seq); 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 ? */ - dv_write_pack(dv_video_recdate, c, &buf[j+14]); - dv_write_pack(dv_video_rectime, c, &buf[j+22]); - dv_write_pack(dv_video_recdate, c, &buf[j+38]); - dv_write_pack(dv_video_rectime, c, &buf[j+46]); + dv_write_pack(dv_video_recdate, c, &buf[j+14], 0, seq); + dv_write_pack(dv_video_rectime, c, &buf[j+22], 0, seq); + dv_write_pack(dv_video_recdate, c, &buf[j+38], 0, seq); + dv_write_pack(dv_video_rectime, c, &buf[j+46], 0, seq); } } /* DV VAUX: 4th, 5th and 6th 3DIFs */ for (j = 80*3 + 3; j < 80*6; j += 80) { - dv_write_pack(dv_video_recdate, c, &buf[j+5*2]); - dv_write_pack(dv_video_rectime, c, &buf[j+5*3]); - dv_write_pack(dv_video_recdate, c, &buf[j+5*11]); - dv_write_pack(dv_video_rectime, c, &buf[j+5*12]); + dv_write_pack(dv_video_recdate, c, &buf[j+5* 2], 0, seq); + dv_write_pack(dv_video_rectime, c, &buf[j+5* 3], 0, seq); + dv_write_pack(dv_video_recdate, c, &buf[j+5*11], 0, seq); + dv_write_pack(dv_video_rectime, c, &buf[j+5*12], 0, seq); } } } @@ -307,42 +311,39 @@ static DVMuxContext* dv_init_mux(AVFormatContext* s) int i; /* we support at most 1 video and 2 audio streams */ - if (s->nb_streams > 3) + if (s->nb_streams > 5) return NULL; - c->n_ast = 0; - c->ast[0] = c->ast[1] = NULL; - /* We have to sort out where audio and where video stream is */ for (i=0; inb_streams; i++) { - switch (s->streams[i]->codecpar->codec_type) { + AVStream *st = s->streams[i]; + switch (st->codecpar->codec_type) { case AVMEDIA_TYPE_VIDEO: if (vst) return NULL; - vst = s->streams[i]; + if (st->codecpar->codec_id != AV_CODEC_ID_DVVIDEO) + goto bail_out; + vst = st; break; case AVMEDIA_TYPE_AUDIO: if (c->n_ast > 1) return NULL; - c->ast[c->n_ast++] = s->streams[i]; + /* Some checks -- DV format is very picky about its incoming streams */ + if(st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE || + st->codecpar->channels != 2) + goto bail_out; + if (st->codecpar->sample_rate != 48000 && + st->codecpar->sample_rate != 44100 && + st->codecpar->sample_rate != 32000 ) + goto bail_out; + c->ast[c->n_ast++] = st; break; default: goto bail_out; } } - /* Some checks -- DV format is very picky about its incoming streams */ - if (!vst || vst->codecpar->codec_id != AV_CODEC_ID_DVVIDEO) + if (!vst) goto bail_out; - for (i=0; in_ast; i++) { - if (c->ast[i]) { - if(c->ast[i]->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE || - c->ast[i]->codecpar->channels != 2) - goto bail_out; - if (c->ast[i]->codecpar->sample_rate != 48000 && - c->ast[i]->codecpar->sample_rate != 44100 && - c->ast[i]->codecpar->sample_rate != 32000 ) - goto bail_out; - } - } + c->sys = av_dv_codec_profile2(vst->codecpar->width, vst->codecpar->height, vst->codecpar->format, vst->time_base); if (!c->sys) @@ -355,8 +356,9 @@ static DVMuxContext* dv_init_mux(AVFormatContext* s) goto bail_out; } - if ((c->n_ast > 1) && (c->sys->n_difchan < 2)) { - /* only 1 stereo pair is allowed in 25Mbps mode */ + if (((c->n_ast > 1) && (c->sys->n_difchan < 2)) || + ((c->n_ast > 2) && (c->sys->n_difchan < 4))) { + /* only 2 stereo pairs allowed in 50Mbps mode */ goto bail_out; } @@ -368,10 +370,6 @@ static DVMuxContext* dv_init_mux(AVFormatContext* s) for (i=0; i < c->n_ast; i++) { if (c->ast[i] && !(c->audio_data[i]=av_fifo_alloc_array(100, MAX_AUDIO_FRAME_SIZE))) { - while (i > 0) { - i--; - av_fifo_freep(&c->audio_data[i]); - } goto bail_out; } } @@ -382,13 +380,6 @@ bail_out: return NULL; } -static void dv_delete_mux(DVMuxContext *c) -{ - int i; - for (i=0; i < c->n_ast; i++) - av_fifo_freep(&c->audio_data[i]); -} - static int dv_write_header(AVFormatContext *s) { AVRational rate; @@ -424,9 +415,10 @@ static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt) fsize = dv_assemble_frame(s, s->priv_data, s->streams[pkt->stream_index], pkt->data, pkt->size, &frame); - if (fsize > 0) { - avio_write(s->pb, frame, fsize); + if (fsize < 0) { + return fsize; } + avio_write(s->pb, frame, fsize); return 0; } @@ -436,13 +428,15 @@ static int dv_write_packet(struct AVFormatContext *s, AVPacket *pkt) * Currently we simply drop the last frame. I don't know whether this * is the best strategy of all */ -static int dv_write_trailer(struct AVFormatContext *s) +static void dv_deinit(AVFormatContext *s) { - dv_delete_mux(s->priv_data); - return 0; + DVMuxContext *c = s->priv_data; + + for (int i = 0; i < c->n_ast; i++) + av_fifo_freep(&c->audio_data[i]); } -AVOutputFormat ff_dv_muxer = { +const AVOutputFormat ff_dv_muxer = { .name = "dv", .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"), .extensions = "dv", @@ -451,5 +445,5 @@ AVOutputFormat ff_dv_muxer = { .video_codec = AV_CODEC_ID_DVVIDEO, .write_header = dv_write_header, .write_packet = dv_write_packet, - .write_trailer = dv_write_trailer, + .deinit = dv_deinit, };