wav.c
Go to the documentation of this file.
1 /*
2  * WAV muxer and demuxer
3  * Copyright (c) 2001, 2002 Fabrice Bellard
4  *
5  * Sony Wave64 demuxer
6  * RF64 demuxer
7  * Copyright (c) 2009 Daniel Verkamp
8  *
9  * This file is part of Libav.
10  *
11  * Libav is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * Libav is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with Libav; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/log.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
31 #include "avformat.h"
32 #include "internal.h"
33 #include "avio_internal.h"
34 #include "pcm.h"
35 #include "riff.h"
36 #include "avio.h"
37 #include "metadata.h"
38 
39 typedef struct {
40  const AVClass *class;
41  int64_t data;
42  int64_t data_end;
43  int64_t minpts;
44  int64_t maxpts;
46  int w64;
48 } WAVContext;
49 
50 #if CONFIG_WAV_MUXER
51 static inline void bwf_write_bext_string(AVFormatContext *s, const char *key, int maxlen)
52 {
54  int len = 0;
55 
56  if (tag = av_dict_get(s->metadata, key, NULL, 0)) {
57  len = strlen(tag->value);
58  len = FFMIN(len, maxlen);
59  avio_write(s->pb, tag->value, len);
60  }
61 
62  ffio_fill(s->pb, 0, maxlen - len);
63 }
64 
65 static void bwf_write_bext_chunk(AVFormatContext *s)
66 {
67  AVDictionaryEntry *tmp_tag;
68  uint64_t time_reference = 0;
69  int64_t bext = ff_start_tag(s->pb, "bext");
70 
71  bwf_write_bext_string(s, "description", 256);
72  bwf_write_bext_string(s, "originator", 32);
73  bwf_write_bext_string(s, "originator_reference", 32);
74  bwf_write_bext_string(s, "origination_date", 10);
75  bwf_write_bext_string(s, "origination_time", 8);
76 
77  if (tmp_tag = av_dict_get(s->metadata, "time_reference", NULL, 0))
78  time_reference = strtoll(tmp_tag->value, NULL, 10);
79  avio_wl64(s->pb, time_reference);
80  avio_wl16(s->pb, 1); // set version to 1
81 
82  if (tmp_tag = av_dict_get(s->metadata, "umid", NULL, 0)) {
83  unsigned char umidpart_str[17] = {0};
84  int i;
85  uint64_t umidpart;
86  int len = strlen(tmp_tag->value+2);
87 
88  for (i = 0; i < len/16; i++) {
89  memcpy(umidpart_str, tmp_tag->value + 2 + (i*16), 16);
90  umidpart = strtoll(umidpart_str, NULL, 16);
91  avio_wb64(s->pb, umidpart);
92  }
93  ffio_fill(s->pb, 0, 64 - i*8);
94  } else
95  ffio_fill(s->pb, 0, 64); // zero UMID
96 
97  ffio_fill(s->pb, 0, 190); // Reserved
98 
99  if (tmp_tag = av_dict_get(s->metadata, "coding_history", NULL, 0))
100  avio_put_str(s->pb, tmp_tag->value);
101 
102  ff_end_tag(s->pb, bext);
103 }
104 
105 static int wav_write_header(AVFormatContext *s)
106 {
107  WAVContext *wav = s->priv_data;
108  AVIOContext *pb = s->pb;
109  int64_t fmt, fact;
110 
111  ffio_wfourcc(pb, "RIFF");
112  avio_wl32(pb, 0); /* file length */
113  ffio_wfourcc(pb, "WAVE");
114 
115  /* format header */
116  fmt = ff_start_tag(pb, "fmt ");
117  if (ff_put_wav_header(pb, s->streams[0]->codec) < 0) {
118  av_log(s, AV_LOG_ERROR, "%s codec not supported in WAVE format\n",
119  s->streams[0]->codec->codec ? s->streams[0]->codec->codec->name : "NONE");
120  return -1;
121  }
122  ff_end_tag(pb, fmt);
123 
124  if (s->streams[0]->codec->codec_tag != 0x01 /* hence for all other than PCM */
125  && s->pb->seekable) {
126  fact = ff_start_tag(pb, "fact");
127  avio_wl32(pb, 0);
128  ff_end_tag(pb, fact);
129  }
130 
131  if (wav->write_bext)
132  bwf_write_bext_chunk(s);
133 
134  avpriv_set_pts_info(s->streams[0], 64, 1, s->streams[0]->codec->sample_rate);
135  wav->maxpts = wav->last_duration = 0;
136  wav->minpts = INT64_MAX;
137 
138  /* data header */
139  wav->data = ff_start_tag(pb, "data");
140 
141  avio_flush(pb);
142 
143  return 0;
144 }
145 
146 static int wav_write_packet(AVFormatContext *s, AVPacket *pkt)
147 {
148  AVIOContext *pb = s->pb;
149  WAVContext *wav = s->priv_data;
150  avio_write(pb, pkt->data, pkt->size);
151  if(pkt->pts != AV_NOPTS_VALUE) {
152  wav->minpts = FFMIN(wav->minpts, pkt->pts);
153  wav->maxpts = FFMAX(wav->maxpts, pkt->pts);
154  wav->last_duration = pkt->duration;
155  } else
156  av_log(s, AV_LOG_ERROR, "wav_write_packet: NOPTS\n");
157  return 0;
158 }
159 
160 static int wav_write_trailer(AVFormatContext *s)
161 {
162  AVIOContext *pb = s->pb;
163  WAVContext *wav = s->priv_data;
164  int64_t file_size;
165 
166  avio_flush(pb);
167 
168  if (s->pb->seekable) {
169  ff_end_tag(pb, wav->data);
170 
171  /* update file size */
172  file_size = avio_tell(pb);
173  avio_seek(pb, 4, SEEK_SET);
174  avio_wl32(pb, (uint32_t)(file_size - 8));
175  avio_seek(pb, file_size, SEEK_SET);
176 
177  avio_flush(pb);
178 
179  if(s->streams[0]->codec->codec_tag != 0x01) {
180  /* Update num_samps in fact chunk */
181  int number_of_samples;
182  number_of_samples = av_rescale(wav->maxpts - wav->minpts + wav->last_duration,
183  s->streams[0]->codec->sample_rate * (int64_t)s->streams[0]->time_base.num,
184  s->streams[0]->time_base.den);
185  avio_seek(pb, wav->data-12, SEEK_SET);
186  avio_wl32(pb, number_of_samples);
187  avio_seek(pb, file_size, SEEK_SET);
188  avio_flush(pb);
189  }
190  }
191  return 0;
192 }
193 
194 #define OFFSET(x) offsetof(WAVContext, x)
195 #define ENC AV_OPT_FLAG_ENCODING_PARAM
196 static const AVOption options[] = {
197  { "write_bext", "Write BEXT chunk.", OFFSET(write_bext), AV_OPT_TYPE_INT, { 0 }, 0, 1, ENC },
198  { NULL },
199 };
200 
201 static const AVClass wav_muxer_class = {
202  .class_name = "WAV muxer",
203  .item_name = av_default_item_name,
204  .option = options,
205  .version = LIBAVUTIL_VERSION_INT,
206 };
207 
208 AVOutputFormat ff_wav_muxer = {
209  .name = "wav",
210  .long_name = NULL_IF_CONFIG_SMALL("WAV format"),
211  .mime_type = "audio/x-wav",
212  .extensions = "wav",
213  .priv_data_size = sizeof(WAVContext),
214  .audio_codec = CODEC_ID_PCM_S16LE,
215  .video_codec = CODEC_ID_NONE,
216  .write_header = wav_write_header,
217  .write_packet = wav_write_packet,
218  .write_trailer = wav_write_trailer,
219  .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
220  .priv_class = &wav_muxer_class,
221 };
222 #endif /* CONFIG_WAV_MUXER */
223 
224 
225 #if CONFIG_WAV_DEMUXER
226 
227 static int64_t next_tag(AVIOContext *pb, uint32_t *tag)
228 {
229  *tag = avio_rl32(pb);
230  return avio_rl32(pb);
231 }
232 
233 /* RIFF chunks are always on a even offset. */
234 static int64_t wav_seek_tag(AVIOContext *s, int64_t offset, int whence)
235 {
236  return avio_seek(s, offset + (offset & 1), whence);
237 }
238 
239 /* return the size of the found tag */
240 static int64_t find_tag(AVIOContext *pb, uint32_t tag1)
241 {
242  unsigned int tag;
243  int64_t size;
244 
245  for (;;) {
246  if (pb->eof_reached)
247  return -1;
248  size = next_tag(pb, &tag);
249  if (tag == tag1)
250  break;
251  wav_seek_tag(pb, size, SEEK_CUR);
252  }
253  return size;
254 }
255 
256 static int wav_probe(AVProbeData *p)
257 {
258  /* check file header */
259  if (p->buf_size <= 32)
260  return 0;
261  if (!memcmp(p->buf + 8, "WAVE", 4)) {
262  if (!memcmp(p->buf, "RIFF", 4))
263  /*
264  Since ACT demuxer has standard WAV header at top of it's own,
265  returning score is decreased to avoid probe conflict
266  between ACT and WAV.
267  */
268  return AVPROBE_SCORE_MAX - 1;
269  else if (!memcmp(p->buf, "RF64", 4) &&
270  !memcmp(p->buf + 12, "ds64", 4))
271  return AVPROBE_SCORE_MAX;
272  }
273  return 0;
274 }
275 
276 static int wav_parse_fmt_tag(AVFormatContext *s, int64_t size, AVStream **st)
277 {
278  AVIOContext *pb = s->pb;
279  int ret;
280 
281  /* parse fmt header */
282  *st = avformat_new_stream(s, NULL);
283  if (!*st)
284  return AVERROR(ENOMEM);
285 
286  ret = ff_get_wav_header(pb, (*st)->codec, size);
287  if (ret < 0)
288  return ret;
289  (*st)->need_parsing = AVSTREAM_PARSE_FULL;
290 
291  avpriv_set_pts_info(*st, 64, 1, (*st)->codec->sample_rate);
292 
293  return 0;
294 }
295 
296 static inline int wav_parse_bext_string(AVFormatContext *s, const char *key,
297  int length)
298 {
299  char temp[257];
300  int ret;
301 
302  av_assert0(length <= sizeof(temp));
303  if ((ret = avio_read(s->pb, temp, length)) < 0)
304  return ret;
305 
306  temp[length] = 0;
307 
308  if (strlen(temp))
309  return av_dict_set(&s->metadata, key, temp, 0);
310 
311  return 0;
312 }
313 
314 static int wav_parse_bext_tag(AVFormatContext *s, int64_t size)
315 {
316  char temp[131], *coding_history;
317  int ret, x;
318  uint64_t time_reference;
319  int64_t umid_parts[8], umid_mask = 0;
320 
321  if ((ret = wav_parse_bext_string(s, "description", 256)) < 0 ||
322  (ret = wav_parse_bext_string(s, "originator", 32)) < 0 ||
323  (ret = wav_parse_bext_string(s, "originator_reference", 32)) < 0 ||
324  (ret = wav_parse_bext_string(s, "origination_date", 10)) < 0 ||
325  (ret = wav_parse_bext_string(s, "origination_time", 8)) < 0)
326  return ret;
327 
328  time_reference = avio_rl64(s->pb);
329  snprintf(temp, sizeof(temp), "%"PRIu64, time_reference);
330  if ((ret = av_dict_set(&s->metadata, "time_reference", temp, 0)) < 0)
331  return ret;
332 
333  /* check if version is >= 1, in which case an UMID may be present */
334  if (avio_rl16(s->pb) >= 1) {
335  for (x = 0; x < 8; x++)
336  umid_mask |= umid_parts[x] = avio_rb64(s->pb);
337 
338  if (umid_mask) {
339  /* the string formatting below is per SMPTE 330M-2004 Annex C */
340  if (umid_parts[4] == 0 && umid_parts[5] == 0 && umid_parts[6] == 0 && umid_parts[7] == 0) {
341  /* basic UMID */
342  snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
343  umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3]);
344  } else {
345  /* extended UMID */
346  snprintf(temp, sizeof(temp), "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64
347  "0x%016"PRIX64"%016"PRIX64"%016"PRIX64"%016"PRIX64,
348  umid_parts[0], umid_parts[1], umid_parts[2], umid_parts[3],
349  umid_parts[4], umid_parts[5], umid_parts[6], umid_parts[7]);
350  }
351 
352  if ((ret = av_dict_set(&s->metadata, "umid", temp, 0)) < 0)
353  return ret;
354  }
355 
356  avio_skip(s->pb, 190);
357  } else
358  avio_skip(s->pb, 254);
359 
360  if (size > 602) {
361  /* CodingHistory present */
362  size -= 602;
363 
364  if (!(coding_history = av_malloc(size+1)))
365  return AVERROR(ENOMEM);
366 
367  if ((ret = avio_read(s->pb, coding_history, size)) < 0)
368  return ret;
369 
370  coding_history[size] = 0;
371  if ((ret = av_dict_set(&s->metadata, "coding_history", coding_history,
373  return ret;
374  }
375 
376  return 0;
377 }
378 
379 static const AVMetadataConv wav_metadata_conv[] = {
380  {"description", "comment" },
381  {"originator", "encoded_by" },
382  {"origination_date", "date" },
383  {"origination_time", "creation_time"},
384  {0},
385 };
386 
387 /* wav input */
388 static int wav_read_header(AVFormatContext *s,
389  AVFormatParameters *ap)
390 {
391  int64_t size, av_uninit(data_size);
392  int64_t sample_count=0;
393  int rf64;
394  uint32_t tag, list_type;
395  AVIOContext *pb = s->pb;
396  AVStream *st;
397  WAVContext *wav = s->priv_data;
398  int ret, got_fmt = 0;
399  int64_t next_tag_ofs, data_ofs = -1;
400 
401  /* check RIFF header */
402  tag = avio_rl32(pb);
403 
404  rf64 = tag == MKTAG('R', 'F', '6', '4');
405  if (!rf64 && tag != MKTAG('R', 'I', 'F', 'F'))
406  return -1;
407  avio_rl32(pb); /* file size */
408  tag = avio_rl32(pb);
409  if (tag != MKTAG('W', 'A', 'V', 'E'))
410  return -1;
411 
412  if (rf64) {
413  if (avio_rl32(pb) != MKTAG('d', 's', '6', '4'))
414  return -1;
415  size = avio_rl32(pb);
416  if (size < 16)
417  return -1;
418  avio_rl64(pb); /* RIFF size */
419  data_size = avio_rl64(pb);
420  sample_count = avio_rl64(pb);
421  if (data_size < 0 || sample_count < 0) {
422  av_log(s, AV_LOG_ERROR, "negative data_size and/or sample_count in "
423  "ds64: data_size = %"PRId64", sample_count = %"PRId64"\n",
424  data_size, sample_count);
425  return AVERROR_INVALIDDATA;
426  }
427  avio_skip(pb, size - 16); /* skip rest of ds64 chunk */
428  }
429 
430  for (;;) {
431  size = next_tag(pb, &tag);
432  next_tag_ofs = avio_tell(pb) + size;
433 
434  if (pb->eof_reached)
435  break;
436 
437  switch (tag) {
438  case MKTAG('f', 'm', 't', ' '):
439  /* only parse the first 'fmt ' tag found */
440  if (!got_fmt && (ret = wav_parse_fmt_tag(s, size, &st) < 0)) {
441  return ret;
442  } else if (got_fmt)
443  av_log(s, AV_LOG_WARNING, "found more than one 'fmt ' tag\n");
444 
445  got_fmt = 1;
446  break;
447  case MKTAG('d', 'a', 't', 'a'):
448  if (!got_fmt) {
449  av_log(s, AV_LOG_ERROR, "found no 'fmt ' tag before the 'data' tag\n");
450  return AVERROR_INVALIDDATA;
451  }
452 
453  if (rf64) {
454  next_tag_ofs = wav->data_end = avio_tell(pb) + data_size;
455  } else {
456  data_size = size;
457  next_tag_ofs = wav->data_end = size ? next_tag_ofs : INT64_MAX;
458  }
459 
460  data_ofs = avio_tell(pb);
461 
462  /* don't look for footer metadata if we can't seek or if we don't
463  * know where the data tag ends
464  */
465  if (!pb->seekable || (!rf64 && !size))
466  goto break_loop;
467  break;
468  case MKTAG('f','a','c','t'):
469  if (!sample_count)
470  sample_count = avio_rl32(pb);
471  break;
472  case MKTAG('b','e','x','t'):
473  if ((ret = wav_parse_bext_tag(s, size)) < 0)
474  return ret;
475  break;
476  case MKTAG('L', 'I', 'S', 'T'):
477  list_type = avio_rl32(pb);
478  if (size < 4) {
479  av_log(s, AV_LOG_ERROR, "too short LIST");
480  return AVERROR_INVALIDDATA;
481  }
482  switch (list_type) {
483  case MKTAG('I', 'N', 'F', 'O'):
484  if ((ret = ff_read_riff_info(s, size - 4)) < 0)
485  return ret;
486  }
487  break;
488  }
489 
490  /* seek to next tag unless we know that we'll run into EOF */
491  if ((avio_size(pb) > 0 && next_tag_ofs >= avio_size(pb)) ||
492  wav_seek_tag(pb, next_tag_ofs, SEEK_SET) < 0) {
493  break;
494  }
495  }
496 break_loop:
497  if (data_ofs < 0) {
498  av_log(s, AV_LOG_ERROR, "no 'data' tag found\n");
499  return AVERROR_INVALIDDATA;
500  }
501 
502  avio_seek(pb, data_ofs, SEEK_SET);
503 
504  if (!sample_count && st->codec->channels && av_get_bits_per_sample(st->codec->codec_id))
505  sample_count = (data_size<<3) / (st->codec->channels * (uint64_t)av_get_bits_per_sample(st->codec->codec_id));
506  if (sample_count)
507  st->duration = sample_count;
508 
509  ff_metadata_conv_ctx(s, NULL, wav_metadata_conv);
511 
512  return 0;
513 }
514 
518 static int64_t find_guid(AVIOContext *pb, const uint8_t guid1[16])
519 {
520  uint8_t guid[16];
521  int64_t size;
522 
523  while (!pb->eof_reached) {
524  avio_read(pb, guid, 16);
525  size = avio_rl64(pb);
526  if (size <= 24)
527  return -1;
528  if (!memcmp(guid, guid1, 16))
529  return size;
530  avio_skip(pb, FFALIGN(size, INT64_C(8)) - 24);
531  }
532  return -1;
533 }
534 
535 static const uint8_t guid_data[16] = { 'd', 'a', 't', 'a',
536  0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
537 
538 #define MAX_SIZE 4096
539 
540 static int wav_read_packet(AVFormatContext *s,
541  AVPacket *pkt)
542 {
543  int ret, size;
544  int64_t left;
545  AVStream *st;
546  WAVContext *wav = s->priv_data;
547 
548  st = s->streams[0];
549 
550  left = wav->data_end - avio_tell(s->pb);
551  if (left <= 0){
552  if (CONFIG_W64_DEMUXER && wav->w64)
553  left = find_guid(s->pb, guid_data) - 24;
554  else
555  left = find_tag(s->pb, MKTAG('d', 'a', 't', 'a'));
556  if (left < 0)
557  return AVERROR_EOF;
558  wav->data_end= avio_tell(s->pb) + left;
559  }
560 
561  size = MAX_SIZE;
562  if (st->codec->block_align > 1) {
563  if (size < st->codec->block_align)
564  size = st->codec->block_align;
565  size = (size / st->codec->block_align) * st->codec->block_align;
566  }
567  size = FFMIN(size, left);
568  ret = av_get_packet(s->pb, pkt, size);
569  if (ret < 0)
570  return ret;
571  pkt->stream_index = 0;
572 
573  return ret;
574 }
575 
576 static int wav_read_seek(AVFormatContext *s,
577  int stream_index, int64_t timestamp, int flags)
578 {
579  AVStream *st;
580 
581  st = s->streams[0];
582  switch (st->codec->codec_id) {
583  case CODEC_ID_MP2:
584  case CODEC_ID_MP3:
585  case CODEC_ID_AC3:
586  case CODEC_ID_DTS:
587  /* use generic seeking with dynamically generated indexes */
588  return -1;
589  default:
590  break;
591  }
592  return pcm_read_seek(s, stream_index, timestamp, flags);
593 }
594 
595 AVInputFormat ff_wav_demuxer = {
596  .name = "wav",
597  .long_name = NULL_IF_CONFIG_SMALL("WAV format"),
598  .priv_data_size = sizeof(WAVContext),
599  .read_probe = wav_probe,
600  .read_header = wav_read_header,
601  .read_packet = wav_read_packet,
602  .read_seek = wav_read_seek,
603  .flags= AVFMT_GENERIC_INDEX,
604  .codec_tag= (const AVCodecTag* const []){ff_codec_wav_tags, 0},
605 };
606 #endif /* CONFIG_WAV_DEMUXER */
607 
608 
609 #if CONFIG_W64_DEMUXER
610 static const uint8_t guid_riff[16] = { 'r', 'i', 'f', 'f',
611  0x2E, 0x91, 0xCF, 0x11, 0xA5, 0xD6, 0x28, 0xDB, 0x04, 0xC1, 0x00, 0x00 };
612 
613 static const uint8_t guid_wave[16] = { 'w', 'a', 'v', 'e',
614  0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
615 
616 static const uint8_t guid_fmt [16] = { 'f', 'm', 't', ' ',
617  0xF3, 0xAC, 0xD3, 0x11, 0x8C, 0xD1, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A };
618 
619 static int w64_probe(AVProbeData *p)
620 {
621  if (p->buf_size <= 40)
622  return 0;
623  if (!memcmp(p->buf, guid_riff, 16) &&
624  !memcmp(p->buf + 24, guid_wave, 16))
625  return AVPROBE_SCORE_MAX;
626  else
627  return 0;
628 }
629 
630 static int w64_read_header(AVFormatContext *s, AVFormatParameters *ap)
631 {
632  int64_t size;
633  AVIOContext *pb = s->pb;
634  WAVContext *wav = s->priv_data;
635  AVStream *st;
636  uint8_t guid[16];
637  int ret;
638 
639  avio_read(pb, guid, 16);
640  if (memcmp(guid, guid_riff, 16))
641  return -1;
642 
643  if (avio_rl64(pb) < 16 + 8 + 16 + 8 + 16 + 8) /* riff + wave + fmt + sizes */
644  return -1;
645 
646  avio_read(pb, guid, 16);
647  if (memcmp(guid, guid_wave, 16)) {
648  av_log(s, AV_LOG_ERROR, "could not find wave guid\n");
649  return -1;
650  }
651 
652  size = find_guid(pb, guid_fmt);
653  if (size < 0) {
654  av_log(s, AV_LOG_ERROR, "could not find fmt guid\n");
655  return -1;
656  }
657 
658  st = avformat_new_stream(s, NULL);
659  if (!st)
660  return AVERROR(ENOMEM);
661 
662  /* subtract chunk header size - normal wav file doesn't count it */
663  ret = ff_get_wav_header(pb, st->codec, size - 24);
664  if (ret < 0)
665  return ret;
666  avio_skip(pb, FFALIGN(size, INT64_C(8)) - size);
667 
669 
670  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
671 
672  size = find_guid(pb, guid_data);
673  if (size < 0) {
674  av_log(s, AV_LOG_ERROR, "could not find data guid\n");
675  return -1;
676  }
677  wav->data_end = avio_tell(pb) + size - 24;
678  wav->w64 = 1;
679 
680  return 0;
681 }
682 
683 AVInputFormat ff_w64_demuxer = {
684  .name = "w64",
685  .long_name = NULL_IF_CONFIG_SMALL("Sony Wave64 format"),
686  .priv_data_size = sizeof(WAVContext),
687  .read_probe = w64_probe,
688  .read_header = w64_read_header,
689  .read_packet = wav_read_packet,
690  .read_seek = wav_read_seek,
691  .flags = AVFMT_GENERIC_INDEX,
692  .codec_tag = (const AVCodecTag* const []){ff_codec_wav_tags, 0},
693 };
694 #endif /* CONFIG_W64_DEMUXER */
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:521
static int write_header(AVFormatContext *s)
Definition: assenc.c:28
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:527
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:281
Buffered I/O operations.
int size
void ff_end_tag(AVIOContext *pb, int64_t start)
int64_t ff_start_tag(AVIOContext *pb, const char *tag)
AVOption.
Definition: opt.h:244
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:117
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: assenc.c:58
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:148
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:909
AVOptions.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:211
#define ENC
Definition: gif.c:352
struct AVCodec * codec
Definition: avcodec.h:1529
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:27
#define MAX_SIZE
Definition: vf_unsharp.c:45
#define OFFSET(x)
Definition: avconv.c:4395
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1751
#define FFALIGN(x, a)
Definition: common.h:60
Format I/O context.
Definition: avformat.h:863
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
internal metadata API header see avformat.h or the public API!
Public dictionary API.
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:316
enum AVStreamParseType need_parsing
Definition: avformat.h:815
AVStream ** streams
Definition: avformat.h:908
static int read_header(FFV1Context *f)
Definition: ffv1.c:1513
uint8_t * data
Definition: avcodec.h:908
static int flags
Definition: log.c:34
uint32_t tag
Definition: movenc.c:670
#define AVERROR_EOF
End of file.
Definition: error.h:51
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:269
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:842
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:492
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:190
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:50
int av_get_bits_per_sample(enum CodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1634
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:930
int64_t maxpts
Definition: wav.c:44
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:67
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:515
AVDictionary * metadata
Definition: avformat.h:1085
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:738
#define AVERROR(e)
Definition: error.h:43
int ff_get_wav_header(AVIOContext *pb, AVCodecContext *codec, int size)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:191
int write_bext
Definition: wav.c:47
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
const char * name
Name of the codec implementation.
Definition: avcodec.h:3196
#define CONFIG_W64_DEMUXER
Definition: config.h:703
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:289
#define FFMAX(a, b)
Definition: common.h:53
AVCodecContext * codec
codec context
Definition: avformat.h:623
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:342
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:122
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:205
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:128
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:176
#define FFMIN(a, b)
Definition: common.h:55
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() and chilren.
Definition: dict.h:66
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:483
const char * name
Definition: avformat.h:390
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static int read_probe(AVProbeData *p)
Definition: img2.c:185
LIBAVUTIL_VERSION_INT
Definition: eval.c:50
int avio_put_str(AVIOContext *s, const char *str)
Write a NULL-terminated string.
Definition: aviobuf.c:469
Stream structure.
Definition: avformat.h:620
int pcm_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: pcm.c:26
NULL
Definition: eval.c:50
int sample_rate
samples per second
Definition: avcodec.h:1456
int64_t minpts
Definition: wav.c:43
AVIOContext * pb
Definition: avformat.h:896
av_default_item_name
Definition: dnxhdenc.c:43
static const OptionDef options[]
Definition: avconv.c:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1590
#define INT64_MAX
Definition: internal.h:80
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:109
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:50
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:64
Describe the class of an AVClass context structure.
Definition: log.h:33
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:376
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
const AVMetadataConv ff_riff_info_conv[]
Definition: riff.c:346
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:684
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:88
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:345
full parsing and repack
Definition: avformat.h:581
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:722
Main libavformat public API header.
int ff_put_wav_header(AVIOContext *pb, AVCodecContext *enc)
int den
denominator
Definition: rational.h:45
char * value
Definition: dict.h:76
int eof_reached
true if eof reached
Definition: avio.h:98
int len
int channels
number of audio channels
Definition: avcodec.h:1457
void * priv_data
Format private data.
Definition: avformat.h:883
int w64
Definition: wav.c:46
#define av_uninit(x)
Definition: attributes.h:124
int last_duration
Definition: wav.c:45
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
int stream_index
Definition: avcodec.h:910
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:652
#define MKTAG(a, b, c, d)
Definition: common.h:231
int ff_read_riff_info(AVFormatContext *s, int64_t size)
Definition: riff.c:645
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:746
int64_t data
Definition: wav.c:41
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:271
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:336
int64_t data_end
Definition: wav.c:42
Definition: wav.c:39
enum CodecID codec_id
Definition: avcodec.h:1575