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 #include "libavutil/avassert.h"
00027 #include "libavutil/dict.h"
00028 #include "libavutil/log.h"
00029 #include "libavutil/mathematics.h"
00030 #include "libavutil/opt.h"
00031 #include "avformat.h"
00032 #include "internal.h"
00033 #include "avio_internal.h"
00034 #include "pcm.h"
00035 #include "riff.h"
00036 #include "avio.h"
00037 #include "metadata.h"
00038
00039 typedef struct WAVDemuxContext {
00040 const AVClass *class;
00041 int64_t data_end;
00042 int w64;
00043 int64_t smv_data_ofs;
00044 int smv_block_size;
00045 int smv_frames_per_jpeg;
00046 int smv_block;
00047 int smv_last_stream;
00048 int smv_eof;
00049 int audio_eof;
00050 int ignore_length;
00051 } WAVDemuxContext;
00052
00053
00054 #if CONFIG_WAV_DEMUXER
00055
00056 static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
00057 {
00058 *tag = avio_rl32(pb);
00059 return avio_rl32(pb);
00060 }
00061
00062
00063 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
00064 {
00065 unsigned int tag;
00066 int64_t size;
00067
00068 for (;;) {
00069 if (url_feof(pb))
00070 return -1;
00071 size = next_tag(pb, &tag);
00072 if (tag == tag1)
00073 break;
00074 avio_skip(pb, size);
00075 }
00076 return size;
00077 }
00078
00079 static int wav_probe(AVProbeData *p)
00080 {
00081
00082 if (p->buf_size <= 32)
00083 return 0;
00084 if (!memcmp(p->buf + 8, "WAVE", 4)) {
00085 if (!memcmp(p->buf, "RIFF", 4))
00086
00087
00088
00089
00090
00091 return AVPROBE_SCORE_MAX - 1;
00092 else if (!memcmp(p->buf, "RF64", 4) &&
00093 !memcmp(p->buf + 12, "ds64", 4))
00094 return AVPROBE_SCORE_MAX;
00095 }
00096 return 0;
00097 }
00098
00099 static void handle_stream_probing(AVStream *st)
00100 {
00101 if (st->codec->codec_id == AV_CODEC_ID_PCM_S16LE) {
00102 st->request_probe = AVPROBE_SCORE_MAX/2;
00103 st->probe_packets = FFMIN(st->probe_packets, 4);
00104 }
00105 }
00106
00107 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
00108 {
00109 AVIOContext *pb = s->pb;
00110 int ret;
00111
00112
00113 *st = avformat_new_stream(s, NULL);
00114 if (!*st)
00115 return AVERROR(ENOMEM);
00116
00117 ret = ff_get_wav_header(pb, (*st)->codec, size);
00118 if (ret < 0)
00119 return ret;
00120 handle_stream_probing(*st);
00121
00122 (*st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
00123
00124 avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
00125
00126 return 0;
00127 }
00128
00129 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
00130 int length)
00131 {
00132 char temp[257];
00133 int ret;
00134
00135 av_assert0(length <= sizeof(temp));
00136 if ((ret = avio_read(s->pb, temp, length)) < 0)
00137 return ret;
00138
00139 temp[length] = 0;
00140
00141 if (strlen(temp))
00142 return av_dict_set(&s->metadata, key, temp, 0);
00143
00144 return 0;
00145 }
00146
00147 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
00148 {
00149 char temp[131], *coding_history;
00150 int ret, x;
00151 uint64_t time_reference;
00152 int64_t umid_parts[8], umid_mask = 0;
00153
00154 if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
00155 (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
00156 (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
00157 (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
00158 (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
00159 return ret;
00160
00161 time_reference = avio_rl64(s->pb);
00162 snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
00163 if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
00164 return ret;
00165
00166
00167 if (avio_rl16(s->pb) >= 1) {
00168 for (x = 0; x < 8; x++)
00169 umid_mask |= umid_parts[x] = avio_rb64(s->pb);
00170
00171 if (umid_mask) {
00172
00173 if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
00174
00175 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
00176 umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
00177 } else {
00178
00179 snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
00180 "%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
00181 umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
00182 umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
00183 }
00184
00185 if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
00186 return ret;
00187 }
00188
00189 avio_skip(s->pb, 190);
00190 } else
00191 avio_skip(s->pb, 254);
00192
00193 if (size > 602) {
00194
00195 size -= 602;
00196
00197 if (!(coding_history = av_malloc(size+1)))
00198 return AVERROR(ENOMEM);
00199
00200 if ((ret = avio_read(s->pb, coding_history, size)) < 0)
00201 return ret;
00202
00203 coding_history[size] = 0;
00204 if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
00205 AV_DICT_DONT_STRDUP_VAL)) < 0)
00206 return ret;
00207 }
00208
00209 return 0;
00210 }
00211
00212 static const AVMetadataConv wav_metadata_conv[] = {
00213 {"description", "comment" },
00214 {"originator", "encoded_by" },
00215 {"origination_date", "date" },
00216 {"origination_time", "creation_time"},
00217 {0},
00218 };
00219
00220
00221 static int wav_read_header(AVFormatContext *s)
00222 {
00223 int64_t size, av_uninit(data_size);
00224 int64_t sample_count=0;
00225 int rf64;
00226 uint32_t tag;
00227 AVIOContext *pb = s->pb;
00228 AVStream *st = NULL;
00229 WAVDemuxContext *wav = s->priv_data;
00230 int ret, got_fmt = 0;
00231 int64_t next_tag_ofs, data_ofs = -1;
00232
00233 wav->smv_data_ofs = -1;
00234
00235
00236 tag = avio_rl32(pb);
00237
00238 rf64 = tag == MKTAG('R', 'F', '6', '4');
00239 if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
00240 return -1;
00241 avio_rl32(pb);
00242 tag = avio_rl32(pb);
00243 if (tag != MKTAG('W', 'A', 'V', 'E'))
00244 return -1;
00245
00246 if (rf64) {
00247 if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
00248 return -1;
00249 size = avio_rl32(pb);
00250 if (size < 24)
00251 return -1;
00252 avio_rl64(pb);
00253 data_size = avio_rl64(pb);
00254 sample_count = avio_rl64(pb);
00255 if (data_size < 0 || sample_count < 0) {
00256 av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
00257 "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
00258 data_size, sample_count);
00259 return AVERROR_INVALIDDATA;
00260 }
00261 avio_skip(pb, size - 24);
00262
00263 }
00264
00265 for (;;) {
00266 AVStream *vst;
00267 size = next_tag(pb, &tag);
00268 next_tag_ofs = avio_tell(pb) + size;
00269
00270 if (url_feof(pb))
00271 break;
00272
00273 switch (tag) {
00274 case MKTAG('f', 'm', 't', ' '):
00275
00276 if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st)) < 0) {
00277 return ret;
00278 } else if (got_fmt)
00279 av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
00280
00281 got_fmt = 1;
00282 break;
00283 case MKTAG('d', 'a', 't', 'a'):
00284 if (!got_fmt) {
00285 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
00286 return AVERROR_INVALIDDATA;
00287 }
00288
00289 if (rf64) {
00290 next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
00291 } else {
00292 data_size = size;
00293 next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
00294 }
00295
00296 data_ofs = avio_tell(pb);
00297
00298
00299
00300
00301 if (!pb->seekable || (!rf64 && !size))
00302 goto break_loop;
00303 break;
00304 case MKTAG('f','a','c','t'):
00305 if (!sample_count)
00306 sample_count = avio_rl32(pb);
00307 break;
00308 case MKTAG('b','e','x','t'):
00309 if ((ret = wav_parse_bext_tag(s, size)) < 0)
00310 return ret;
00311 break;
00312 case MKTAG('S','M','V','0'):
00313 if (!got_fmt) {
00314 av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'SMV0' tag\n");
00315 return AVERROR_INVALIDDATA;
00316 }
00317
00318 if (size != MKTAG('0','2','0','0')) {
00319 av_log(s, AV_LOG_ERROR, "Unknown SMV version found\n");
00320 goto break_loop;
00321 }
00322 av_log(s, AV_LOG_DEBUG, "Found SMV data\n");
00323 vst = avformat_new_stream(s, NULL);
00324 if (!vst)
00325 return AVERROR(ENOMEM);
00326 avio_r8(pb);
00327 vst->id = 1;
00328 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00329 vst->codec->codec_id = AV_CODEC_ID_MJPEG;
00330 vst->codec->width = avio_rl24(pb);
00331 vst->codec->height = avio_rl24(pb);
00332 size = avio_rl24(pb);
00333 wav->smv_data_ofs = avio_tell(pb) + (size - 5) * 3;
00334 avio_rl24(pb);
00335 wav->smv_block_size = avio_rl24(pb);
00336 avpriv_set_pts_info(vst, 32, 1, avio_rl24(pb));
00337 vst->duration = avio_rl24(pb);
00338 avio_rl24(pb);
00339 avio_rl24(pb);
00340 wav->smv_frames_per_jpeg = avio_rl24(pb);
00341 goto break_loop;
00342 case MKTAG('L', 'I', 'S', 'T'):
00343 if (size < 4) {
00344 av_log(s, AV_LOG_ERROR, "too short LIST tag\n");
00345 return AVERROR_INVALIDDATA;
00346 }
00347 switch (avio_rl32(pb)) {
00348 case MKTAG('I', 'N', 'F', 'O'):
00349 ff_read_riff_info(s, size - 4);
00350 }
00351 break;
00352 }
00353
00354
00355 if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
00356 avio_seek(pb, next_tag_ofs, SEEK_SET) < 0) {
00357 break;
00358 }
00359 }
00360 break_loop:
00361 if (data_ofs < 0) {
00362 av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
00363 return AVERROR_INVALIDDATA;
00364 }
00365
00366 avio_seek(pb, data_ofs, SEEK_SET);
00367
00368 if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
00369 sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
00370 if (sample_count)
00371 st->duration = sample_count;
00372
00373 ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
00374 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
00375
00376 return 0;
00377 }
00378
00382 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
00383 {
00384 uint8_t guid[16];
00385 int64_t size;
00386
00387 while (!url_feof(pb)) {
00388 avio_read(pb, guid, 16);
00389 size = avio_rl64(pb);
00390 if (size <= 24)
00391 return -1;
00392 if (!memcmp(guid, guid1, 16))
00393 return size;
00394 avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
00395 }
00396 return -1;
00397 }
00398
00399 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
00400 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00401
00402 #define MAX_SIZE 4096
00403
00404 static int wav_read_packet(AVFormatContext *s,
00405 AVPacket *pkt)
00406 {
00407 int ret, size;
00408 int64_t left;
00409 AVStream *st;
00410 WAVDemuxContext *wav = s->priv_data;
00411
00412 if (wav->smv_data_ofs > 0) {
00413 int64_t audio_dts, video_dts;
00414 smv_retry:
00415 audio_dts = s->streams[0]->cur_dts;
00416 video_dts = s->streams[1]->cur_dts;
00417 if (audio_dts != AV_NOPTS_VALUE && video_dts != AV_NOPTS_VALUE) {
00418 audio_dts = av_rescale_q(audio_dts, s->streams[0]->time_base, AV_TIME_BASE_Q);
00419 video_dts = av_rescale_q(video_dts, s->streams[1]->time_base, AV_TIME_BASE_Q);
00420 wav->smv_last_stream = video_dts >= audio_dts;
00421 }
00422 wav->smv_last_stream = !wav->smv_last_stream;
00423 wav->smv_last_stream |= wav->audio_eof;
00424 wav->smv_last_stream &= !wav->smv_eof;
00425 if (wav->smv_last_stream) {
00426 uint64_t old_pos = avio_tell(s->pb);
00427 uint64_t new_pos = wav->smv_data_ofs +
00428 wav->smv_block * wav->smv_block_size;
00429 if (avio_seek(s->pb, new_pos, SEEK_SET) < 0) {
00430 ret = AVERROR_EOF;
00431 goto smv_out;
00432 }
00433 size = avio_rl24(s->pb);
00434 ret = av_get_packet(s->pb, pkt, size);
00435 if (ret < 0)
00436 goto smv_out;
00437 pkt->pos -= 3;
00438 pkt->pts = wav->smv_block * wav->smv_frames_per_jpeg;
00439 wav->smv_block++;
00440 pkt->stream_index = 1;
00441 smv_out:
00442 avio_seek(s->pb, old_pos, SEEK_SET);
00443 if (ret == AVERROR_EOF) {
00444 wav->smv_eof = 1;
00445 goto smv_retry;
00446 }
00447 return ret;
00448 }
00449 }
00450
00451 st = s->streams[0];
00452
00453 left = wav->data_end - avio_tell(s->pb);
00454 if (wav->ignore_length)
00455 left= INT_MAX;
00456 if (left <= 0){
00457 if (CONFIG_W64_DEMUXER && wav->w64)
00458 left = find_guid(s->pb, guid_data) - 24;
00459 else
00460 left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
00461 if (left < 0) {
00462 wav->audio_eof = 1;
00463 if (wav->smv_data_ofs > 0 && !wav->smv_eof)
00464 goto smv_retry;
00465 return AVERROR_EOF;
00466 }
00467 wav->data_end= avio_tell(s->pb) + left;
00468 }
00469
00470 size = MAX_SIZE;
00471 if (st->codec->block_align > 1) {
00472 if (size < st->codec->block_align)
00473 size = st->codec->block_align;
00474 size = (size / st->codec->block_align) * st->codec->block_align;
00475 }
00476 size = FFMIN(size, left);
00477 ret = av_get_packet(s->pb, pkt, size);
00478 if (ret < 0)
00479 return ret;
00480 pkt->stream_index = 0;
00481
00482 return ret;
00483 }
00484
00485 static int wav_read_seek(AVFormatContext *s,
00486 int stream_index, int64_t timestamp, int flags)
00487 {
00488 WAVDemuxContext *wav = s->priv_data;
00489 AVStream *st;
00490 wav->smv_eof = 0;
00491 wav->audio_eof = 0;
00492 if (wav->smv_data_ofs > 0) {
00493 int64_t smv_timestamp = timestamp;
00494 if (stream_index == 0)
00495 smv_timestamp = av_rescale_q(timestamp, s->streams[0]->time_base, s->streams[1]->time_base);
00496 else
00497 timestamp = av_rescale_q(smv_timestamp, s->streams[1]->time_base, s->streams[0]->time_base);
00498 wav->smv_block = smv_timestamp / wav->smv_frames_per_jpeg;
00499 }
00500
00501 st = s->streams[0];
00502 switch (st->codec->codec_id) {
00503 case AV_CODEC_ID_MP2:
00504 case AV_CODEC_ID_MP3:
00505 case AV_CODEC_ID_AC3:
00506 case AV_CODEC_ID_DTS:
00507
00508 return -1;
00509 default:
00510 break;
00511 }
00512 return ff_pcm_read_seek(s, stream_index, timestamp, flags);
00513 }
00514
00515 #define OFFSET(x) offsetof(WAVDemuxContext, x)
00516 #define DEC AV_OPT_FLAG_DECODING_PARAM
00517 static const AVOption demux_options[] = {
00518 { "ignore_length", "Ignore length", OFFSET(ignore_length), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, DEC },
00519 { NULL },
00520 };
00521
00522 static const AVClass wav_demuxer_class = {
00523 .class_name = "WAV demuxer",
00524 .item_name = av_default_item_name,
00525 .option = demux_options,
00526 .version = LIBAVUTIL_VERSION_INT,
00527 };
00528 AVInputFormat ff_wav_demuxer = {
00529 .name = "wav",
00530 .long_name = NULL_IF_CONFIG_SMALL("WAV / WAVE (Waveform Audio)"),
00531 .priv_data_size = sizeof(WAVDemuxContext),
00532 .read_probe = wav_probe,
00533 .read_header = wav_read_header,
00534 .read_packet = wav_read_packet,
00535 .read_seek = wav_read_seek,
00536 .flags = AVFMT_GENERIC_INDEX,
00537 .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
00538 .priv_class = &wav_demuxer_class,
00539 };
00540 #endif
00541
00542
00543 #if CONFIG_W64_DEMUXER
00544 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
00545 0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
00546
00547 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
00548 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00549
00550 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
00551 0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
00552
00553 static int w64_probe(AVProbeData *p)
00554 {
00555 if (p->buf_size <= 40)
00556 return 0;
00557 if (!memcmp(p->buf, guid_riff, 16) &&
00558 !memcmp(p->buf + 24, guid_wave, 16))
00559 return AVPROBE_SCORE_MAX;
00560 else
00561 return 0;
00562 }
00563
00564 static int w64_read_header(AVFormatContext *s)
00565 {
00566 int64_t size;
00567 AVIOContext *pb = s->pb;
00568 WAVDemuxContext *wav = s->priv_data;
00569 AVStream *st;
00570 uint8_t guid[16];
00571 int ret;
00572
00573 avio_read(pb, guid, 16);
00574 if (memcmp(guid, guid_riff, 16))
00575 return -1;
00576
00577 if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8)
00578 return -1;
00579
00580 avio_read(pb, guid, 16);
00581 if (memcmp(guid, guid_wave, 16)) {
00582 av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
00583 return -1;
00584 }
00585
00586 size = find_guid(pb, guid_fmt);
00587 if (size < 0) {
00588 av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
00589 return -1;
00590 }
00591
00592 st = avformat_new_stream(s, NULL);
00593 if (!st)
00594 return AVERROR(ENOMEM);
00595
00596
00597 ret = ff_get_wav_header(pb, st->codec, size - 24);
00598 if (ret < 0)
00599 return ret;
00600 avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
00601
00602 handle_stream_probing(st);
00603 st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
00604
00605 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00606
00607 size = find_guid(pb, guid_data);
00608 if (size < 0) {
00609 av_log(s, AV_LOG_ERROR, "could not find data guid\n");
00610 return -1;
00611 }
00612 wav->data_end = avio_tell(pb) + size - 24;
00613 wav->w64 = 1;
00614
00615 return 0;
00616 }
00617
00618 AVInputFormat ff_w64_demuxer = {
00619 .name = "w64",
00620 .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64"),
00621 .priv_data_size = sizeof(WAVDemuxContext),
00622 .read_probe = w64_probe,
00623 .read_header = w64_read_header,
00624 .read_packet = wav_read_packet,
00625 .read_seek = wav_read_seek,
00626 .flags = AVFMT_GENERIC_INDEX,
00627 .codec_tag = (const AVCodecTag* const []){ ff_codec_wav_tags, 0 },
00628 };
00629 #endif