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