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