FFmpeg
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 "mux.h"
27 #include "rawenc.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/mem.h"
30 #include "libavcodec/mpegaudio.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/dict.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/crc.h"
39 #include "libavutil/mathematics.h"
40 #include "libavutil/replaygain.h"
41 
42 static int id3v1_set_string(AVFormatContext *s, const char *key,
43  uint8_t *buf, int buf_size)
44 {
46  if ((tag = av_dict_get(s->metadata, key, NULL, 0)))
47  av_strlcpy(buf, tag->value, buf_size);
48  return !!tag;
49 }
50 
51 // refer to: http://id3.org/ID3v1
52 static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
53 {
55  int i, count = 0;
56 
57  memset(buf, 0, ID3v1_TAG_SIZE); /* fail safe */
58  buf[0] = 'T';
59  buf[1] = 'A';
60  buf[2] = 'G';
61  /* we knowingly overspecify each tag length by one byte to compensate for the mandatory null byte added by av_strlcpy */
62  count += id3v1_set_string(s, "TIT2", buf + 3, 30 + 1); //title
63  count += id3v1_set_string(s, "TPE1", buf + 33, 30 + 1); //author|artist
64  count += id3v1_set_string(s, "TALB", buf + 63, 30 + 1); //album
65  if ((tag = av_dict_get(s->metadata, "TYER", NULL, 0))) { //year
66  av_strlcpy(buf + 93, tag->value, 4 + 1);
67  count++;
68  } else if ((tag = av_dict_get(s->metadata, "TDRC", NULL, 0))) {
69  av_strlcpy(buf + 93, tag->value, 4 + 1);
70  count++;
71  } else if ((tag = av_dict_get(s->metadata, "TDAT", NULL, 0))) {
72  av_strlcpy(buf + 93, tag->value, 4 + 1);
73  count++;
74  }
75 
76  count += id3v1_set_string(s, "comment", buf + 97, 30 + 1);
77  if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) { //track
78  buf[125] = 0;
79  buf[126] = atoi(tag->value);
80  count++;
81  }
82  buf[127] = 0xFF; /* default to unknown genre */
83  if ((tag = av_dict_get(s->metadata, "TCON", NULL, 0))) { //genre
84  for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
85  if (!av_strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
86  buf[127] = i;
87  count++;
88  break;
89  }
90  }
91  }
92  return count;
93 }
94 
95 #define XING_NUM_BAGS 400
96 #define XING_TOC_SIZE 100
97 // size of the XING/LAME data, starting from the Xing tag
98 #define XING_SIZE 156
99 
100 typedef struct MP3Context {
101  const AVClass *class;
106 
107  /* xing header */
108  // a buffer containing the whole XING/LAME frame
109  uint8_t *xing_frame;
111 
112  AVCRC audio_crc; // CRC of the audio data
113  uint32_t audio_size; // total size of the audio data
114 
115  // offset of the XING/LAME frame in the file
117  // offset of the XING/INFO tag in the frame
119 
122  uint32_t want;
123  uint32_t seen;
124  uint32_t pos;
125  uint64_t bag[XING_NUM_BAGS];
128  int delay;
129  int padding;
130 
131  /* index of the audio stream */
133  /* number of attached pictures we still need to write */
135 
136  /* audio packets are queued here until we get all the attached pictures */
138 } MP3Context;
139 
140 static const uint8_t xing_offtbl[2][2] = {{32, 17}, {17, 9}};
141 
142 /*
143  * Write an empty XING header and initialize respective data.
144  */
146 {
147  MP3Context *mp3 = s->priv_data;
148  AVCodecParameters *par = s->streams[mp3->audio_stream_idx]->codecpar;
149  AVDictionaryEntry *enc = av_dict_get(s->streams[mp3->audio_stream_idx]->metadata, "encoder", NULL, 0);
150  AVIOContext *dyn_ctx;
151  int32_t header;
152  MPADecodeHeader mpah;
153  int srate_idx, i, channels;
154  int bitrate_idx;
155  int best_bitrate_idx = -1;
156  int best_bitrate_error = INT_MAX;
157  int ret;
158  int ver = 0;
159  int bytes_needed;
160 
161  if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL) || !mp3->write_xing)
162  return 0;
163 
164  for (i = 0; i < FF_ARRAY_ELEMS(ff_mpa_freq_tab); i++) {
165  const uint16_t base_freq = ff_mpa_freq_tab[i];
166 
167  if (par->sample_rate == base_freq) ver = 0x3; // MPEG 1
168  else if (par->sample_rate == base_freq / 2) ver = 0x2; // MPEG 2
169  else if (par->sample_rate == base_freq / 4) ver = 0x0; // MPEG 2.5
170  else continue;
171 
172  srate_idx = i;
173  break;
174  }
175  if (i == FF_ARRAY_ELEMS(ff_mpa_freq_tab)) {
176  av_log(s, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n");
177  return -1;
178  }
179 
180  switch (par->ch_layout.nb_channels) {
181  case 1: channels = MPA_MONO; break;
182  case 2: channels = MPA_STEREO; break;
183  default: av_log(s, AV_LOG_WARNING, "Unsupported number of channels, "
184  "not writing Xing header.\n");
185  return -1;
186  }
187 
188  /* dummy MPEG audio header */
189  header = 0xffU << 24; // sync
190  header |= (0x7 << 5 | ver << 3 | 0x1 << 1 | 0x1) << 16; // sync/audio-version/layer 3/no crc*/
191  header |= (srate_idx << 2) << 8;
192  header |= channels << 6;
193 
194  for (bitrate_idx = 1; bitrate_idx < 15; bitrate_idx++) {
195  int bit_rate = 1000 * ff_mpa_bitrate_tab[ver != 3][3 - 1][bitrate_idx];
196  int error = FFABS(bit_rate - par->bit_rate);
197 
198  if (error < best_bitrate_error) {
199  best_bitrate_error = error;
200  best_bitrate_idx = bitrate_idx;
201  }
202  }
203  av_assert0(best_bitrate_idx >= 0);
204 
205  for (bitrate_idx = best_bitrate_idx; ; bitrate_idx++) {
206  int32_t mask = bitrate_idx << (4 + 8);
207  if (15 == bitrate_idx)
208  return -1;
209  header |= mask;
210 
212  av_assert0(ret >= 0);
213  mp3->xing_offset = xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1] + 4;
214  bytes_needed = mp3->xing_offset + XING_SIZE;
215 
216  if (bytes_needed <= mpah.frame_size)
217  break;
218 
219  header &= ~mask;
220  }
221 
222  ret = avio_open_dyn_buf(&dyn_ctx);
223  if (ret < 0)
224  return ret;
225 
226  avio_wb32(dyn_ctx, header);
227 
228  ffio_fill(dyn_ctx, 0, mp3->xing_offset - 4);
229  ffio_wfourcc(dyn_ctx, "Xing");
230  avio_wb32(dyn_ctx, 0x01 | 0x02 | 0x04 | 0x08); // frames / size / TOC / vbr scale
231 
232  mp3->size = mpah.frame_size;
233  mp3->want=1;
234  mp3->seen=0;
235  mp3->pos=0;
236 
237  avio_wb32(dyn_ctx, 0); // frames
238  avio_wb32(dyn_ctx, 0); // size
239 
240  // TOC
241  for (i = 0; i < XING_TOC_SIZE; i++)
242  avio_w8(dyn_ctx, (uint8_t)(255 * i / XING_TOC_SIZE));
243 
244  // vbr quality
245  // we write it, because some (broken) tools always expect it to be present
246  avio_wb32(dyn_ctx, 0);
247 
248  // encoder short version string
249  if (enc) {
250  uint8_t encoder_str[9] = { 0 };
251  if ( strlen(enc->value) > sizeof(encoder_str)
252  && !strcmp("Lavc libmp3lame", enc->value)) {
253  memcpy(encoder_str, "Lavf lame", 9);
254  } else
255  memcpy(encoder_str, enc->value, FFMIN(strlen(enc->value), sizeof(encoder_str)));
256 
257  avio_write(dyn_ctx, encoder_str, sizeof(encoder_str));
258  } else
259  avio_write(dyn_ctx, "Lavf\0\0\0\0\0", 9);
260 
261  avio_w8(dyn_ctx, 0); // tag revision 0 / unknown vbr method
262  avio_w8(dyn_ctx, 0); // unknown lowpass filter value
263  ffio_fill(dyn_ctx, 0, 8); // empty replaygain fields
264  avio_w8(dyn_ctx, 0); // unknown encoding flags
265  avio_w8(dyn_ctx, 0); // unknown abr/minimal bitrate
266  avio_wb24(dyn_ctx, 0); // empty encoder delay/padding
267 
268  avio_w8(dyn_ctx, 0); // misc
269  avio_w8(dyn_ctx, 0); // mp3gain
270  avio_wb16(dyn_ctx, 0); // preset
271 
272  // audio length and CRCs (will be updated later)
273  avio_wb32(dyn_ctx, 0); // music length
274  avio_wb16(dyn_ctx, 0); // music crc
275  avio_wb16(dyn_ctx, 0); // tag crc
276 
277  ffio_fill(dyn_ctx, 0, mpah.frame_size - bytes_needed);
278 
279  mp3->xing_frame_size = avio_close_dyn_buf(dyn_ctx, &mp3->xing_frame);
280  mp3->xing_frame_offset = avio_tell(s->pb);
281  avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
282 
283  mp3->audio_size = mp3->xing_frame_size;
284 
285  return 0;
286 }
287 
288 /*
289  * Add a frame to XING data.
290  * Following lame's "VbrTag.c".
291  */
293 {
294  int i;
295 
296  mp3->frames++;
297  mp3->seen++;
298  mp3->size += pkt->size;
299 
300  if (mp3->want == mp3->seen) {
301  mp3->bag[mp3->pos] = mp3->size;
302 
303  if (XING_NUM_BAGS == ++mp3->pos) {
304  /* shrink table to half size by throwing away each second bag. */
305  for (i = 1; i < XING_NUM_BAGS; i += 2)
306  mp3->bag[i >> 1] = mp3->bag[i];
307 
308  /* double wanted amount per bag. */
309  mp3->want *= 2;
310  /* adjust current position to half of table size. */
311  mp3->pos = XING_NUM_BAGS / 2;
312  }
313 
314  mp3->seen = 0;
315  }
316 }
317 
319 {
320  MP3Context *mp3 = s->priv_data;
321 
322  if (pkt->data && pkt->size >= 4) {
323  MPADecodeHeader mpah;
324  int ret;
325  int av_unused base;
326  uint32_t h;
327 
328  h = AV_RB32(pkt->data);
330  if (ret >= 0) {
331  if (!mp3->initial_bitrate)
332  mp3->initial_bitrate = mpah.bit_rate;
333  if ((mpah.bit_rate == 0) || (mp3->initial_bitrate != mpah.bit_rate))
334  mp3->has_variable_bitrate = 1;
335  } else {
336  av_log(s, AV_LOG_WARNING, "Audio packet of size %d (starting with %08"PRIX32"...) "
337  "is invalid, writing it anyway.\n", pkt->size, h);
338  }
339 
340 #ifdef FILTER_VBR_HEADERS
341  /* filter out XING and INFO headers. */
342  base = 4 + xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1];
343 
344  if (base + 4 <= pkt->size) {
345  uint32_t v = AV_RB32(pkt->data + base);
346 
347  if (MKBETAG('X','i','n','g') == v || MKBETAG('I','n','f','o') == v)
348  return 0;
349  }
350 
351  /* filter out VBRI headers. */
352  base = 4 + 32;
353 
354  if (base + 4 <= pkt->size && MKBETAG('V','B','R','I') == AV_RB32(pkt->data + base))
355  return 0;
356 #endif
357 
358  if (mp3->xing_offset) {
359  uint8_t *side_data = NULL;
360  size_t side_data_size;
361 
362  mp3_xing_add_frame(mp3, pkt);
363  mp3->audio_size += pkt->size;
365  mp3->audio_crc, pkt->data, pkt->size);
366 
367  side_data = av_packet_get_side_data(pkt,
369  &side_data_size);
370  if (side_data && side_data_size >= 10) {
371  mp3->padding = FFMAX(AV_RL32(side_data + 4) + 528 + 1, 0);
372  if (!mp3->delay)
373  mp3->delay = FFMAX(AV_RL32(side_data) - 528 - 1, 0);
374  } else {
375  mp3->padding = 0;
376  }
377  }
378  }
379 
380  return ff_raw_write_packet(s, pkt);
381 }
382 
384 {
385  MP3Context *mp3 = s->priv_data;
386  AVPacket *const pkt = ffformatcontext(s)->pkt;
387  int ret = 0, write = 1;
388 
389  ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
390  mp3_write_xing(s);
391 
392  while (mp3->queue.head) {
394  if (write && (ret = mp3_write_audio_packet(s, pkt)) < 0)
395  write = 0;
397  }
398  return ret;
399 }
400 
402 {
403  MP3Context *mp3 = s->priv_data;
404  const AVPacketSideData *sd;
405  AVReplayGain *rg;
406  uint16_t tag_crc;
407  uint8_t *toc;
408  int i;
409  int64_t old_pos = avio_tell(s->pb);
410 
411  /* replace "Xing" identification string with "Info" for CBR files. */
412  if (!mp3->has_variable_bitrate)
413  AV_WL32(mp3->xing_frame + mp3->xing_offset, MKTAG('I', 'n', 'f', 'o'));
414 
415  AV_WB32(mp3->xing_frame + mp3->xing_offset + 8, mp3->frames);
416  AV_WB32(mp3->xing_frame + mp3->xing_offset + 12, mp3->size);
417 
418  toc = mp3->xing_frame + mp3->xing_offset + 16;
419  toc[0] = 0; // first toc entry has to be zero.
420  for (i = 1; i < XING_TOC_SIZE; ++i) {
421  int j = i * mp3->pos / XING_TOC_SIZE;
422  int seek_point = 256LL * mp3->bag[j] / mp3->size;
423  toc[i] = FFMIN(seek_point, 255);
424  }
425 
426  /* write replaygain */
427  sd = av_packet_side_data_get(s->streams[0]->codecpar->coded_side_data,
428  s->streams[0]->codecpar->nb_coded_side_data,
430  if (sd && sd->size >= sizeof(*rg)) {
431  uint16_t val;
432 
433  rg = (AVReplayGain *)sd->data;
434  AV_WB32(mp3->xing_frame + mp3->xing_offset + 131,
435  av_rescale(rg->track_peak, 1 << 23, 100000));
436 
437  if (rg->track_gain != INT32_MIN) {
438  val = FFABS(rg->track_gain / 10000) & ((1 << 9) - 1);
439  val |= (rg->track_gain < 0) << 9;
440  val |= 1 << 13;
441  AV_WB16(mp3->xing_frame + mp3->xing_offset + 135, val);
442  }
443 
444  if (rg->album_gain != INT32_MIN) {
445  val = FFABS(rg->album_gain / 10000) & ((1 << 9) - 1);
446  val |= (rg->album_gain < 0) << 9;
447  val |= 1 << 14;
448  AV_WB16(mp3->xing_frame + mp3->xing_offset + 137, val);
449  }
450  }
451 
452  /* write encoder delay/padding */
453  if (mp3->delay >= 1 << 12) {
454  mp3->delay = (1 << 12) - 1;
455  av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n");
456  }
457  if (mp3->padding >= 1 << 12) {
458  mp3->padding = (1 << 12) - 1;
459  av_log(s, AV_LOG_WARNING, "Too many samples of trailing padding.\n");
460  }
461  AV_WB24(mp3->xing_frame + mp3->xing_offset + 141, (mp3->delay << 12) + mp3->padding);
462 
463  AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size);
464  AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc);
465 
466  tag_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), 0, mp3->xing_frame, 190);
467  AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 2, tag_crc);
468 
469  avio_seek(s->pb, mp3->xing_frame_offset, SEEK_SET);
470  avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
471  avio_seek(s->pb, old_pos, SEEK_SET);
472 }
473 
475 {
476  uint8_t buf[ID3v1_TAG_SIZE];
477  MP3Context *mp3 = s->priv_data;
478 
479  if (mp3->pics_to_write) {
480  av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
481  "attached pictures.\n");
483  }
484 
485  /* write the id3v1 tag */
486  if (mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
487  avio_write(s->pb, buf, ID3v1_TAG_SIZE);
488  }
489 
490  if (mp3->xing_offset)
492 
493  return 0;
494 }
495 
496 static int query_codec(enum AVCodecID id, int std_compliance)
497 {
499 
500  if (id == AV_CODEC_ID_MP3)
501  return 1;
502 
503  while(cm->id != AV_CODEC_ID_NONE) {
504  if(id == cm->id)
505  return MKTAG('A', 'P', 'I', 'C');
506  cm++;
507  }
508  return 0;
509 }
510 
511 static const AVOption options[] = {
512  { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
513  offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 0, 4, AV_OPT_FLAG_ENCODING_PARAM},
514  { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
515  offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
516  { "write_xing", "Write the Xing header containing file duration.",
517  offsetof(MP3Context, write_xing), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
518  { NULL },
519 };
520 
521 static const AVClass mp3_muxer_class = {
522  .class_name = "MP3 muxer",
523  .item_name = av_default_item_name,
524  .option = options,
525  .version = LIBAVUTIL_VERSION_INT,
526 };
527 
529 {
530  MP3Context *mp3 = s->priv_data;
531 
532  if (pkt->stream_index == mp3->audio_stream_idx) {
533  if (mp3->pics_to_write) {
534  /* buffer audio packets until we get all the pictures */
535  int ret = avpriv_packet_list_put(&mp3->queue, pkt, NULL, 0);
536 
537  if (ret < 0) {
538  av_log(s, AV_LOG_WARNING, "Not enough memory to buffer audio. Skipping picture streams\n");
539  mp3->pics_to_write = 0;
541  return mp3_write_audio_packet(s, pkt);
542  }
543  } else
544  return mp3_write_audio_packet(s, pkt);
545  } else {
546  int ret;
547 
548  /* warn only once for each stream */
549  if (s->streams[pkt->stream_index]->nb_frames == 1) {
550  av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
551  " ignoring.\n", pkt->stream_index);
552  }
553  if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)
554  return 0;
555 
556  if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)
557  return ret;
558  mp3->pics_to_write--;
559 
560  /* flush the buffered audio packets */
561  if (!mp3->pics_to_write &&
562  (ret = mp3_queue_flush(s)) < 0)
563  return ret;
564  }
565 
566  return 0;
567 }
568 
569 /**
570  * Write an ID3v2 header at beginning of stream
571  */
572 
573 static int mp3_init(struct AVFormatContext *s)
574 {
575  MP3Context *mp3 = s->priv_data;
576  int i;
577 
578  if (mp3->id3v2_version &&
579  mp3->id3v2_version != 3 &&
580  mp3->id3v2_version != 4) {
581  av_log(s, AV_LOG_ERROR, "Invalid ID3v2 version requested: %d. Only "
582  "3, 4 or 0 (disabled) are allowed.\n", mp3->id3v2_version);
583  return AVERROR(EINVAL);
584  }
585 
586  /* check the streams -- we want exactly one audio and arbitrary number of
587  * video (attached pictures) */
588  mp3->audio_stream_idx = -1;
589  for (i = 0; i < s->nb_streams; i++) {
590  AVStream *st = s->streams[i];
591  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
592  if (mp3->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_MP3) {
593  av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
594  "audio stream is required.\n");
595  return AVERROR(EINVAL);
596  }
597  mp3->audio_stream_idx = i;
598  } else if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
599  av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
600  return AVERROR(EINVAL);
601  }
602  }
603  if (mp3->audio_stream_idx < 0) {
604  av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
605  return AVERROR(EINVAL);
606  }
607  mp3->pics_to_write = s->nb_streams - 1;
608 
609  if (mp3->pics_to_write && !mp3->id3v2_version) {
610  av_log(s, AV_LOG_ERROR, "Attached pictures were requested, but the "
611  "ID3v2 header is disabled.\n");
612  return AVERROR(EINVAL);
613  }
614 
615  return 0;
616 }
617 
618 static int mp3_write_header(struct AVFormatContext *s)
619 {
620  MP3Context *mp3 = s->priv_data;
621  int ret;
622 
623  if (mp3->id3v2_version) {
625  ret = ff_id3v2_write_metadata(s, &mp3->id3);
626  if (ret < 0)
627  return ret;
628  }
629 
630  if (!mp3->pics_to_write) {
631  if (mp3->id3v2_version)
632  ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
633  mp3_write_xing(s);
634  }
635 
636  return 0;
637 }
638 
639 static void mp3_deinit(struct AVFormatContext *s)
640 {
641  MP3Context *mp3 = s->priv_data;
642 
644  av_freep(&mp3->xing_frame);
645 }
646 
648  .p.name = "mp3",
649  .p.long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
650  .p.mime_type = "audio/mpeg",
651  .p.extensions = "mp3",
652  .priv_data_size = sizeof(MP3Context),
653  .p.audio_codec = AV_CODEC_ID_MP3,
654  .p.video_codec = AV_CODEC_ID_PNG,
655  .init = mp3_init,
656  .write_header = mp3_write_header,
657  .write_packet = mp3_write_packet,
658  .write_trailer = mp3_write_trailer,
659  .deinit = mp3_deinit,
660  .query_codec = query_codec,
661  .p.flags = AVFMT_NOTIMESTAMPS,
662  .p.priv_class = &mp3_muxer_class,
663 };
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
MPA_MONO
#define MPA_MONO
Definition: mpegaudio.h:49
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:427
MP3Context::pos
uint32_t pos
Definition: mp3enc.c:124
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
XING_SIZE
#define XING_SIZE
Definition: mp3enc.c:98
PacketList::head
PacketListEntry * head
Definition: packet_internal.h:34
ID3v1_TAG_SIZE
#define ID3v1_TAG_SIZE
Definition: id3v1.h:27
ID3v2EncContext
Definition: id3v2.h:51
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
xing_offtbl
static const uint8_t xing_offtbl[2][2]
Definition: mp3enc.c:140
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:424
ffio_wfourcc
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:124
MP3Context::bag
uint64_t bag[XING_NUM_BAGS]
Definition: mp3enc.c:125
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:188
AVCRC
uint32_t AVCRC
Definition: crc.h:46
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
av_strcasecmp
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:207
av_unused
#define av_unused
Definition: attributes.h:131
id3v2.h
ID3v1_GENRE_MAX
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
MP3Context::delay
int delay
Definition: mp3enc.c:128
AVPacketSideData
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition: packet.h:375
mpegaudiodecheader.h
AVPacket::data
uint8_t * data
Definition: packet.h:524
AVOption
AVOption.
Definition: opt.h:346
AVReplayGain::album_gain
int32_t album_gain
Same as track_gain, but for the whole album.
Definition: replaygain.h:43
PacketList
Definition: packet_internal.h:33
MPADecodeHeader
Definition: mpegaudiodecheader.h:47
ff_mp3_muxer
const FFOutputFormat ff_mp3_muxer
Definition: mp3enc.c:647
MP3Context
Definition: mp3enc.c:100
base
uint8_t base
Definition: vp3data.h:128
mathematics.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
replaygain.h
id3v1.h
ff_id3v2_start
void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version, const char *magic)
Initialize an ID3v2 tag.
Definition: id3v2enc.c:206
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
crc.h
CodecMime
Definition: internal.h:47
AVPacketSideData::size
size_t size
Definition: packet.h:377
AV_PKT_DATA_REPLAYGAIN
@ AV_PKT_DATA_REPLAYGAIN
This side data should be associated with an audio stream and contains ReplayGain information in form ...
Definition: packet.h:100
MP3Context::audio_crc
AVCRC audio_crc
Definition: mp3enc.c:112
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
avpriv_packet_list_get
int avpriv_packet_list_get(PacketList *pkt_buffer, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
Definition: packet.c:580
AV_CRC_16_ANSI_LE
@ AV_CRC_16_ANSI_LE
Definition: crc.h:54
val
static double val(void *priv, double ch)
Definition: aeval.c:78
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:441
mp3_write_header
static int mp3_write_header(struct AVFormatContext *s)
Definition: mp3enc.c:618
id3v1_create_tag
static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
Definition: mp3enc.c:52
avio_close_dyn_buf
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1407
avassert.h
mp3_write_audio_packet
static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mp3enc.c:318
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
avpriv_mpegaudio_decode_header
int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
Definition: mpegaudiodecheader.c:34
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
mask
static const uint16_t mask[17]
Definition: lzw.c:38
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1362
av_dict_get
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:62
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
MP3Context::initial_bitrate
int initial_bitrate
Definition: mp3enc.c:126
MP3Context::size
int32_t size
Definition: mp3enc.c:121
MP3Context::want
uint32_t want
Definition: mp3enc.c:122
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
query_codec
static int query_codec(enum AVCodecID id, int std_compliance)
Definition: mp3enc.c:496
AVPacketSideData::data
uint8_t * data
Definition: packet.h:376
channels
channels
Definition: aptx.h:31
ff_raw_write_packet
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:31
XING_NUM_BAGS
#define XING_NUM_BAGS
Definition: mp3enc.c:95
mp3_write_trailer
static int mp3_write_trailer(struct AVFormatContext *s)
Definition: mp3enc.c:474
ff_id3v2_write_metadata
int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
Convert and write all global metadata from s into an ID3v2 tag.
Definition: id3v2enc.c:331
key
const char * key
Definition: hwcontext_opencl.c:189
AVReplayGain::track_peak
uint32_t track_peak
Peak track amplitude, with 100000 representing full scale (but values may overflow).
Definition: replaygain.h:39
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:73
AV_CODEC_ID_PNG
@ AV_CODEC_ID_PNG
Definition: codec_id.h:113
avpriv_packet_list_free
void avpriv_packet_list_free(PacketList *pkt_buf)
Wipe the list and unref all the packets in it.
Definition: packet.c:594
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
mp3_queue_flush
static int mp3_queue_flush(AVFormatContext *s)
Definition: mp3enc.c:383
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:403
avpriv_packet_list_put
int avpriv_packet_list_put(PacketList *packet_buffer, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Definition: packet.c:541
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
MP3Context::padding
int padding
Definition: mp3enc.c:129
MP3Context::write_id3v1
int write_id3v1
Definition: mp3enc.c:104
FFOutputFormat
Definition: mux.h:61
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:179
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:187
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
mp3_write_packet
static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mp3enc.c:528
AV_OPT_FLAG_ENCODING_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition: opt.h:269
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
mp3_xing_add_frame
static void mp3_xing_add_frame(MP3Context *mp3, AVPacket *pkt)
Definition: mp3enc.c:292
ff_id3v2_mime_tags
const CodecMime ff_id3v2_mime_tags[]
Definition: id3v2.c:133
MP3Context::pics_to_write
int pics_to_write
Definition: mp3enc.c:134
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
av_packet_side_data_get
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
Definition: packet.c:654
AV_WB32
#define AV_WB32(p, v)
Definition: intreadwrite.h:417
ff_id3v2_write_apic
int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
Write an attached picture from pkt into an ID3v2 tag.
Definition: id3v2enc.c:352
MP3Context::xing_offset
int xing_offset
Definition: mp3enc.c:118
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:525
MPA_STEREO
#define MPA_STEREO
Definition: mpegaudio.h:46
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
MP3Context::xing_frame_size
int xing_frame_size
Definition: mp3enc.c:110
ID3v2_DEFAULT_MAGIC
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
AV_RB32
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:96
AVReplayGain::track_gain
int32_t track_gain
Track replay gain in microbels (divide by 100000 to get the value in dB).
Definition: replaygain.h:34
AV_WB24
#define AV_WB24(p, d)
Definition: intreadwrite.h:448
header
static const uint8_t header[24]
Definition: sdr2.c:68
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:365
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
MP3Context::id3v2_version
int id3v2_version
Definition: mp3enc.c:103
MP3Context::write_xing
int write_xing
Definition: mp3enc.c:105
rawenc.h
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
XING_TOC_SIZE
#define XING_TOC_SIZE
Definition: mp3enc.c:96
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
avio_internal.h
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition: packet.c:252
MP3Context::has_variable_bitrate
int has_variable_bitrate
Definition: mp3enc.c:127
mp3_deinit
static void mp3_deinit(struct AVFormatContext *s)
Definition: mp3enc.c:639
ff_id3v1_genre_str
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition: id3v1.c:26
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
MP3Context::frames
int32_t frames
Definition: mp3enc.c:120
av_rescale
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
mpegaudio.h
MP3Context::audio_stream_idx
int audio_stream_idx
Definition: mp3enc.c:132
tag
uint32_t tag
Definition: movenc.c:1787
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
AVClass::class_name
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:71
avformat.h
dict.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
av_crc
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:392
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
options
static const AVOption options[]
Definition: mp3enc.c:511
cm
#define cm
Definition: dvbsubdec.c:40
ff_mpa_freq_tab
const uint16_t ff_mpa_freq_tab[3]
Definition: mpegaudiotabs.h:37
AV_PKT_DATA_SKIP_SAMPLES
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
Definition: packet.h:157
MP3Context::audio_size
uint32_t audio_size
Definition: mp3enc.c:113
AVPacket::stream_index
int stream_index
Definition: packet.h:526
mpegaudiodata.h
ff_id3v2_finish
void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb, int padding_bytes)
Finalize an opened ID3v2 tag.
Definition: id3v2enc.c:421
MP3Context::xing_frame
uint8_t * xing_frame
Definition: mp3enc.c:109
AVReplayGain
ReplayGain information (see http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1....
Definition: replaygain.h:29
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
MP3Context::queue
PacketList queue
Definition: mp3enc.c:137
packet_internal.h
FFFormatContext::pkt
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition: internal.h:134
avio_wb24
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:455
ff_mpa_bitrate_tab
const FF_VISIBILITY_PUSH_HIDDEN uint16_t ff_mpa_bitrate_tab[2][3][15]
Definition: mpegaudiotabs.h:27
AVDictionaryEntry
Definition: dict.h:89
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:501
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:251
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
MP3Context::id3
ID3v2EncContext id3
Definition: mp3enc.c:102
int32_t
int32_t
Definition: audioconvert.c:56
id3v1_set_string
static int id3v1_set_string(AVFormatContext *s, const char *key, uint8_t *buf, int buf_size)
Definition: mp3enc.c:42
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:443
MP3Context::seen
uint32_t seen
Definition: mp3enc.c:123
av_strlcpy
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:85
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
mp3_write_xing
static int mp3_write_xing(AVFormatContext *s)
Definition: mp3enc.c:145
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
h
h
Definition: vp9dsp_template.c:2038
AVDictionaryEntry::value
char * value
Definition: dict.h:91
avstring.h
mp3_init
static int mp3_init(struct AVFormatContext *s)
Write an ID3v2 header at beginning of stream.
Definition: mp3enc.c:573
mp3_update_xing
static void mp3_update_xing(AVFormatContext *s)
Definition: mp3enc.c:401
mp3_muxer_class
static const AVClass mp3_muxer_class
Definition: mp3enc.c:521
MP3Context::xing_frame_offset
int64_t xing_frame_offset
Definition: mp3enc.c:116
mux.h