+ // TOC
+ for (i = 0; i < XING_TOC_SIZE; i++)
+ avio_w8(dyn_ctx, 255 * i / XING_TOC_SIZE);
+
+ // vbr quality
+ // we write it, because some (broken) tools always expect it to be present
+ avio_wb32(dyn_ctx, 0);
+
+ // encoder short version string
+ if (enc) {
+ uint8_t encoder_str[9] = { 0 };
+ memcpy(encoder_str, enc->value, FFMIN(strlen(enc->value), sizeof(encoder_str)));
+ avio_write(dyn_ctx, encoder_str, sizeof(encoder_str));
+ } else
+ ffio_fill(dyn_ctx, 0, 9);
+
+ avio_w8(dyn_ctx, 0); // tag revision 0 / unknown vbr method
+ avio_w8(dyn_ctx, 0); // unknown lowpass filter value
+ ffio_fill(dyn_ctx, 0, 8); // empty replaygain fields
+ avio_w8(dyn_ctx, 0); // unknown encoding flags
+ avio_w8(dyn_ctx, 0); // unknown abr/minimal bitrate
+
+ // encoder delay
+ if (codec->initial_padding >= 1 << 12) {
+ av_log(s, AV_LOG_WARNING, "Too many samples of initial padding.\n");
+ avio_wb24(dyn_ctx, 0);
+ } else {
+ avio_wb24(dyn_ctx, codec->initial_padding << 12);
+ }
+
+ avio_w8(dyn_ctx, 0); // misc
+ avio_w8(dyn_ctx, 0); // mp3gain
+ avio_wb16(dyn_ctx, 0); // preset
+
+ // audio length and CRCs (will be updated later)
+ avio_wb32(dyn_ctx, 0); // music length
+ avio_wb16(dyn_ctx, 0); // music crc
+ avio_wb16(dyn_ctx, 0); // tag crc
+
+ ffio_fill(dyn_ctx, 0, mpah.frame_size - bytes_needed);
+
+ mp3->xing_frame_size = avio_close_dyn_buf(dyn_ctx, &mp3->xing_frame);
+ mp3->xing_frame_offset = avio_tell(s->pb);
+ avio_write(s->pb, mp3->xing_frame, mp3->xing_frame_size);
+
+ mp3->audio_size = mp3->xing_frame_size;
+}
+
+/*
+ * Add a frame to XING data.
+ * Following lame's "VbrTag.c".
+ */
+static void mp3_xing_add_frame(MP3Context *mp3, AVPacket *pkt)
+{
+ int i;
+
+ mp3->frames++;
+ mp3->seen++;
+ mp3->size += pkt->size;
+
+ if (mp3->want == mp3->seen) {
+ mp3->bag[mp3->pos] = mp3->size;
+
+ if (XING_NUM_BAGS == ++mp3->pos) {
+ /* shrink table to half size by throwing away each second bag. */
+ for (i = 1; i < XING_NUM_BAGS; i += 2)
+ mp3->bag[i / 2] = mp3->bag[i];
+
+ /* double wanted amount per bag. */
+ mp3->want *= 2;
+ /* adjust current position to half of table size. */
+ mp3->pos = XING_NUM_BAGS / 2;
+ }
+
+ mp3->seen = 0;
+ }
+}
+
+static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt)
+{
+ MP3Context *mp3 = s->priv_data;
+
+ if (mp3->xing_offset && pkt->size >= 4) {
+ MPADecodeHeader c;
+ uint32_t h;
+
+ h = AV_RB32(pkt->data);
+ if (ff_mpa_check_header(h) == 0) {
+ avpriv_mpegaudio_decode_header(&c, h);
+ if (!mp3->initial_bitrate)
+ mp3->initial_bitrate = c.bit_rate;
+ if ((c.bit_rate == 0) || (mp3->initial_bitrate != c.bit_rate))
+ mp3->has_variable_bitrate = 1;
+ }
+
+ mp3_xing_add_frame(mp3, pkt);
+
+ if (mp3->xing_offset) {
+ mp3->audio_size += pkt->size;
+ mp3->audio_crc = av_crc(av_crc_get_table(AV_CRC_16_ANSI_LE),
+ mp3->audio_crc, pkt->data, pkt->size);
+ }
+ }
+
+ return ff_raw_write_packet(s, pkt);