00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <time.h>
00032 #include "avformat.h"
00033 #include "internal.h"
00034 #include "libavcodec/dv_profile.h"
00035 #include "libavcodec/dvdata.h"
00036 #include "libavutil/channel_layout.h"
00037 #include "libavutil/intreadwrite.h"
00038 #include "libavutil/mathematics.h"
00039 #include "libavutil/timecode.h"
00040 #include "dv.h"
00041 #include "libavutil/avassert.h"
00042
00043 struct DVDemuxContext {
00044 const DVprofile* sys;
00045 AVFormatContext* fctx;
00046 AVStream* vst;
00047 AVStream* ast[4];
00048 AVPacket audio_pkt[4];
00049 uint8_t audio_buf[4][8192];
00050 int ach;
00051 int frames;
00052 uint64_t abytes;
00053 };
00054
00055 static inline uint16_t dv_audio_12to16(uint16_t sample)
00056 {
00057 uint16_t shift, result;
00058
00059 sample = (sample < 0x800) ? sample : sample | 0xf000;
00060 shift = (sample & 0xf00) >> 8;
00061
00062 if (shift < 0x2 || shift > 0xd) {
00063 result = sample;
00064 } else if (shift < 0x8) {
00065 shift--;
00066 result = (sample - (256 * shift)) << shift;
00067 } else {
00068 shift = 0xe - shift;
00069 result = ((sample + ((256 * shift) + 1)) << shift) - 1;
00070 }
00071
00072 return result;
00073 }
00074
00075
00076
00077
00078
00079
00080 static const uint8_t* dv_extract_pack(uint8_t* frame, enum dv_pack_type t)
00081 {
00082 int offs;
00083
00084 switch (t) {
00085 case dv_audio_source:
00086 offs = (80*6 + 80*16*3 + 3);
00087 break;
00088 case dv_audio_control:
00089 offs = (80*6 + 80*16*4 + 3);
00090 break;
00091 case dv_video_control:
00092 offs = (80*5 + 48 + 5);
00093 break;
00094 case dv_timecode:
00095 offs = (80*1 + 3 + 3);
00096 break;
00097 default:
00098 return NULL;
00099 }
00100
00101 return frame[offs] == t ? &frame[offs] : NULL;
00102 }
00103
00104 static const int dv_audio_frequency[3] = {
00105 48000, 44100, 32000,
00106 };
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 static int dv_extract_audio(uint8_t* frame, uint8_t* ppcm[4],
00117 const DVprofile *sys)
00118 {
00119 int size, chan, i, j, d, of, smpls, freq, quant, half_ch;
00120 uint16_t lc, rc;
00121 const uint8_t* as_pack;
00122 uint8_t *pcm, ipcm;
00123
00124 as_pack = dv_extract_pack(frame, dv_audio_source);
00125 if (!as_pack)
00126 return 0;
00127
00128 smpls = as_pack[1] & 0x3f;
00129 freq = (as_pack[4] >> 3) & 0x07;
00130 quant = as_pack[4] & 0x07;
00131
00132 if (quant > 1)
00133 return -1;
00134
00135 if (freq >= FF_ARRAY_ELEMS(dv_audio_frequency))
00136 return AVERROR_INVALIDDATA;
00137
00138 size = (sys->audio_min_samples[freq] + smpls) * 4;
00139 half_ch = sys->difseg_size / 2;
00140
00141
00142
00143 ipcm = (sys->height == 720 && !(frame[1] & 0x0C)) ? 2 : 0;
00144
00145 if (ipcm + sys->n_difchan > (quant == 1 ? 2 : 4)) {
00146 av_log(NULL, AV_LOG_ERROR, "too many dv pcm frames\n");
00147 return AVERROR_INVALIDDATA;
00148 }
00149
00150
00151 for (chan = 0; chan < sys->n_difchan; chan++) {
00152 av_assert0(ipcm<4);
00153 pcm = ppcm[ipcm++];
00154 if (!pcm)
00155 break;
00156
00157
00158 for (i = 0; i < sys->difseg_size; i++) {
00159 frame += 6 * 80;
00160 if (quant == 1 && i == half_ch) {
00161
00162 av_assert0(ipcm<4);
00163 pcm = ppcm[ipcm++];
00164 if (!pcm)
00165 break;
00166 }
00167
00168
00169 for (j = 0; j < 9; j++) {
00170 for (d = 8; d < 80; d += 2) {
00171 if (quant == 0) {
00172 of = sys->audio_shuffle[i][j] + (d - 8) / 2 * sys->audio_stride;
00173 if (of*2 >= size)
00174 continue;
00175
00176 pcm[of*2] = frame[d+1];
00177 pcm[of*2+1] = frame[d];
00178 if (pcm[of*2+1] == 0x80 && pcm[of*2] == 0x00)
00179 pcm[of*2+1] = 0;
00180 } else {
00181 lc = ((uint16_t)frame[d] << 4) |
00182 ((uint16_t)frame[d+2] >> 4);
00183 rc = ((uint16_t)frame[d+1] << 4) |
00184 ((uint16_t)frame[d+2] & 0x0f);
00185 lc = (lc == 0x800 ? 0 : dv_audio_12to16(lc));
00186 rc = (rc == 0x800 ? 0 : dv_audio_12to16(rc));
00187
00188 of = sys->audio_shuffle[i%half_ch][j] + (d - 8) / 3 * sys->audio_stride;
00189 if (of*2 >= size)
00190 continue;
00191
00192 pcm[of*2] = lc & 0xff;
00193 pcm[of*2+1] = lc >> 8;
00194 of = sys->audio_shuffle[i%half_ch+half_ch][j] +
00195 (d - 8) / 3 * sys->audio_stride;
00196 pcm[of*2] = rc & 0xff;
00197 pcm[of*2+1] = rc >> 8;
00198 ++d;
00199 }
00200 }
00201
00202 frame += 16 * 80;
00203 }
00204 }
00205 }
00206
00207 return size;
00208 }
00209
00210 static int dv_extract_audio_info(DVDemuxContext* c, uint8_t* frame)
00211 {
00212 const uint8_t* as_pack;
00213 int freq, stype, smpls, quant, i, ach;
00214
00215 as_pack = dv_extract_pack(frame, dv_audio_source);
00216 if (!as_pack || !c->sys) {
00217 c->ach = 0;
00218 return 0;
00219 }
00220
00221 smpls = as_pack[1] & 0x3f;
00222 freq = (as_pack[4] >> 3) & 0x07;
00223 stype = (as_pack[3] & 0x1f);
00224 quant = as_pack[4] & 0x07;
00225
00226 if (freq >= FF_ARRAY_ELEMS(dv_audio_frequency)) {
00227 av_log(c->fctx, AV_LOG_ERROR,
00228 "Unrecognized audio sample rate index (%d)\n", freq);
00229 return 0;
00230 }
00231
00232 if (stype > 3) {
00233 av_log(c->fctx, AV_LOG_ERROR, "stype %d is invalid\n", stype);
00234 c->ach = 0;
00235 return 0;
00236 }
00237
00238
00239 ach = ((int[4]){ 1, 0, 2, 4})[stype];
00240 if (ach == 1 && quant && freq == 2)
00241 ach = 2;
00242
00243
00244 for (i = 0; i < ach; i++) {
00245 if (!c->ast[i]) {
00246 c->ast[i] = avformat_new_stream(c->fctx, NULL);
00247 if (!c->ast[i])
00248 break;
00249 avpriv_set_pts_info(c->ast[i], 64, 1, 30000);
00250 c->ast[i]->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00251 c->ast[i]->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
00252
00253 av_init_packet(&c->audio_pkt[i]);
00254 c->audio_pkt[i].size = 0;
00255 c->audio_pkt[i].data = c->audio_buf[i];
00256 c->audio_pkt[i].stream_index = c->ast[i]->index;
00257 c->audio_pkt[i].flags |= AV_PKT_FLAG_KEY;
00258 }
00259 c->ast[i]->codec->sample_rate = dv_audio_frequency[freq];
00260 c->ast[i]->codec->channels = 2;
00261 c->ast[i]->codec->channel_layout = AV_CH_LAYOUT_STEREO;
00262 c->ast[i]->codec->bit_rate = 2 * dv_audio_frequency[freq] * 16;
00263 c->ast[i]->start_time = 0;
00264 }
00265 c->ach = i;
00266
00267 return (c->sys->audio_min_samples[freq] + smpls) * 4; ;
00268 }
00269
00270 static int dv_extract_video_info(DVDemuxContext *c, uint8_t* frame)
00271 {
00272 const uint8_t* vsc_pack;
00273 AVCodecContext* avctx;
00274 int apt, is16_9;
00275 int size = 0;
00276
00277 if (c->sys) {
00278 avctx = c->vst->codec;
00279
00280 avpriv_set_pts_info(c->vst, 64, c->sys->time_base.num,
00281 c->sys->time_base.den);
00282 avctx->time_base= c->sys->time_base;
00283
00284
00285 vsc_pack = dv_extract_pack(frame, dv_video_control);
00286 apt = frame[4] & 0x07;
00287 is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 ||
00288 (!apt && (vsc_pack[2] & 0x07) == 0x07)));
00289 c->vst->sample_aspect_ratio = c->sys->sar[is16_9];
00290 avctx->bit_rate = av_rescale_q(c->sys->frame_size, (AVRational){8,1},
00291 c->sys->time_base);
00292 size = c->sys->frame_size;
00293 }
00294 return size;
00295 }
00296
00297 static int dv_extract_timecode(DVDemuxContext* c, uint8_t* frame, char *tc)
00298 {
00299 const uint8_t *tc_pack;
00300
00301
00302
00303
00304 int prevent_df = c->sys->ltc_divisor == 25 || c->sys->ltc_divisor == 50;
00305
00306 tc_pack = dv_extract_pack(frame, dv_timecode);
00307 if (!tc_pack)
00308 return 0;
00309 av_timecode_make_smpte_tc_string(tc, AV_RB32(tc_pack + 1), prevent_df);
00310 return 1;
00311 }
00312
00313
00314
00315
00316
00317 DVDemuxContext* avpriv_dv_init_demux(AVFormatContext *s)
00318 {
00319 DVDemuxContext *c;
00320
00321 c = av_mallocz(sizeof(DVDemuxContext));
00322 if (!c)
00323 return NULL;
00324
00325 c->vst = avformat_new_stream(s, NULL);
00326 if (!c->vst) {
00327 av_free(c);
00328 return NULL;
00329 }
00330
00331 c->fctx = s;
00332 c->vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00333 c->vst->codec->codec_id = AV_CODEC_ID_DVVIDEO;
00334 c->vst->codec->bit_rate = 25000000;
00335 c->vst->start_time = 0;
00336
00337 return c;
00338 }
00339
00340 int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
00341 {
00342 int size = -1;
00343 int i;
00344
00345 for (i = 0; i < c->ach; i++) {
00346 if (c->ast[i] && c->audio_pkt[i].size) {
00347 *pkt = c->audio_pkt[i];
00348 c->audio_pkt[i].size = 0;
00349 size = pkt->size;
00350 break;
00351 }
00352 }
00353
00354 return size;
00355 }
00356
00357 int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt,
00358 uint8_t* buf, int buf_size, int64_t pos)
00359 {
00360 int size, i;
00361 uint8_t *ppcm[4] = {0};
00362
00363 if (buf_size < DV_PROFILE_BYTES ||
00364 !(c->sys = avpriv_dv_frame_profile(c->sys, buf, buf_size)) ||
00365 buf_size < c->sys->frame_size) {
00366 return -1;
00367 }
00368
00369
00370
00371 size = dv_extract_audio_info(c, buf);
00372 for (i = 0; i < c->ach; i++) {
00373 c->audio_pkt[i].pos = pos;
00374 c->audio_pkt[i].size = size;
00375 c->audio_pkt[i].pts = c->abytes * 30000 * 8 / c->ast[i]->codec->bit_rate;
00376 ppcm[i] = c->audio_buf[i];
00377 }
00378 if (c->ach)
00379 dv_extract_audio(buf, ppcm, c->sys);
00380
00381
00382
00383 if (c->sys->height == 720) {
00384 if (buf[1] & 0x0C) {
00385 c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
00386 } else {
00387 c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
00388 c->abytes += size;
00389 }
00390 } else {
00391 c->abytes += size;
00392 }
00393
00394
00395 size = dv_extract_video_info(c, buf);
00396 av_init_packet(pkt);
00397 pkt->data = buf;
00398 pkt->pos = pos;
00399 pkt->size = size;
00400 pkt->flags |= AV_PKT_FLAG_KEY;
00401 pkt->stream_index = c->vst->index;
00402 pkt->pts = c->frames;
00403
00404 c->frames++;
00405
00406 return size;
00407 }
00408
00409 static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
00410 int64_t timestamp, int flags)
00411 {
00412
00413 const DVprofile* sys = avpriv_dv_codec_profile(c->vst->codec);
00414 int64_t offset;
00415 int64_t size = avio_size(s->pb) - s->data_offset;
00416 int64_t max_offset = ((size-1) / sys->frame_size) * sys->frame_size;
00417
00418 offset = sys->frame_size * timestamp;
00419
00420 if (size >= 0 && offset > max_offset) offset = max_offset;
00421 else if (offset < 0) offset = 0;
00422
00423 return offset + s->data_offset;
00424 }
00425
00426 void ff_dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
00427 {
00428 c->frames= frame_offset;
00429 if (c->ach) {
00430 if (c->sys) {
00431 c->abytes= av_rescale_q(c->frames, c->sys->time_base,
00432 (AVRational){8, c->ast[0]->codec->bit_rate});
00433 }else
00434 av_log(c->fctx, AV_LOG_ERROR, "cannot adjust audio bytes\n");
00435 }
00436 c->audio_pkt[0].size = c->audio_pkt[1].size = 0;
00437 c->audio_pkt[2].size = c->audio_pkt[3].size = 0;
00438 }
00439
00440
00441
00442
00443
00444 typedef struct RawDVContext {
00445 DVDemuxContext* dv_demux;
00446 uint8_t buf[DV_MAX_FRAME_SIZE];
00447 } RawDVContext;
00448
00449 static int dv_read_timecode(AVFormatContext *s) {
00450 int ret;
00451 char timecode[AV_TIMECODE_STR_SIZE];
00452 int64_t pos = avio_tell(s->pb);
00453
00454
00455 int partial_frame_size = 3 * 80;
00456 uint8_t *partial_frame = av_mallocz(sizeof(*partial_frame) *
00457 partial_frame_size);
00458
00459 RawDVContext *c = s->priv_data;
00460 ret = avio_read(s->pb, partial_frame, partial_frame_size);
00461 if (ret < 0)
00462 goto finish;
00463
00464 if (ret < partial_frame_size) {
00465 ret = -1;
00466 goto finish;
00467 }
00468
00469 ret = dv_extract_timecode(c->dv_demux, partial_frame, timecode);
00470 if (ret)
00471 av_dict_set(&s->metadata, "timecode", timecode, 0);
00472 else
00473 av_log(s, AV_LOG_ERROR, "Detected timecode is invalid\n");
00474
00475 finish:
00476 av_free(partial_frame);
00477 avio_seek(s->pb, pos, SEEK_SET);
00478 return ret;
00479 }
00480
00481 static int dv_read_header(AVFormatContext *s)
00482 {
00483 unsigned state, marker_pos = 0;
00484 RawDVContext *c = s->priv_data;
00485
00486 c->dv_demux = avpriv_dv_init_demux(s);
00487 if (!c->dv_demux)
00488 return -1;
00489
00490 state = avio_rb32(s->pb);
00491 while ((state & 0xffffff7f) != 0x1f07003f) {
00492 if (url_feof(s->pb)) {
00493 av_log(s, AV_LOG_ERROR, "Cannot find DV header.\n");
00494 return -1;
00495 }
00496 if (state == 0x003f0700 || state == 0xff3f0700)
00497 marker_pos = avio_tell(s->pb);
00498 if (state == 0xff3f0701 && avio_tell(s->pb) - marker_pos == 80) {
00499 avio_seek(s->pb, -163, SEEK_CUR);
00500 state = avio_rb32(s->pb);
00501 break;
00502 }
00503 state = (state << 8) | avio_r8(s->pb);
00504 }
00505 AV_WB32(c->buf, state);
00506
00507 if (avio_read(s->pb, c->buf + 4, DV_PROFILE_BYTES - 4) != DV_PROFILE_BYTES - 4 ||
00508 avio_seek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0)
00509 return AVERROR(EIO);
00510
00511 c->dv_demux->sys = avpriv_dv_frame_profile(c->dv_demux->sys, c->buf, DV_PROFILE_BYTES);
00512 if (!c->dv_demux->sys) {
00513 av_log(s, AV_LOG_ERROR, "Can't determine profile of DV input stream.\n");
00514 return -1;
00515 }
00516
00517 s->bit_rate = av_rescale_q(c->dv_demux->sys->frame_size, (AVRational){8,1},
00518 c->dv_demux->sys->time_base);
00519
00520 if (s->pb->seekable)
00521 dv_read_timecode(s);
00522
00523 return 0;
00524 }
00525
00526
00527 static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
00528 {
00529 int size;
00530 RawDVContext *c = s->priv_data;
00531
00532 size = avpriv_dv_get_packet(c->dv_demux, pkt);
00533
00534 if (size < 0) {
00535 int64_t pos = avio_tell(s->pb);
00536 if (!c->dv_demux->sys)
00537 return AVERROR(EIO);
00538 size = c->dv_demux->sys->frame_size;
00539 if (avio_read(s->pb, c->buf, size) <= 0)
00540 return AVERROR(EIO);
00541
00542 size = avpriv_dv_produce_packet(c->dv_demux, pkt, c->buf, size, pos);
00543 }
00544
00545 return size;
00546 }
00547
00548 static int dv_read_seek(AVFormatContext *s, int stream_index,
00549 int64_t timestamp, int flags)
00550 {
00551 RawDVContext *r = s->priv_data;
00552 DVDemuxContext *c = r->dv_demux;
00553 int64_t offset = dv_frame_offset(s, c, timestamp, flags);
00554
00555 if (avio_seek(s->pb, offset, SEEK_SET) < 0)
00556 return -1;
00557
00558 ff_dv_offset_reset(c, offset / c->sys->frame_size);
00559 return 0;
00560 }
00561
00562 static int dv_read_close(AVFormatContext *s)
00563 {
00564 RawDVContext *c = s->priv_data;
00565 av_free(c->dv_demux);
00566 return 0;
00567 }
00568
00569 static int dv_probe(AVProbeData *p)
00570 {
00571 unsigned state, marker_pos = 0;
00572 int i;
00573 int matches = 0;
00574 int secondary_matches = 0;
00575
00576 if (p->buf_size < 5)
00577 return 0;
00578
00579 state = AV_RB32(p->buf);
00580 for (i = 4; i < p->buf_size; i++) {
00581 if ((state & 0xffffff7f) == 0x1f07003f)
00582 matches++;
00583
00584
00585 if ((state & 0xff07ff7f) == 0x1f07003f)
00586 secondary_matches++;
00587 if (state == 0x003f0700 || state == 0xff3f0700)
00588 marker_pos = i;
00589 if (state == 0xff3f0701 && i - marker_pos == 80)
00590 matches++;
00591 state = (state << 8) | p->buf[i];
00592 }
00593
00594 if (matches && p->buf_size / matches < 1024*1024) {
00595 if (matches > 4 || (secondary_matches >= 10 && p->buf_size / secondary_matches < 24000))
00596 return AVPROBE_SCORE_MAX*3/4;
00597 return AVPROBE_SCORE_MAX/4;
00598 }
00599 return 0;
00600 }
00601
00602 #if CONFIG_DV_DEMUXER
00603 AVInputFormat ff_dv_demuxer = {
00604 .name = "dv",
00605 .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
00606 .priv_data_size = sizeof(RawDVContext),
00607 .read_probe = dv_probe,
00608 .read_header = dv_read_header,
00609 .read_packet = dv_read_packet,
00610 .read_close = dv_read_close,
00611 .read_seek = dv_read_seek,
00612 .extensions = "dv,dif",
00613 };
00614 #endif