FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mp3enc.c
Go to the documentation of this file.
1 /*
2  * MP3 muxer
3  * Copyright (c) 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "avformat.h"
23 #include "avio_internal.h"
24 #include "id3v1.h"
25 #include "id3v2.h"
26 #include "rawenc.h"
27 #include "libavutil/avstring.h"
28 #include "libavcodec/mpegaudio.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/dict.h"
34 #include "libavutil/avassert.h"
35 #include "libavutil/crc.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/replaygain.h"
38 
39 static int id3v1_set_string(AVFormatContext *s, const char *key,
40  uint8_t *buf, int buf_size)
41 {
43  if ((tag = av_dict_get(s->metadata, key, NULL, 0)))
44  av_strlcpy(buf, tag->value, buf_size);
45  return !!tag;
46 }
47 
49 {
51  int i, count = 0;
52 
53  memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
54  buf[0] = 'T';
55  buf[1] = 'A';
56  buf[2] = 'G';
57  /* we knowingly overspecify each tag length by one byte to compensate for the mandatory null byte added by av_strlcpy */
58  count += id3v1_set_string(s, "TIT2", buf + 3, 30 + 1); //title
59  count += id3v1_set_string(s, "TPE1", buf + 33, 30 + 1); //author|artist
60  count += id3v1_set_string(s, "TALB", buf + 63, 30 + 1); //album
61  count += id3v1_set_string(s, "TDRC", buf + 93, 4 + 1); //date
62  count += id3v1_set_string(s, "comment", buf + 97, 30 + 1);
63  if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) { //track
64  buf[125] = 0;
65  buf[126] = atoi(tag->value);
66  count++;
67  }
68  buf[127] = 0xFF; /* default to unknown genre */
69  if ((tag = av_dict_get(s->metadata, "TCON", NULL, 0))) { //genre
70  for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
71  if (!av_strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
72  buf[127] = i;
73  count++;
74  break;
75  }
76  }
77  }
78  return count;
79 }
80 
81 #define XING_NUM_BAGS 400
82 #define XING_TOC_SIZE 100
83 // size of the XING/LAME data, starting from the Xing tag
84 #define XING_SIZE 156
85 
86 typedef struct MP3Context {
87  const AVClass *class;
92 
93  /* xing header */
94  // a buffer containing the whole XING/LAME frame
97 
98  AVCRC audio_crc; // CRC of the audio data
99  uint32_t audio_size; // total size of the audio data
100 
101  // offset of the XING/LAME frame in the file
103  // offset of the XING/INFO tag in the frame
105 
108  uint32_t want;
109  uint32_t seen;
110  uint32_t pos;
111  uint64_t bag[XING_NUM_BAGS];
114 
115  /* index of the audio stream */
117  /* number of attached pictures we still need to write */
119 
120  /* audio packets are queued here until we get all the attached pictures */
122 } MP3Context;
123 
124 static const uint8_t xing_offtbl[2][2] = {{32, 17}, {17, 9}};
125 
126 /*
127  * Write an empty XING header and initialize respective data.
128  */
130 {
131  MP3Context *mp3 = s->priv_data;
133  AVDictionaryEntry *enc = av_dict_get(s->streams[mp3->audio_stream_idx]->metadata, "encoder", NULL, 0);
134  AVIOContext *dyn_ctx;
135  int32_t header;
136  MPADecodeHeader mpah;
137  int srate_idx, i, channels;
138  int bitrate_idx;
139  int best_bitrate_idx = -1;
140  int best_bitrate_error = INT_MAX;
141  int ret;
142  int ver = 0;
143  int bytes_needed;
144 
145  if (!s->pb->seekable || !mp3->write_xing)
146  return 0;
147 
148  for (i = 0; i < FF_ARRAY_ELEMS(avpriv_mpa_freq_tab); i++) {
149  const uint16_t base_freq = avpriv_mpa_freq_tab[i];
150 
151  if (par->sample_rate == base_freq) ver = 0x3; // MPEG 1
152  else if (par->sample_rate == base_freq / 2) ver = 0x2; // MPEG 2
153  else if (par->sample_rate == base_freq / 4) ver = 0x0; // MPEG 2.5
154  else continue;
155 
156  srate_idx = i;
157  break;
158  }
160  av_log(s, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n");
161  return -1;
162  }
163 
164  switch (par->channels) {
165  case 1: channels = MPA_MONO; break;
166  case 2: channels = MPA_STEREO; break;
167  default: av_log(s, AV_LOG_WARNING, "Unsupported number of channels, "
168  "not writing Xing header.\n");
169  return -1;
170  }
171 
172  /* dummy MPEG audio header */
173  header = 0xffU << 24; // sync
174  header |= (0x7 << 5 | ver << 3 | 0x1 << 1 | 0x1) << 16; // sync/audio-version/layer 3/no crc*/
175  header |= (srate_idx << 2) << 8;
176  header |= channels << 6;
177 
178  for (bitrate_idx = 1; bitrate_idx < 15; bitrate_idx++) {
179  int bit_rate = 1000 * avpriv_mpa_bitrate_tab[ver != 3][3 - 1][bitrate_idx];
180  int error = FFABS(bit_rate - par->bit_rate);
181 
182  if (error < best_bitrate_error) {
183  best_bitrate_error = error;
184  best_bitrate_idx = bitrate_idx;
185  }
186  }
187  av_assert0(best_bitrate_idx >= 0);
188 
189  for (bitrate_idx = best_bitrate_idx; ; bitrate_idx++) {
190  int32_t mask = bitrate_idx << (4 + 8);
191  if (15 == bitrate_idx)
192  return -1;
193  header |= mask;
194 
195  ret = avpriv_mpegaudio_decode_header(&mpah, header);
196  av_assert0(ret >= 0);
197  mp3->xing_offset = xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1] + 4;
198  bytes_needed = mp3->xing_offset + XING_SIZE;
199 
200  if (bytes_needed <= mpah.frame_size)
201  break;
202 
203  header &= ~mask;
204  }
205 
206  ret = avio_open_dyn_buf(&dyn_ctx);
207  if (ret < 0)
208  return ret;
209 
210  avio_wb32(dyn_ctx, header);
211 
212  ffio_fill(dyn_ctx, 0, mp3->xing_offset - 4);
213  ffio_wfourcc(dyn_ctx, "Xing");
214  avio_wb32(dyn_ctx, 0x01 | 0x02 | 0x04 | 0x08); // frames / size / TOC / vbr scale
215 
216  mp3->size = mpah.frame_size;
217  mp3->want=1;
218  mp3->seen=0;
219  mp3->pos=0;
220 
221  avio_wb32(dyn_ctx, 0); // frames
222  avio_wb32(dyn_ctx, 0); // size
223 
224  // TOC
225  for (i = 0; i < XING_TOC_SIZE; i++)
226  avio_w8(dyn_ctx, (uint8_t)(255 * i / XING_TOC_SIZE));
227 
228  // vbr quality
229  // we write it, because some (broken) tools always expect it to be present
230  avio_wb32(dyn_ctx, 0);
231 
232  // encoder short version string
233  if (enc) {
234  uint8_t encoder_str[9] = { 0 };
235  if ( strlen(enc->value) > sizeof(encoder_str)
236  && !strcmp("Lavc libmp3lame", enc->value)) {
237  memcpy(encoder_str, "Lavf lame", 9);
238  } else
239  memcpy(encoder_str, enc->value, FFMIN(strlen(enc->value), sizeof(encoder_str)));
240 
241  avio_write(dyn_ctx, encoder_str, sizeof(encoder_str));
242  } else
243  avio_write(dyn_ctx, "Lavf\0\0\0\0\0", 9);
244 
245  avio_w8(dyn_ctx, 0); // tag revision 0 / unknown vbr method
246  avio_w8(dyn_ctx, 0); // unknown lowpass filter value
247  ffio_fill(dyn_ctx, 0, 8); // empty replaygain fields
248  avio_w8(dyn_ctx, 0); // unknown encoding flags
249  avio_w8(dyn_ctx, 0); // unknown abr/minimal bitrate
250 
251  // encoder delay
252  if (par->initial_padding - 528 - 1 >= 1 << 12) {
253  av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n");
254  }
255  avio_wb24(dyn_ctx, FFMAX(par->initial_padding - 528 - 1, 0)<<12);
256 
257  avio_w8(dyn_ctx, 0); // misc
258  avio_w8(dyn_ctx, 0); // mp3gain
259  avio_wb16(dyn_ctx, 0); // preset
260 
261  // audio length and CRCs (will be updated later)
262  avio_wb32(dyn_ctx, 0); // music length
263  avio_wb16(dyn_ctx, 0); // music crc
264  avio_wb16(dyn_ctx, 0); // tag crc
265 
266  ffio_fill(dyn_ctx, 0, mpah.frame_size - bytes_needed);
267 
268  mp3->xing_frame_size = avio_close_dyn_buf(dyn_ctx, &mp3->xing_frame);
269  mp3->xing_frame_offset = avio_tell(s->pb);
270  avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
271 
272  mp3->audio_size = mp3->xing_frame_size;
273 
274  return 0;
275 }
276 
277 /*
278  * Add a frame to XING data.
279  * Following lame's "VbrTag.c".
280  */
282 {
283  int i;
284 
285  mp3->frames++;
286  mp3->seen++;
287  mp3->size += pkt->size;
288 
289  if (mp3->want == mp3->seen) {
290  mp3->bag[mp3->pos] = mp3->size;
291 
292  if (XING_NUM_BAGS == ++mp3->pos) {
293  /* shrink table to half size by throwing away each second bag. */
294  for (i = 1; i < XING_NUM_BAGS; i += 2)
295  mp3->bag[i >> 1] = mp3->bag[i];
296 
297  /* double wanted amount per bag. */
298  mp3->want *= 2;
299  /* adjust current position to half of table size. */
300  mp3->pos = XING_NUM_BAGS / 2;
301  }
302 
303  mp3->seen = 0;
304  }
305 }
306 
308 {
309  MP3Context *mp3 = s->priv_data;
310 
311  if (pkt->data && pkt->size >= 4) {
312  MPADecodeHeader mpah;
313  int ret;
314  int av_unused base;
315  uint32_t h;
316 
317  h = AV_RB32(pkt->data);
318  ret = avpriv_mpegaudio_decode_header(&mpah, h);
319  if (ret >= 0) {
320  if (!mp3->initial_bitrate)
321  mp3->initial_bitrate = mpah.bit_rate;
322  if ((mpah.bit_rate == 0) || (mp3->initial_bitrate != mpah.bit_rate))
323  mp3->has_variable_bitrate = 1;
324  } else {
325  av_log(s, AV_LOG_WARNING, "Audio packet of size %d (starting with %08X...) "
326  "is invalid, writing it anyway.\n", pkt->size, h);
327  }
328 
329 #ifdef FILTER_VBR_HEADERS
330  /* filter out XING and INFO headers. */
331  base = 4 + xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1];
332 
333  if (base + 4 <= pkt->size) {
334  uint32_t v = AV_RB32(pkt->data + base);
335 
336  if (MKBETAG('X','i','n','g') == v || MKBETAG('I','n','f','o') == v)
337  return 0;
338  }
339 
340  /* filter out VBRI headers. */
341  base = 4 + 32;
342 
343  if (base + 4 <= pkt->size && MKBETAG('V','B','R','I') == AV_RB32(pkt->data + base))
344  return 0;
345 #endif
346 
347  if (mp3->xing_offset) {
348  mp3_xing_add_frame(mp3, pkt);
349  mp3->audio_size += pkt->size;
351  mp3->audio_crc, pkt->data, pkt->size);
352  }
353  }
354 
355  return ff_raw_write_packet(s, pkt);
356 }
357 
359 {
360  MP3Context *mp3 = s->priv_data;
361  AVPacketList *pktl;
362  int ret = 0, write = 1;
363 
365  mp3_write_xing(s);
366 
367  while ((pktl = mp3->queue)) {
368  if (write && (ret = mp3_write_audio_packet(s, &pktl->pkt)) < 0)
369  write = 0;
370  av_packet_unref(&pktl->pkt);
371  mp3->queue = pktl->next;
372  av_freep(&pktl);
373  }
374  mp3->queue_end = NULL;
375  return ret;
376 }
377 
379 {
380  MP3Context *mp3 = s->priv_data;
381  AVReplayGain *rg;
382  uint16_t tag_crc;
383  uint8_t *toc;
384  int i, rg_size;
385 
386  /* replace "Xing" identification string with "Info" for CBR files. */
387  if (!mp3->has_variable_bitrate)
388  AV_WL32(mp3->xing_frame + mp3->xing_offset, MKTAG('I', 'n', 'f', 'o'));
389 
390  AV_WB32(mp3->xing_frame + mp3->xing_offset + 8, mp3->frames);
391  AV_WB32(mp3->xing_frame + mp3->xing_offset + 12, mp3->size);
392 
393  toc = mp3->xing_frame + mp3->xing_offset + 16;
394  toc[0] = 0; // first toc entry has to be zero.
395  for (i = 1; i < XING_TOC_SIZE; ++i) {
396  int j = i * mp3->pos / XING_TOC_SIZE;
397  int seek_point = 256LL * mp3->bag[j] / mp3->size;
398  toc[i] = FFMIN(seek_point, 255);
399  }
400 
401  /* write replaygain */
403  &rg_size);
404  if (rg && rg_size >= sizeof(*rg)) {
405  uint16_t val;
406 
407  AV_WB32(mp3->xing_frame + mp3->xing_offset + 131,
408  av_rescale(rg->track_peak, 1 << 23, 100000));
409 
410  if (rg->track_gain != INT32_MIN) {
411  val = FFABS(rg->track_gain / 10000) & ((1 << 9) - 1);
412  val |= (rg->track_gain < 0) << 9;
413  val |= 1 << 13;
414  AV_WB16(mp3->xing_frame + mp3->xing_offset + 135, val);
415  }
416 
417  if (rg->album_gain != INT32_MIN) {
418  val = FFABS(rg->album_gain / 10000) & ((1 << 9) - 1);
419  val |= (rg->album_gain < 0) << 9;
420  val |= 1 << 14;
421  AV_WB16(mp3->xing_frame + mp3->xing_offset + 137, val);
422  }
423  }
424 
425  AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size);
426  AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc);
427 
428  tag_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), 0, mp3->xing_frame, 190);
429  AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 2, tag_crc);
430 
431  avio_seek(s->pb, mp3->xing_frame_offset, SEEK_SET);
432  avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
433  avio_seek(s->pb, 0, SEEK_END);
434 }
435 
437 {
439  MP3Context *mp3 = s->priv_data;
440 
441  if (mp3->pics_to_write) {
442  av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
443  "attached pictures.\n");
444  mp3_queue_flush(s);
445  }
446 
447  /* write the id3v1 tag */
448  if (mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
449  avio_write(s->pb, buf, ID3v1_TAG_SIZE);
450  }
451 
452  if (mp3->xing_offset)
453  mp3_update_xing(s);
454 
455  av_freep(&mp3->xing_frame);
456 
457  return 0;
458 }
459 
460 static int query_codec(enum AVCodecID id, int std_compliance)
461 {
463  while(cm->id != AV_CODEC_ID_NONE) {
464  if(id == cm->id)
465  return MKTAG('A', 'P', 'I', 'C');
466  cm++;
467  }
468  return -1;
469 }
470 
471 #if CONFIG_MP2_MUXER
472 AVOutputFormat ff_mp2_muxer = {
473  .name = "mp2",
474  .long_name = NULL_IF_CONFIG_SMALL("MP2 (MPEG audio layer 2)"),
475  .mime_type = "audio/mpeg",
476  .extensions = "mp2,m2a,mpa",
477  .audio_codec = AV_CODEC_ID_MP2,
478  .video_codec = AV_CODEC_ID_NONE,
479  .write_packet = ff_raw_write_packet,
480  .flags = AVFMT_NOTIMESTAMPS,
481 };
482 #endif
483 
484 #if CONFIG_MP3_MUXER
485 
486 static const AVOption options[] = {
487  { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
488  offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 0, 4, AV_OPT_FLAG_ENCODING_PARAM},
489  { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
490  offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
491  { "write_xing", "Write the Xing header containing file duration.",
492  offsetof(MP3Context, write_xing), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
493  { NULL },
494 };
495 
496 static const AVClass mp3_muxer_class = {
497  .class_name = "MP3 muxer",
498  .item_name = av_default_item_name,
499  .option = options,
500  .version = LIBAVUTIL_VERSION_INT,
501 };
502 
503 static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
504 {
505  MP3Context *mp3 = s->priv_data;
506 
507  if (pkt->stream_index == mp3->audio_stream_idx) {
508  if (mp3->pics_to_write) {
509  /* buffer audio packets until we get all the pictures */
510  AVPacketList *pktl = av_mallocz(sizeof(*pktl));
511  int ret;
512  if (!pktl) {
513  av_log(s, AV_LOG_WARNING, "Not enough memory to buffer audio. Skipping picture streams\n");
514  mp3->pics_to_write = 0;
515  mp3_queue_flush(s);
516  return mp3_write_audio_packet(s, pkt);
517  }
518 
519  ret = av_copy_packet(&pktl->pkt, pkt);
520  if (ret < 0) {
521  av_freep(&pktl);
522  return ret;
523  }
524 
525  if (mp3->queue_end)
526  mp3->queue_end->next = pktl;
527  else
528  mp3->queue = pktl;
529  mp3->queue_end = pktl;
530  } else
531  return mp3_write_audio_packet(s, pkt);
532  } else {
533  int ret;
534 
535  /* warn only once for each stream */
536  if (s->streams[pkt->stream_index]->nb_frames == 1) {
537  av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
538  " ignoring.\n", pkt->stream_index);
539  }
540  if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)
541  return 0;
542 
543  if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)
544  return ret;
545  mp3->pics_to_write--;
546 
547  /* flush the buffered audio packets */
548  if (!mp3->pics_to_write &&
549  (ret = mp3_queue_flush(s)) < 0)
550  return ret;
551  }
552 
553  return 0;
554 }
555 
556 /**
557  * Write an ID3v2 header at beginning of stream
558  */
559 
560 static int mp3_write_header(struct AVFormatContext *s)
561 {
562  MP3Context *mp3 = s->priv_data;
563  int ret, i;
564 
565  if (mp3->id3v2_version &&
566  mp3->id3v2_version != 3 &&
567  mp3->id3v2_version != 4) {
568  av_log(s, AV_LOG_ERROR, "Invalid ID3v2 version requested: %d. Only "
569  "3, 4 or 0 (disabled) are allowed.\n", mp3->id3v2_version);
570  return AVERROR(EINVAL);
571  }
572 
573  /* check the streams -- we want exactly one audio and arbitrary number of
574  * video (attached pictures) */
575  mp3->audio_stream_idx = -1;
576  for (i = 0; i < s->nb_streams; i++) {
577  AVStream *st = s->streams[i];
578  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
579  if (mp3->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_MP3) {
580  av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
581  "audio stream is required.\n");
582  return AVERROR(EINVAL);
583  }
584  mp3->audio_stream_idx = i;
585  } else if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
586  av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
587  return AVERROR(EINVAL);
588  }
589  }
590  if (mp3->audio_stream_idx < 0) {
591  av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
592  return AVERROR(EINVAL);
593  }
594  mp3->pics_to_write = s->nb_streams - 1;
595 
596  if (mp3->pics_to_write && !mp3->id3v2_version) {
597  av_log(s, AV_LOG_ERROR, "Attached pictures were requested, but the "
598  "ID3v2 header is disabled.\n");
599  return AVERROR(EINVAL);
600  }
601 
602  if (mp3->id3v2_version) {
604  ret = ff_id3v2_write_metadata(s, &mp3->id3);
605  if (ret < 0)
606  return ret;
607  }
608 
609  if (!mp3->pics_to_write) {
610  if (mp3->id3v2_version)
612  mp3_write_xing(s);
613  }
614 
615  return 0;
616 }
617 
618 AVOutputFormat ff_mp3_muxer = {
619  .name = "mp3",
620  .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
621  .mime_type = "audio/mpeg",
622  .extensions = "mp3",
623  .priv_data_size = sizeof(MP3Context),
624  .audio_codec = AV_CODEC_ID_MP3,
625  .video_codec = AV_CODEC_ID_PNG,
626  .write_header = mp3_write_header,
627  .write_packet = mp3_write_packet,
631  .priv_class = &mp3_muxer_class,
632 };
633 #endif
#define MPA_STEREO
Definition: mpegaudio.h:45
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:147
#define XING_NUM_BAGS
Definition: mp3enc.c:81
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1272
AVOption.
Definition: opt.h:245
void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version, const char *magic)
Initialize an ID3v2 tag.
Definition: id3v2enc.c:152
static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mp3enc.c:307
int64_t xing_frame_offset
Definition: mp3enc.c:102
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb, int padding_bytes)
Finalize an opened ID3v2 tag.
Definition: id3v2enc.c:326
uint32_t pos
Definition: mp3enc.c:110
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3939
int size
Definition: avcodec.h:1589
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
uint8_t * av_stream_get_side_data(AVStream *stream, enum AVPacketSideDataType type, int *size)
Get side information from stream.
Definition: utils.c:5005
static AVPacket pkt
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1260
mpeg audio layer common tables.
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3931
uint64_t bag[XING_NUM_BAGS]
Definition: mp3enc.c:111
uint32_t track_peak
Peak track amplitude, with 100000 representing full scale (but values may overflow).
Definition: replaygain.h:39
Format I/O context.
Definition: avformat.h:1325
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:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Public dictionary API.
static void mp3_update_xing(AVFormatContext *s)
Definition: mp3enc.c:378
int32_t size
Definition: mp3enc.c:107
static int mp3_write_trailer(struct AVFormatContext *s)
Definition: mp3enc.c:436
uint8_t
AVOptions.
AVPacket pkt
Definition: avformat.h:1933
const uint16_t avpriv_mpa_freq_tab[3]
Definition: mpegaudiodata.c:40
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1393
int initial_padding
Audio only.
Definition: avcodec.h:4068
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
uint8_t * data
Definition: avcodec.h:1588
int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
uint32_t tag
Definition: movenc.c:1376
static const uint8_t xing_offtbl[2][2]
Definition: mp3enc.c:124
enum AVCodecID id
Definition: internal.h:51
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:511
static const uint8_t header[24]
Definition: sdr2.c:67
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:204
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:66
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
const OptionDef options[]
Definition: ffserver.c:3969
AVCRC audio_crc
Definition: mp3enc.c:98
int32_t album_gain
Same as track_gain, but for the whole album.
Definition: replaygain.h:43
#define av_log(a,...)
#define cm
Definition: dvbsubdec.c:36
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:275
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3968
AVPacketList * queue_end
Definition: mp3enc.c:121
#define U(x)
Definition: vp56_arith.h:37
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:189
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1539
static const uint16_t mask[17]
Definition: lzw.c:38
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:515
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3935
simple assert() macros that are a bit more flexible than ISO C assert().
uint32_t want
Definition: mp3enc.c:108
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
int initial_bitrate
Definition: mp3enc.c:112
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:129
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1381
static int mp3_write_xing(AVFormatContext *s)
Definition: mp3enc.c:129
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:243
int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
Convert and write all global metadata from s into an ID3v2 tag.
Definition: id3v2enc.c:239
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:129
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:190
int id3v2_version
Definition: mp3enc.c:89
#define FFMIN(a, b)
Definition: common.h:96
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:213
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:464
const char * name
Definition: avformat.h:522
static int query_codec(enum AVCodecID id, int std_compliance)
Definition: mp3enc.c:460
int32_t
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:357
AVPacketList * queue
Definition: mp3enc.c:121
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:26
AVDictionary * metadata
Definition: avformat.h:945
int write_id3v1
Definition: mp3enc.c:90
#define FF_ARRAY_ELEMS(a)
int metadata_header_padding
Number of bytes to be written as padding in a metadata header.
Definition: avformat.h:1799
Stream structure.
Definition: avformat.h:876
int pics_to_write
Definition: mp3enc.c:118
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:484
int has_variable_bitrate
Definition: mp3enc.c:113
int audio_stream_idx
Definition: mp3enc.c:116
int xing_offset
Definition: mp3enc.c:104
AVIOContext * pb
I/O context.
Definition: avformat.h:1367
int32_t frames
Definition: mp3enc.c:106
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:182
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:563
int av_copy_packet(AVPacket *dst, const AVPacket *src)
Copy packet, including contents.
Definition: avpacket.c:260
void * buf
Definition: avisynth_c.h:553
int xing_frame_size
Definition: mp3enc.c:96
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_WB32(p, v)
Definition: intreadwrite.h:419
uint8_t * xing_frame
Definition: mp3enc.c:95
#define MPA_MONO
Definition: mpegaudio.h:48
#define XING_SIZE
Definition: mp3enc.c:84
uint32_t audio_size
Definition: mp3enc.c:99
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:452
This side data should be associated with an audio stream and contains ReplayGain information in form ...
Definition: avcodec.h:1403
static int id3v1_set_string(AVFormatContext *s, const char *key, uint8_t *buf, int buf_size)
Definition: mp3enc.c:39
static int flags
Definition: cpu.c:47
int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
Write an attached picture from pkt into an ID3v2 tag.
Definition: id3v2enc.c:257
int write_xing
Definition: mp3enc.c:91
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:343
int sample_rate
Audio only.
Definition: avcodec.h:4049
MPEG Audio header decoder.
static int mp3_queue_flush(AVFormatContext *s)
Definition: mp3enc.c:358
Main libavformat public API header.
struct AVPacketList * next
Definition: avformat.h:1934
mpeg audio declarations for both encoder and decoder.
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:932
#define MKBETAG(a, b, c, d)
Definition: common.h:343
#define XING_TOC_SIZE
Definition: mp3enc.c:82
char * value
Definition: dict.h:87
void * priv_data
Format private data.
Definition: avformat.h:1353
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:498
int channels
Audio only.
Definition: avcodec.h:4045
#define ID3v1_TAG_SIZE
Definition: id3v1.h:27
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:354
#define av_freep(p)
int32_t track_gain
Track replay gain in microbels (divide by 100000 to get the value in dB).
Definition: replaygain.h:34
const uint16_t avpriv_mpa_bitrate_tab[2][3][15]
Definition: mpegaudiodata.c:30
AVCodecParameters * codecpar
Definition: avformat.h:1228
int stream_index
Definition: avcodec.h:1590
static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
Definition: mp3enc.c:48
#define MKTAG(a, b, c, d)
Definition: common.h:342
ReplayGain information (see http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1.0_specification).
Definition: replaygain.h:29
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition: id3v1.c:27
This structure stores compressed data.
Definition: avcodec.h:1565
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
uint32_t AVCRC
Definition: crc.h:35
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
ID3v2EncContext id3
Definition: mp3enc.c:88
#define av_unused
Definition: attributes.h:126
static void mp3_xing_add_frame(MP3Context *mp3, AVPacket *pkt)
Definition: mp3enc.c:281
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
uint32_t seen
Definition: mp3enc.c:109