+/**
+ * Encodes and outputs the entire frame in the JPEG format.
+ *
+ * @param s The MpegEncContext.
+ */
+void ff_mjpeg_encode_picture_frame(MpegEncContext *s)
+{
+ int i, nbits, code, table_id;
+ MJpegContext *m = s->mjpeg_ctx;
+ uint8_t *huff_size[4] = {m->huff_size_dc_luminance,
+ m->huff_size_dc_chrominance,
+ m->huff_size_ac_luminance,
+ m->huff_size_ac_chrominance};
+ uint16_t *huff_code[4] = {m->huff_code_dc_luminance,
+ m->huff_code_dc_chrominance,
+ m->huff_code_ac_luminance,
+ m->huff_code_ac_chrominance};
+ size_t total_bits = 0;
+ size_t bytes_needed;
+
+ s->header_bits = get_bits_diff(s);
+ // Estimate the total size first
+ for (i = 0; i < m->huff_ncode; i++) {
+ table_id = m->huff_buffer[i].table_id;
+ code = m->huff_buffer[i].code;
+ nbits = code & 0xf;
+
+ total_bits += huff_size[table_id][code] + nbits;
+ }
+
+ bytes_needed = (total_bits + 7) / 8;
+ ff_mpv_reallocate_putbitbuffer(s, bytes_needed, bytes_needed);
+
+ for (i = 0; i < m->huff_ncode; i++) {
+ table_id = m->huff_buffer[i].table_id;
+ code = m->huff_buffer[i].code;
+ nbits = code & 0xf;
+
+ put_bits(&s->pb, huff_size[table_id][code], huff_code[table_id][code]);
+ if (nbits != 0) {
+ put_sbits(&s->pb, nbits, m->huff_buffer[i].mant);
+ }
+ }
+
+ m->huff_ncode = 0;
+ s->i_tex_bits = get_bits_diff(s);
+}
+