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 = AVMEDIA_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 |= AV_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 = AVMEDIA_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 = ff_dv_frame_profile(c->sys, buf, buf_size)) || 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 340 /* We work with 720p frames split in half, thus even frames have 341 * channels 0,1 and odd 2,3. */ 342 if (c->sys->height == 720) { 343 if (buf[1] & 0x0C) { 344 c->audio_pkt[2].size = c->audio_pkt[3].size = 0; 345 } else { 346 c->audio_pkt[0].size = c->audio_pkt[1].size = 0; 347 c->abytes += size; 348 } 349 } else { 350 c->abytes += size; 351 } 352 353 /* Now it's time to return video packet */ 354 size = dv_extract_video_info(c, buf); 355 av_init_packet(pkt); 356 pkt->data = buf; 357 pkt->size = size; 358 pkt->flags |= AV_PKT_FLAG_KEY; 359 pkt->stream_index = c->vst->id; 360 pkt->pts = c->frames; 361 362 c->frames++; 363 364 return size; 365} 366 367static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c, 368 int64_t timestamp, int flags) 369{ 370 // FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk) 371 const DVprofile* sys = ff_dv_codec_profile(c->vst->codec); 372 int64_t offset; 373 int64_t size = url_fsize(s->pb); 374 int64_t max_offset = ((size-1) / sys->frame_size) * sys->frame_size; 375 376 offset = sys->frame_size * timestamp; 377 378 if (size >= 0 && offset > max_offset) offset = max_offset; 379 else if (offset < 0) offset = 0; 380 381 return offset; 382} 383 384void dv_offset_reset(DVDemuxContext *c, int64_t frame_offset) 385{ 386 c->frames= frame_offset; 387 if (c->ach) 388 c->abytes= av_rescale_q(c->frames, c->sys->time_base, 389 (AVRational){8, c->ast[0]->codec->bit_rate}); 390 c->audio_pkt[0].size = c->audio_pkt[1].size = 0; 391 c->audio_pkt[2].size = c->audio_pkt[3].size = 0; 392} 393 394/************************************************************ 395 * Implementation of the easiest DV storage of all -- raw DV. 396 ************************************************************/ 397 398typedef struct RawDVContext { 399 DVDemuxContext* dv_demux; 400 uint8_t buf[DV_MAX_FRAME_SIZE]; 401} RawDVContext; 402 403static int dv_read_header(AVFormatContext *s, 404 AVFormatParameters *ap) 405{ 406 unsigned state, marker_pos = 0; 407 RawDVContext *c = s->priv_data; 408 409 c->dv_demux = dv_init_demux(s); 410 if (!c->dv_demux) 411 return -1; 412 413 state = get_be32(s->pb); 414 while ((state & 0xffffff7f) != 0x1f07003f) { 415 if (url_feof(s->pb)) { 416 av_log(s, AV_LOG_ERROR, "Cannot find DV header.\n"); 417 return -1; 418 } 419 if (state == 0x003f0700 || state == 0xff3f0700) 420 marker_pos = url_ftell(s->pb); 421 if (state == 0xff3f0701 && url_ftell(s->pb) - marker_pos == 80) { 422 url_fseek(s->pb, -163, SEEK_CUR); 423 state = get_be32(s->pb); 424 break; 425 } 426 state = (state << 8) | get_byte(s->pb); 427 } 428 AV_WB32(c->buf, state); 429 430 if (get_buffer(s->pb, c->buf + 4, DV_PROFILE_BYTES - 4) <= 0 || 431 url_fseek(s->pb, -DV_PROFILE_BYTES, SEEK_CUR) < 0) 432 return AVERROR(EIO); 433 434 c->dv_demux->sys = ff_dv_frame_profile(c->dv_demux->sys, c->buf, DV_PROFILE_BYTES); 435 if (!c->dv_demux->sys) { 436 av_log(s, AV_LOG_ERROR, "Can't determine profile of DV input stream.\n"); 437 return -1; 438 } 439 440 s->bit_rate = av_rescale_q(c->dv_demux->sys->frame_size, (AVRational){8,1}, 441 c->dv_demux->sys->time_base); 442 443 return 0; 444} 445 446 447static int dv_read_packet(AVFormatContext *s, AVPacket *pkt) 448{ 449 int size; 450 RawDVContext *c = s->priv_data; 451 452 size = dv_get_packet(c->dv_demux, pkt); 453 454 if (size < 0) { 455 if (!c->dv_demux->sys) 456 return AVERROR(EIO); 457 size = c->dv_demux->sys->frame_size; 458 if (get_buffer(s->pb, c->buf, size) <= 0) 459 return AVERROR(EIO); 460 461 size = dv_produce_packet(c->dv_demux, pkt, c->buf, size); 462 } 463 464 return size; 465} 466 467static int dv_read_seek(AVFormatContext *s, int stream_index, 468 int64_t timestamp, int flags) 469{ 470 RawDVContext *r = s->priv_data; 471 DVDemuxContext *c = r->dv_demux; 472 int64_t offset = dv_frame_offset(s, c, timestamp, flags); 473 474 dv_offset_reset(c, offset / c->sys->frame_size); 475 476 offset = url_fseek(s->pb, offset, SEEK_SET); 477 return (offset < 0) ? offset : 0; 478} 479 480static int dv_read_close(AVFormatContext *s) 481{ 482 RawDVContext *c = s->priv_data; 483 av_free(c->dv_demux); 484 return 0; 485} 486 487static int dv_probe(AVProbeData *p) 488{ 489 unsigned state, marker_pos = 0; 490 int i; 491 int matches = 0; 492 int secondary_matches = 0; 493 494 if (p->buf_size < 5) 495 return 0; 496 497 state = AV_RB32(p->buf); 498 for (i = 4; i < p->buf_size; i++) { 499 if ((state & 0xffffff7f) == 0x1f07003f) 500 matches++; 501 // any section header, also with seq/chan num != 0, 502 // should appear around every 12000 bytes, at least 10 per frame 503 if ((state & 0xff07ff7f) == 0x1f07003f) 504 secondary_matches++; 505 if (state == 0x003f0700 || state == 0xff3f0700) 506 marker_pos = i; 507 if (state == 0xff3f0701 && i - marker_pos == 80) 508 matches++; 509 state = (state << 8) | p->buf[i]; 510 } 511 512 if (matches && p->buf_size / matches < 1024*1024) { 513 if (matches > 4 || (secondary_matches >= 10 && p->buf_size / secondary_matches < 24000)) 514 return AVPROBE_SCORE_MAX*3/4; // not max to avoid dv in mov to match 515 return AVPROBE_SCORE_MAX/4; 516 } 517 return 0; 518} 519 520#if CONFIG_DV_DEMUXER 521AVInputFormat dv_demuxer = { 522 "dv", 523 NULL_IF_CONFIG_SMALL("DV video format"), 524 sizeof(RawDVContext), 525 dv_probe, 526 dv_read_header, 527 dv_read_packet, 528 dv_read_close, 529 dv_read_seek, 530 .extensions = "dv,dif", 531}; 532#endif 533