Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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  count += id3v1_set_string(s, "TIT2", buf + 3, 30); //title
58  count += id3v1_set_string(s, "TPE1", buf + 33, 30); //author|artist
59  count += id3v1_set_string(s, "TALB", buf + 63, 30); //album
60  count += id3v1_set_string(s, "TDRL", buf + 93, 4); //date
61  count += id3v1_set_string(s, "comment", buf + 97, 30);
62  if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) { //track
63  buf[125] = 0;
64  buf[126] = atoi(tag->value);
65  count++;
66  }
67  buf[127] = 0xFF; /* default to unknown genre */
68  if ((tag = av_dict_get(s->metadata, "TCON", NULL, 0))) { //genre
69  for(i = 0; i <= ID3v1_GENRE_MAX; i++) {
70  if (!av_strcasecmp(tag->value, ff_id3v1_genre_str[i])) {
71  buf[127] = i;
72  count++;
73  break;
74  }
75  }
76  }
77  return count;
78 }
79 
80 #define XING_NUM_BAGS 400
81 #define XING_TOC_SIZE 100
82 // size of the XING/LAME data, starting from the Xing tag
83 #define XING_SIZE 156
84 
85 typedef struct MP3Context {
86  const AVClass *class;
91 
92  /* xing header */
93  // a buffer containing the whole XING/LAME frame
96 
97  AVCRC audio_crc; // CRC of the audio data
98  uint32_t audio_size; // total size of the audio data
99 
100  // offset of the XING/LAME frame in the file
102  // offset of the XING/INFO tag in the frame
104 
107  uint32_t want;
108  uint32_t seen;
109  uint32_t pos;
110  uint64_t bag[XING_NUM_BAGS];
113 
114  /* index of the audio stream */
116  /* number of attached pictures we still need to write */
118 
119  /* audio packets are queued here until we get all the attached pictures */
121 } MP3Context;
122 
123 static const uint8_t xing_offtbl[2][2] = {{32, 17}, {17, 9}};
124 
125 /*
126  * Write an empty XING header and initialize respective data.
127  */
129 {
130  MP3Context *mp3 = s->priv_data;
132  AVDictionaryEntry *enc = av_dict_get(s->streams[mp3->audio_stream_idx]->metadata, "encoder", NULL, 0);
133  AVIOContext *dyn_ctx;
134  int32_t header;
135  MPADecodeHeader mpah;
136  int srate_idx, i, channels;
137  int bitrate_idx;
138  int best_bitrate_idx;
139  int best_bitrate_error = INT_MAX;
140  int ret;
141  int ver = 0;
142  int lsf, bytes_needed;
143 
144  if (!s->pb->seekable || !mp3->write_xing)
145  return;
146 
147  for (i = 0; i < FF_ARRAY_ELEMS(avpriv_mpa_freq_tab); i++) {
148  const uint16_t base_freq = avpriv_mpa_freq_tab[i];
149 
150  if (par->sample_rate == base_freq) ver = 0x3; // MPEG 1
151  else if (par->sample_rate == base_freq / 2) ver = 0x2; // MPEG 2
152  else if (par->sample_rate == base_freq / 4) ver = 0x0; // MPEG 2.5
153  else continue;
154 
155  srate_idx = i;
156  break;
157  }
159  av_log(s, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing "
160  "header.\n");
161  return;
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;
170  }
171 
172  /* dummy MPEG audio header */
173  header = 0xff << 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  lsf = !((header & (1 << 20) && header & (1 << 19)));
179 
180  mp3->xing_offset = xing_offtbl[ver != 3][channels == 1] + 4;
181  bytes_needed = mp3->xing_offset + XING_SIZE;
182 
183  for (bitrate_idx = 1; bitrate_idx < 15; bitrate_idx++) {
184  int bit_rate = 1000 * avpriv_mpa_bitrate_tab[lsf][3 - 1][bitrate_idx];
185  int error = FFABS(bit_rate - par->bit_rate);
186 
187  if (error < best_bitrate_error){
188  best_bitrate_error = error;
189  best_bitrate_idx = bitrate_idx;
190  }
191  }
192 
193  for (bitrate_idx = best_bitrate_idx; bitrate_idx < 15; bitrate_idx++) {
194  int32_t mask = bitrate_idx << (4 + 8);
195  header |= mask;
196 
197  avpriv_mpegaudio_decode_header(&mpah, header);
198 
199  if (bytes_needed <= mpah.frame_size)
200  break;
201 
202  header &= ~mask;
203  }
204 
205  ret = avio_open_dyn_buf(&dyn_ctx);
206  if (ret < 0)
207  return;
208 
209  avio_wb32(dyn_ctx, header);
210 
211  avpriv_mpegaudio_decode_header(&mpah, header);
212 
213  av_assert0(mpah.frame_size >= bytes_needed);
214 
215  ffio_fill(dyn_ctx, 0, mp3->xing_offset - 4);
216  ffio_wfourcc(dyn_ctx, "Xing");
217  avio_wb32(dyn_ctx, 0x01 | 0x02 | 0x04 | 0x08); // frames / size / TOC / vbr scale
218 
219  mp3->size = mpah.frame_size;
220  mp3->want = 1;
221 
222  avio_wb32(dyn_ctx, 0); // frames
223  avio_wb32(dyn_ctx, 0); // size
224 
225  // TOC
226  for (i = 0; i < XING_TOC_SIZE; i++)
227  avio_w8(dyn_ctx, 255 * i / XING_TOC_SIZE);
228 
229  // vbr quality
230  // we write it, because some (broken) tools always expect it to be present
231  avio_wb32(dyn_ctx, 0);
232 
233  // encoder short version string
234  if (enc) {
235  uint8_t encoder_str[9] = { 0 };
236  memcpy(encoder_str, enc->value, FFMIN(strlen(enc->value), sizeof(encoder_str)));
237  avio_write(dyn_ctx, encoder_str, sizeof(encoder_str));
238  } else
239  ffio_fill(dyn_ctx, 0, 9);
240 
241  avio_w8(dyn_ctx, 0); // tag revision 0 / unknown vbr method
242  avio_w8(dyn_ctx, 0); // unknown lowpass filter value
243  ffio_fill(dyn_ctx, 0, 8); // empty replaygain fields
244  avio_w8(dyn_ctx, 0); // unknown encoding flags
245  avio_w8(dyn_ctx, 0); // unknown abr/minimal bitrate
246 
247  // encoder delay
248  if (par->initial_padding >= 1 << 12 ||
249  par->trailing_padding >= 1 << 12) {
250  av_log(s, AV_LOG_WARNING, "Too many samples of padding.\n");
251  avio_wb24(dyn_ctx, 0);
252  } else {
253  avio_wb24(dyn_ctx, par->initial_padding << 12 | par->trailing_padding);
254  }
255 
256  avio_w8(dyn_ctx, 0); // misc
257  avio_w8(dyn_ctx, 0); // mp3gain
258  avio_wb16(dyn_ctx, 0); // preset
259 
260  // audio length and CRCs (will be updated later)
261  avio_wb32(dyn_ctx, 0); // music length
262  avio_wb16(dyn_ctx, 0); // music crc
263  avio_wb16(dyn_ctx, 0); // tag crc
264 
265  ffio_fill(dyn_ctx, 0, mpah.frame_size - bytes_needed);
266 
267  mp3->xing_frame_size = avio_close_dyn_buf(dyn_ctx, &mp3->xing_frame);
268  mp3->xing_frame_offset = avio_tell(s->pb);
269  avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
270 
271  mp3->audio_size = mp3->xing_frame_size;
272 }
273 
274 /*
275  * Add a frame to XING data.
276  * Following lame's "VbrTag.c".
277  */
278 static void mp3_xing_add_frame(MP3Context *mp3, AVPacket *pkt)
279 {
280  int i;
281 
282  mp3->frames++;
283  mp3->seen++;
284  mp3->size += pkt->size;
285 
286  if (mp3->want == mp3->seen) {
287  mp3->bag[mp3->pos] = mp3->size;
288 
289  if (XING_NUM_BAGS == ++mp3->pos) {
290  /* shrink table to half size by throwing away each second bag. */
291  for (i = 1; i < XING_NUM_BAGS; i += 2)
292  mp3->bag[i / 2] = mp3->bag[i];
293 
294  /* double wanted amount per bag. */
295  mp3->want *= 2;
296  /* adjust current position to half of table size. */
297  mp3->pos = XING_NUM_BAGS / 2;
298  }
299 
300  mp3->seen = 0;
301  }
302 }
303 
305 {
306  MP3Context *mp3 = s->priv_data;
307 
308  if (mp3->xing_offset && pkt->size >= 4) {
309  MPADecodeHeader c;
310  int ret;
311  uint32_t h;
312 
313  h = AV_RB32(pkt->data);
314  ret = avpriv_mpegaudio_decode_header(&c, h);
315  if (ret >= 0) {
316  if (!mp3->initial_bitrate)
317  mp3->initial_bitrate = c.bit_rate;
318  if ((c.bit_rate == 0) || (mp3->initial_bitrate != c.bit_rate))
319  mp3->has_variable_bitrate = 1;
320  }
321 
322  mp3_xing_add_frame(mp3, pkt);
323 
324  if (mp3->xing_offset) {
325  mp3->audio_size += pkt->size;
327  mp3->audio_crc, pkt->data, pkt->size);
328  }
329  }
330 
331  return ff_raw_write_packet(s, pkt);
332 }
333 
335 {
336  MP3Context *mp3 = s->priv_data;
337  AVPacketList *pktl;
338  int ret = 0, write = 1;
339 
340  ff_id3v2_finish(&mp3->id3, s->pb);
341  mp3_write_xing(s);
342 
343  while ((pktl = mp3->queue)) {
344  if (write && (ret = mp3_write_audio_packet(s, &pktl->pkt)) < 0)
345  write = 0;
346  av_packet_unref(&pktl->pkt);
347  mp3->queue = pktl->next;
348  av_freep(&pktl);
349  }
350  mp3->queue_end = NULL;
351  return ret;
352 }
353 
355 {
356  MP3Context *mp3 = s->priv_data;
357  AVReplayGain *rg;
358  uint16_t tag_crc;
359  uint8_t *toc;
360  int i, rg_size;
361 
362  /* replace "Xing" identification string with "Info" for CBR files. */
363  if (!mp3->has_variable_bitrate)
364  AV_WL32(mp3->xing_frame + mp3->xing_offset, MKTAG('I', 'n', 'f', 'o'));
365 
366  AV_WB32(mp3->xing_frame + mp3->xing_offset + 8, mp3->frames);
367  AV_WB32(mp3->xing_frame + mp3->xing_offset + 12, mp3->size);
368 
369  toc = mp3->xing_frame + mp3->xing_offset + 16;
370  toc[0] = 0; // first toc entry has to be zero.
371  for (i = 1; i < XING_TOC_SIZE; ++i) {
372  int j = i * mp3->pos / XING_TOC_SIZE;
373  int seek_point = 256LL * mp3->bag[j] / mp3->size;
374  toc[i] = FFMIN(seek_point, 255);
375  }
376 
377  /* write replaygain */
379  &rg_size);
380  if (rg && rg_size >= sizeof(*rg)) {
381  uint16_t val;
382 
383  AV_WB32(mp3->xing_frame + mp3->xing_offset + 131,
384  av_rescale(rg->track_peak, 1 << 23, 100000));
385 
386  if (rg->track_gain != INT32_MIN) {
387  val = FFABS(rg->track_gain / 10000) & ((1 << 9) - 1);
388  val |= (rg->track_gain < 0) << 9;
389  val |= 1 << 13;
390  AV_WB16(mp3->xing_frame + mp3->xing_offset + 135, val);
391  }
392 
393  if (rg->album_gain != INT32_MIN) {
394  val = FFABS(rg->album_gain / 10000) & ((1 << 9) - 1);
395  val |= (rg->album_gain < 0) << 9;
396  val |= 1 << 14;
397  AV_WB16(mp3->xing_frame + mp3->xing_offset + 137, val);
398  }
399  }
400 
401  AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size);
402  AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc);
403 
404  tag_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), 0, mp3->xing_frame, 190);
405  AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 2, tag_crc);
406 
407  avio_seek(s->pb, mp3->xing_frame_offset, SEEK_SET);
408  avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
409  avio_seek(s->pb, 0, SEEK_END);
410 }
411 
412 static int mp3_write_trailer(struct AVFormatContext *s)
413 {
414  uint8_t buf[ID3v1_TAG_SIZE];
415  MP3Context *mp3 = s->priv_data;
416 
417  if (mp3->pics_to_write) {
418  av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
419  "attached pictures.\n");
420  mp3_queue_flush(s);
421  }
422 
423  /* write the id3v1 tag */
424  if (mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
425  avio_write(s->pb, buf, ID3v1_TAG_SIZE);
426  }
427 
428  if (mp3->xing_offset)
429  mp3_update_xing(s);
430 
431  av_freep(&mp3->xing_frame);
432 
433  return 0;
434 }
435 
436 static const AVOption options[] = {
437  { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
438  offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 0, 4, AV_OPT_FLAG_ENCODING_PARAM},
439  { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
440  offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
441  { "write_xing", "Write the Xing header containing file duration.",
442  offsetof(MP3Context, write_xing), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
443  { NULL },
444 };
445 
446 static const AVClass mp3_muxer_class = {
447  .class_name = "MP3 muxer",
448  .item_name = av_default_item_name,
449  .option = options,
450  .version = LIBAVUTIL_VERSION_INT,
451 };
452 
454 {
455  MP3Context *mp3 = s->priv_data;
456 
457  if (pkt->stream_index == mp3->audio_stream_idx) {
458  if (mp3->pics_to_write) {
459  /* buffer audio packets until we get all the pictures */
460  AVPacketList *pktl = av_mallocz(sizeof(*pktl));
461  if (!pktl)
462  return AVERROR(ENOMEM);
463 
464  pktl->pkt = *pkt;
465  pktl->pkt.buf = av_buffer_ref(pkt->buf);
466  if (!pktl->pkt.buf) {
467  av_freep(&pktl);
468  return AVERROR(ENOMEM);
469  }
470 
471  if (mp3->queue_end)
472  mp3->queue_end->next = pktl;
473  else
474  mp3->queue = pktl;
475  mp3->queue_end = pktl;
476  } else
477  return mp3_write_audio_packet(s, pkt);
478  } else {
479  int ret;
480 
481  /* warn only once for each stream */
482  if (s->streams[pkt->stream_index]->nb_frames == 1) {
483  av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
484  " ignoring.\n", pkt->stream_index);
485  }
486  if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)
487  return 0;
488 
489  if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)
490  return ret;
491  mp3->pics_to_write--;
492 
493  /* flush the buffered audio packets */
494  if (!mp3->pics_to_write &&
495  (ret = mp3_queue_flush(s)) < 0)
496  return ret;
497  }
498 
499  return 0;
500 }
501 
506 static int mp3_write_header(struct AVFormatContext *s)
507 {
508  MP3Context *mp3 = s->priv_data;
509  int ret, i;
510 
511  if (mp3->id3v2_version &&
512  mp3->id3v2_version != 3 &&
513  mp3->id3v2_version != 4) {
514  av_log(s, AV_LOG_ERROR, "Invalid ID3v2 version requested: %d. Only "
515  "3, 4 or 0 (disabled) are allowed.\n", mp3->id3v2_version);
516  return AVERROR(EINVAL);
517  }
518 
519  /* check the streams -- we want exactly one audio and arbitrary number of
520  * video (attached pictures) */
521  mp3->audio_stream_idx = -1;
522  for (i = 0; i < s->nb_streams; i++) {
523  AVStream *st = s->streams[i];
524  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
525  if (mp3->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_MP3) {
526  av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
527  "audio stream is required.\n");
528  return AVERROR(EINVAL);
529  }
530  mp3->audio_stream_idx = i;
531  } else if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
532  av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
533  return AVERROR(EINVAL);
534  }
535  }
536  if (mp3->audio_stream_idx < 0) {
537  av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
538  return AVERROR(EINVAL);
539  }
540  mp3->pics_to_write = s->nb_streams - 1;
541 
542  if (mp3->pics_to_write && !mp3->id3v2_version) {
543  av_log(s, AV_LOG_ERROR, "Attached pictures were requested, but the "
544  "ID3v2 header is disabled.\n");
545  return AVERROR(EINVAL);
546  }
547 
548  if (mp3->id3v2_version) {
550  ret = ff_id3v2_write_metadata(s, &mp3->id3);
551  if (ret < 0)
552  return ret;
553  }
554 
555  if (!mp3->pics_to_write) {
556  if (mp3->id3v2_version)
557  ff_id3v2_finish(&mp3->id3, s->pb);
558  mp3_write_xing(s);
559  }
560 
561  return 0;
562 }
563 
565  .name = "mp3",
566  .long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
567  .mime_type = "audio/mpeg",
568  .extensions = "mp3",
569  .priv_data_size = sizeof(MP3Context),
570  .audio_codec = AV_CODEC_ID_MP3,
571  .video_codec = AV_CODEC_ID_PNG,
576  .priv_class = &mp3_muxer_class,
577 };
#define MPA_STEREO
Definition: mpegaudio.h:45
Bytestream IO Context.
Definition: avio.h:104
#define XING_NUM_BAGS
Definition: mp3enc.c:80
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:312
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1155
AVOption.
Definition: opt.h:234
void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version, const char *magic)
Initialize an ID3v2 tag.
Definition: id3v2enc.c:113
static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mp3enc.c:304
int64_t xing_frame_offset
Definition: mp3enc.c:101
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
uint32_t pos
Definition: mp3enc.c:109
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int size
Definition: avcodec.h:1347
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
uint8_t * av_stream_get_side_data(AVStream *stream, enum AVPacketSideDataType type, int *size)
Get side information from stream.
Definition: utils.c:3265
#define FF_ARRAY_ELEMS(a)
int trailing_padding
Audio only.
Definition: avcodec.h:3586
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1143
mpeg audio layer common tables.
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3475
uint64_t bag[XING_NUM_BAGS]
Definition: mp3enc.c:110
uint32_t track_peak
Peak track amplitude, with 100000 representing full scale (but values may overflow).
Definition: replaygain.h:39
#define AV_WB32(p, val)
Definition: intreadwrite.h:246
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
Format I/O context.
Definition: avformat.h:940
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:39
#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:354
int32_t size
Definition: mp3enc.c:106
static int mp3_write_trailer(struct AVFormatContext *s)
Definition: mp3enc.c:412
uint8_t
AVOptions.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:298
AVPacket pkt
Definition: avformat.h:1298
#define AV_RB32
Definition: intreadwrite.h:130
const uint16_t avpriv_mpa_freq_tab[3]
Definition: mpegaudiodata.c:40
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
int initial_padding
Audio only.
Definition: avcodec.h:3579
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:38
uint8_t * data
Definition: avcodec.h:1346
int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
static int flags
Definition: log.c:50
uint32_t tag
Definition: movenc.c:854
static const uint8_t xing_offtbl[2][2]
Definition: mp3enc.c:123
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:221
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:66
AVCRC audio_crc
Definition: mp3enc.c:97
int32_t album_gain
Same as track_gain, but for the whole album.
Definition: replaygain.h:43
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:264
AVPacketList * queue_end
Definition: mp3enc.c:120
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
static const uint16_t mask[17]
Definition: lzw.c:38
#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:148
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:479
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1329
simple assert() macros that are a bit more flexible than ISO C assert().
uint32_t want
Definition: mp3enc.c:107
void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb)
Finalize an opened ID3v2 tag.
Definition: id3v2enc.c:224
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:81
int bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3512
int initial_bitrate
Definition: mp3enc.c:111
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:153
int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
Convert and write all global metadata from s into an ID3v2 tag.
Definition: id3v2enc.c:127
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:86
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:207
int id3v2_version
Definition: mp3enc.c:88
#define FFMIN(a, b)
Definition: common.h:66
int av_strcasecmp(const char *a, const char *b)
Definition: avstring.c:156
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:416
const char * name
Definition: avformat.h:450
int32_t
AVPacketList * queue
Definition: mp3enc.c:120
#define FFABS(a)
Definition: common.h:61
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:26
AVDictionary * metadata
Definition: avformat.h:772
int write_id3v1
Definition: mp3enc.c:89
static void mp3_write_xing(AVFormatContext *s)
Definition: mp3enc.c:128
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
Stream structure.
Definition: avformat.h:705
int pics_to_write
Definition: mp3enc.c:117
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:420
static const AVClass mp3_muxer_class
Definition: mp3enc.c:446
NULL
Definition: eval.c:55
int has_variable_bitrate
Definition: mp3enc.c:112
int audio_stream_idx
Definition: mp3enc.c:115
int xing_offset
Definition: mp3enc.c:103
AVIOContext * pb
I/O context.
Definition: avformat.h:982
int32_t frames
Definition: mp3enc.c:105
av_default_item_name
Definition: dnxhdenc.c:55
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:200
static const AVOption options[]
Definition: mp3enc.c:436
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: avconv.c:278
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
int xing_frame_size
Definition: mp3enc.c:95
Describe the class of an AVClass context structure.
Definition: log.h:34
uint8_t * xing_frame
Definition: mp3enc.c:94
#define MPA_MONO
Definition: mpegaudio.h:48
#define XING_SIZE
Definition: mp3enc.c:83
uint32_t audio_size
Definition: mp3enc.c:98
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:404
This side data should be associated with an audio stream and contains ReplayGain information in form ...
Definition: avcodec.h:1245
static int id3v1_set_string(AVFormatContext *s, const char *key, uint8_t *buf, int buf_size)
Definition: mp3enc.c:39
int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
Write an attached picture from pkt into an ID3v2 tag.
Definition: id3v2enc.c:159
int write_xing
Definition: mp3enc.c:90
int sample_rate
Audio only.
Definition: avcodec.h:3564
MPEG Audio header decoder.
static int mp3_queue_flush(AVFormatContext *s)
Definition: mp3enc.c:334
Main libavformat public API header.
struct AVPacketList * next
Definition: avformat.h:1299
AVOutputFormat ff_mp3_muxer
Definition: mp3enc.c:564
static int mp3_write_header(struct AVFormatContext *s)
Write an ID3v2 header at beginning of stream.
Definition: mp3enc.c:506
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
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:759
uint32_t AVCRC
Definition: crc.h:28
#define XING_TOC_SIZE
Definition: mp3enc.c:81
char * value
Definition: dict.h:74
void * priv_data
Format private data.
Definition: avformat.h:968
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:373
int channels
Audio only.
Definition: avcodec.h:3560
#define ID3v1_TAG_SIZE
Definition: id3v1.h:27
#define AV_WL32(p, val)
Definition: intreadwrite.h:263
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:324
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:831
int stream_index
Definition: avcodec.h:1348
static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
Definition: mp3enc.c:48
#define AV_WB16(p, val)
Definition: intreadwrite.h:218
#define MKTAG(a, b, c, d)
Definition: common.h:256
ReplayGain information (see http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1.0_specification).
Definition: replaygain.h:29
static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mp3enc.c:453
#define ID3v1_GENRE_MAX
Definition: id3v1.h:29
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition: id3v1.c:26
This structure stores compressed data.
Definition: avcodec.h:1323
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:211
ID3v2EncContext id3
Definition: mp3enc.c:87
static void mp3_xing_add_frame(MP3Context *mp3, AVPacket *pkt)
Definition: mp3enc.c:278
uint32_t seen
Definition: mp3enc.c:108