FFmpeg
Loading...
Searching...
No Matches
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"
33#include "packet_internal.h"
35#include "libavutil/opt.h"
36#include "libavutil/dict.h"
37#include "libavutil/avassert.h"
38#include "libavutil/crc.h"
41
42static 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
52static 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
100typedef 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;
128 int delay;
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
140static 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;
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 }
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 uint32_t h;
326
327 h = AV_RB32(pkt->data);
328 ret = avpriv_mpegaudio_decode_header(&mpah, h);
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 int 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
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 uint32_t discard_padding = AV_RL32(side_data + 4);
371 /* Padding longer than a frame is spread over several packets. */
372 mp3->padding = discard_padding ? mp3->padding + discard_padding : 0;
373 if (!mp3->delay)
374 mp3->delay = FFMAX((int64_t)AV_RL32(side_data) - 528 - 1, 0);
375 } else {
376 mp3->padding = 0;
377 }
378 }
379 }
380
381 return ff_raw_write_packet(s, pkt);
382}
383
385{
386 MP3Context *mp3 = s->priv_data;
387 AVPacket *const pkt = ffformatcontext(s)->pkt;
388 int ret = 0, write = 1;
389
390 ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
392
393 while (mp3->queue.head) {
395 if (write && (ret = mp3_write_audio_packet(s, pkt)) < 0)
396 write = 0;
398 }
399 return ret;
400}
401
403{
404 MP3Context *mp3 = s->priv_data;
405 const AVPacketSideData *sd;
406 AVReplayGain *rg;
407 uint16_t tag_crc;
408 uint8_t *toc;
409 int i;
410 int64_t old_pos = avio_tell(s->pb);
411
412 /* replace "Xing" identification string with "Info" for CBR files. */
413 if (!mp3->has_variable_bitrate)
414 AV_WL32(mp3->xing_frame + mp3->xing_offset, MKTAG('I', 'n', 'f', 'o'));
415
416 AV_WB32(mp3->xing_frame + mp3->xing_offset + 8, mp3->frames);
417 AV_WB32(mp3->xing_frame + mp3->xing_offset + 12, mp3->size);
418
419 toc = mp3->xing_frame + mp3->xing_offset + 16;
420 toc[0] = 0; // first toc entry has to be zero.
421 for (i = 1; i < XING_TOC_SIZE; ++i) {
422 int j = i * mp3->pos / XING_TOC_SIZE;
423 int seek_point = 256LL * mp3->bag[j] / mp3->size;
424 toc[i] = FFMIN(seek_point, 255);
425 }
426
427 /* write replaygain */
428 sd = av_packet_side_data_get(s->streams[0]->codecpar->coded_side_data,
429 s->streams[0]->codecpar->nb_coded_side_data,
431 if (sd && sd->size >= sizeof(*rg)) {
432 uint16_t val;
433
434 rg = (AVReplayGain *)sd->data;
435 AV_WB32(mp3->xing_frame + mp3->xing_offset + 131,
436 av_rescale(rg->track_peak, 1 << 23, 100000));
437
438 if (rg->track_gain != INT32_MIN) {
439 val = FFABS(rg->track_gain / 10000) & ((1 << 9) - 1);
440 val |= (rg->track_gain < 0) << 9;
441 val |= 1 << 13;
442 AV_WB16(mp3->xing_frame + mp3->xing_offset + 135, val);
443 }
444
445 if (rg->album_gain != INT32_MIN) {
446 val = FFABS(rg->album_gain / 10000) & ((1 << 9) - 1);
447 val |= (rg->album_gain < 0) << 9;
448 val |= 1 << 14;
449 AV_WB16(mp3->xing_frame + mp3->xing_offset + 137, val);
450 }
451 }
452
453 /* write encoder delay/padding */
454 if (mp3->padding)
455 mp3->padding += 528 + 1;
456 if (mp3->delay >= 1 << 12) {
457 mp3->delay = (1 << 12) - 1;
458 av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n");
459 }
460 if (mp3->padding >= 1 << 12) {
461 mp3->padding = (1 << 12) - 1;
462 av_log(s, AV_LOG_WARNING, "Too many samples of trailing padding.\n");
463 }
464 AV_WB24(mp3->xing_frame + mp3->xing_offset + 141, (mp3->delay << 12) + mp3->padding);
465
466 AV_WB32(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 8, mp3->audio_size);
467 AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 4, mp3->audio_crc);
468
469 tag_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE), 0, mp3->xing_frame, 190);
470 AV_WB16(mp3->xing_frame + mp3->xing_offset + XING_SIZE - 2, tag_crc);
471
472 avio_seek(s->pb, mp3->xing_frame_offset, SEEK_SET);
473 avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
474 avio_seek(s->pb, old_pos, SEEK_SET);
475}
476
478{
479 uint8_t buf[ID3v1_TAG_SIZE];
480 MP3Context *mp3 = s->priv_data;
481
482 if (mp3->pics_to_write) {
483 av_log(s, AV_LOG_WARNING, "No packets were sent for some of the "
484 "attached pictures.\n");
486 }
487
488 /* write the id3v1 tag */
489 if (mp3->write_id3v1 && id3v1_create_tag(s, buf) > 0) {
490 avio_write(s->pb, buf, ID3v1_TAG_SIZE);
491 }
492
493 if (mp3->xing_offset)
495
496 return 0;
497}
498
499static int query_codec(enum AVCodecID id, int std_compliance)
500{
502
503 if (id == AV_CODEC_ID_MP3)
504 return 1;
505
506 while(cm->id != AV_CODEC_ID_NONE) {
507 if(id == cm->id)
508 return MKTAG('A', 'P', 'I', 'C');
509 cm++;
510 }
511 return 0;
512}
513
514static const AVOption options[] = {
515 { "id3v2_version", "Select ID3v2 version to write. Currently 3 and 4 are supported.",
516 offsetof(MP3Context, id3v2_version), AV_OPT_TYPE_INT, {.i64 = 4}, 0, 4, AV_OPT_FLAG_ENCODING_PARAM},
517 { "write_id3v1", "Enable ID3v1 writing. ID3v1 tags are written in UTF-8 which may not be supported by most software.",
518 offsetof(MP3Context, write_id3v1), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
519 { "write_xing", "Write the Xing header containing file duration.",
520 offsetof(MP3Context, write_xing), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
521 { NULL },
522};
523
524static const AVClass mp3_muxer_class = {
525 .class_name = "MP3 muxer",
526 .item_name = av_default_item_name,
527 .option = options,
528 .version = LIBAVUTIL_VERSION_INT,
529};
530
532{
533 MP3Context *mp3 = s->priv_data;
534
535 if (pkt->stream_index == mp3->audio_stream_idx) {
536 if (mp3->pics_to_write) {
537 /* buffer audio packets until we get all the pictures */
538 int ret = ff_packet_list_put(&mp3->queue, pkt, NULL, 0);
539
540 if (ret < 0) {
541 av_log(s, AV_LOG_WARNING, "Not enough memory to buffer audio. Skipping picture streams\n");
542 mp3->pics_to_write = 0;
545 }
546 } else
548 } else {
549 int ret;
550
551 /* warn only once for each stream */
552 if (s->streams[pkt->stream_index]->nb_frames == 1) {
553 av_log(s, AV_LOG_WARNING, "Got more than one picture in stream %d,"
554 " ignoring.\n", pkt->stream_index);
555 }
556 if (!mp3->pics_to_write || s->streams[pkt->stream_index]->nb_frames >= 1)
557 return 0;
558
559 if ((ret = ff_id3v2_write_apic(s, &mp3->id3, pkt)) < 0)
560 return ret;
561 mp3->pics_to_write--;
562
563 /* flush the buffered audio packets */
564 if (!mp3->pics_to_write &&
565 (ret = mp3_queue_flush(s)) < 0)
566 return ret;
567 }
568
569 return 0;
570}
571
572/**
573 * Write an ID3v2 header at beginning of stream
574 */
575
576static int mp3_init(struct AVFormatContext *s)
577{
578 MP3Context *mp3 = s->priv_data;
579 int i;
580
581 if (mp3->id3v2_version &&
582 mp3->id3v2_version != 3 &&
583 mp3->id3v2_version != 4) {
584 av_log(s, AV_LOG_ERROR, "Invalid ID3v2 version requested: %d. Only "
585 "3, 4 or 0 (disabled) are allowed.\n", mp3->id3v2_version);
586 return AVERROR(EINVAL);
587 }
588
589 /* check the streams -- we want exactly one audio and arbitrary number of
590 * video (attached pictures) */
591 mp3->audio_stream_idx = -1;
592 for (i = 0; i < s->nb_streams; i++) {
593 AVStream *st = s->streams[i];
595 if (mp3->audio_stream_idx >= 0 || st->codecpar->codec_id != AV_CODEC_ID_MP3) {
596 av_log(s, AV_LOG_ERROR, "Invalid audio stream. Exactly one MP3 "
597 "audio stream is required.\n");
598 return AVERROR(EINVAL);
599 }
600 mp3->audio_stream_idx = i;
601 } else if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
602 av_log(s, AV_LOG_ERROR, "Only audio streams and pictures are allowed in MP3.\n");
603 return AVERROR(EINVAL);
604 }
605 }
606 if (mp3->audio_stream_idx < 0) {
607 av_log(s, AV_LOG_ERROR, "No audio stream present.\n");
608 return AVERROR(EINVAL);
609 }
610 mp3->pics_to_write = s->nb_streams - 1;
611
612 if (mp3->pics_to_write && !mp3->id3v2_version) {
613 av_log(s, AV_LOG_ERROR, "Attached pictures were requested, but the "
614 "ID3v2 header is disabled.\n");
615 return AVERROR(EINVAL);
616 }
617
618 return 0;
619}
620
622{
623 MP3Context *mp3 = s->priv_data;
624 int ret;
625
626 if (mp3->id3v2_version) {
628 ret = ff_id3v2_write_metadata(s, &mp3->id3);
629 if (ret < 0)
630 return ret;
631 }
632
633 if (!mp3->pics_to_write) {
634 if (mp3->id3v2_version)
635 ff_id3v2_finish(&mp3->id3, s->pb, s->metadata_header_padding);
637 }
638
639 return 0;
640}
641
642static void mp3_deinit(struct AVFormatContext *s)
643{
644 MP3Context *mp3 = s->priv_data;
645
647 av_freep(&mp3->xing_frame);
648}
649
651 .p.name = "mp3",
652 .p.long_name = NULL_IF_CONFIG_SMALL("MP3 (MPEG audio layer 3)"),
653 .p.mime_type = "audio/mpeg",
654 .p.extensions = "mp3",
655 .priv_data_size = sizeof(MP3Context),
656 .p.audio_codec = AV_CODEC_ID_MP3,
657 .p.video_codec = AV_CODEC_ID_PNG,
658 .init = mp3_init,
659 .write_header = mp3_write_header,
660 .write_packet = mp3_write_packet,
661 .write_trailer = mp3_write_trailer,
662 .deinit = mp3_deinit,
663 .query_codec = query_codec,
664 .p.flags = AVFMT_NOTIMESTAMPS,
665 .p.priv_class = &mp3_muxer_class,
666};
static double val(void *priv, double ch)
Definition aeval.c:77
const FFOutputFormat ff_mp3_muxer
Definition mp3enc.c:650
channels
Definition aptx.h:31
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Main libavformat public API header.
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
void avio_wb32(AVIOContext *s, unsigned int val)
Definition aviobuf.c:368
void avio_wb16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:446
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
void avio_wb24(AVIOContext *s, unsigned int val)
Definition aviobuf.c:458
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition aviobuf.c:1410
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition aviobuf.c:1365
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition aviobuf.c:192
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
static AVPacket * pkt
Public dictionary API.
#define cm
Definition dvbsubdec.c:40
const char * key
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
Definition opt.h:351
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_PNG
Definition codec_id.h:111
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:454
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:570
@ AV_PKT_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition packet.h:153
@ AV_PKT_DATA_REPLAYGAIN
This side data should be associated with an audio stream and contains ReplayGain information in form ...
Definition packet.h:96
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
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
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t AVCRC
Definition crc.h:46
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:421
@ AV_CRC_16_ANSI_LE
Definition crc.h:54
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
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
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
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
const char *const ff_id3v1_genre_str[ID3v1_GENRE_MAX+1]
ID3v1 genres.
Definition id3v1.c:26
#define ID3v1_TAG_SIZE
Definition id3v1.h:27
#define ID3v1_GENRE_MAX
Definition id3v1.h:29
const CodecMime ff_id3v2_mime_tags[]
Definition id3v2.c:138
void ff_id3v2_start(ID3v2EncContext *id3, AVIOContext *pb, int id3v2_version, const char *magic)
Initialize an ID3v2 tag.
Definition id3v2enc.c:349
void ff_id3v2_finish(ID3v2EncContext *id3, AVIOContext *pb, int padding_bytes)
Finalize an opened ID3v2 tag.
Definition id3v2enc.c:552
int ff_id3v2_write_metadata(AVFormatContext *s, ID3v2EncContext *id3)
Convert and write all global metadata from s into an ID3v2 tag.
Definition id3v2enc.c:474
int ff_id3v2_write_apic(AVFormatContext *s, ID3v2EncContext *id3, AVPacket *pkt)
Write an attached picture from pkt into an ID3v2 tag.
Definition id3v2enc.c:495
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition id3v2.h:35
static int query_codec(enum AVCodecID id, int std_compliance)
Definition img2enc.c:361
#define AV_WB24(p, d)
#define AV_WB32(p, v)
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_RB32(p)
#define AV_WB16(p, v)
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition internal.h:130
int ff_packet_list_get(PacketList *list, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
Definition packet_list.c:89
void ff_packet_list_free(PacketList *list)
Wipe the list and unref all the packets in it.
int ff_packet_list_put(PacketList *list, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Definition packet_list.c:40
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition rawenc.c:31
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define MKBETAG(a, b, c, d)
Definition macros.h:56
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
uint32_t tag
Definition movenc.c:2073
static void mp3_update_xing(AVFormatContext *s)
Definition mp3enc.c:402
static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt)
Definition mp3enc.c:318
static int mp3_queue_flush(AVFormatContext *s)
Definition mp3enc.c:384
static const AVClass mp3_muxer_class
Definition mp3enc.c:524
static void mp3_xing_add_frame(MP3Context *mp3, AVPacket *pkt)
Definition mp3enc.c:292
#define XING_NUM_BAGS
Definition mp3enc.c:95
static const uint8_t xing_offtbl[2][2]
Definition mp3enc.c:140
static int mp3_init(struct AVFormatContext *s)
Write an ID3v2 header at beginning of stream.
Definition mp3enc.c:576
static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
Definition mp3enc.c:52
static int query_codec(enum AVCodecID id, int std_compliance)
Definition mp3enc.c:499
static int mp3_write_xing(AVFormatContext *s)
Definition mp3enc.c:145
static void mp3_deinit(struct AVFormatContext *s)
Definition mp3enc.c:642
static int mp3_write_header(struct AVFormatContext *s)
Definition mp3enc.c:621
static int id3v1_set_string(AVFormatContext *s, const char *key, uint8_t *buf, int buf_size)
Definition mp3enc.c:42
#define XING_SIZE
Definition mp3enc.c:98
static int mp3_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition mp3enc.c:531
#define XING_TOC_SIZE
Definition mp3enc.c:96
static int mp3_write_trailer(struct AVFormatContext *s)
Definition mp3enc.c:477
mpeg audio declarations for both encoder and decoder.
#define MPA_MONO
Definition mpegaudio.h:49
#define MPA_STEREO
Definition mpegaudio.h:46
mpeg audio layer common tables.
FF_VISIBILITY_PUSH_HIDDEN const uint16_t ff_mpa_bitrate_tab[2][3][15]
const uint16_t ff_mpa_freq_tab[3]
int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
MPEG Audio header decoder.
AVOptions.
static const uint8_t header[24]
Definition sdr2.c:68
#define FF_ARRAY_ELEMS(a)
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
AVOption.
Definition opt.h:428
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
Definition packet.h:424
uint8_t * data
Definition packet.h:425
This structure stores compressed data.
Definition packet.h:580
ReplayGain information (see http://wiki.hydrogenaudio.org/index.php?title=ReplayGain_1....
Definition replaygain.h:29
uint32_t track_peak
Peak track amplitude, with 100000 representing full scale (but values may overflow).
Definition replaygain.h:39
int32_t track_gain
Track replay gain in microbels (divide by 100000 to get the value in dB).
Definition replaygain.h:34
int32_t album_gain
Same as track_gain, but for the whole album.
Definition replaygain.h:43
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition internal.h:111
ID3v2EncContext id3
Definition mp3enc.c:102
uint32_t want
Definition mp3enc.c:122
int pics_to_write
Definition mp3enc.c:134
uint64_t bag[XING_NUM_BAGS]
Definition mp3enc.c:125
int xing_frame_size
Definition mp3enc.c:110
int initial_bitrate
Definition mp3enc.c:126
uint32_t audio_size
Definition mp3enc.c:113
int64_t padding
Definition mp3enc.c:129
AVCRC audio_crc
Definition mp3enc.c:112
int id3v2_version
Definition mp3enc.c:103
int audio_stream_idx
Definition mp3enc.c:132
PacketList queue
Definition mp3enc.c:137
int write_xing
Definition mp3enc.c:105
int xing_offset
Definition mp3enc.c:118
int32_t frames
Definition mp3enc.c:120
uint8_t * xing_frame
Definition mp3enc.c:109
int write_id3v1
Definition mp3enc.c:104
int64_t xing_frame_offset
Definition mp3enc.c:116
int has_variable_bitrate
Definition mp3enc.c:127
int32_t size
Definition mp3enc.c:121
uint32_t pos
Definition mp3enc.c:124
int delay
Definition mp3enc.c:128
uint32_t seen
Definition mp3enc.c:123
PacketListEntry * head
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
uint8_t base
Definition vp3data.h:128