avcodec/mjpegenc_common: Move stuff only used by mjpegenc.c to it

This allows to make ff_init_uni_ac_vlc static;
ff_mjpeg_encode_picture_frame has also been made static, but it could
always have been made static.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
This commit is contained in:
Andreas Rheinhardt 2021-02-17 23:16:58 +01:00
parent 6eae9c1d39
commit bab6b88ebf
5 changed files with 192 additions and 201 deletions

View File

@ -32,12 +32,14 @@
#include "libavutil/frame.h" #include "libavutil/frame.h"
#include "libavutil/mem.h" #include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h" #include "libavutil/pixdesc.h"
#include "avcodec.h" #include "avcodec.h"
#include "idctdsp.h" #include "idctdsp.h"
#include "internal.h" #include "internal.h"
#include "jpegtables.h" #include "jpegtables.h"
#include "mathops.h"
#include "mjpegenc_common.h" #include "mjpegenc_common.h"
#include "mjpeg.h" #include "mjpeg.h"

View File

@ -35,11 +35,198 @@
#include "avcodec.h" #include "avcodec.h"
#include "jpegtables.h" #include "jpegtables.h"
#include "mjpegenc_common.h" #include "mjpegenc_common.h"
#include "mjpegenc_huffman.h"
#include "mpegvideo.h" #include "mpegvideo.h"
#include "mjpeg.h" #include "mjpeg.h"
#include "mjpegenc.h" #include "mjpegenc.h"
#include "profiles.h" #include "profiles.h"
static av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256],
uint8_t *uni_ac_vlc_len)
{
for (int i = 0; i < 128; i++) {
int level = i - 64;
if (!level)
continue;
for (int run = 0; run < 64; run++) {
int len, code, nbits;
int alevel = FFABS(level);
len = (run >> 4) * huff_size_ac[0xf0];
nbits= av_log2_16bit(alevel) + 1;
code = ((15&run) << 4) | nbits;
len += huff_size_ac[code] + nbits;
uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
// We ignore EOB as its just a constant which does not change generally
}
}
}
/**
* Encodes and outputs the entire frame in the JPEG format.
*
* @param s The MpegEncContext.
*/
static void mjpeg_encode_picture_frame(MpegEncContext *s)
{
int 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 (int 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 (int 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);
}
/**
* Builds all 4 optimal Huffman tables.
*
* Uses the data stored in the JPEG buffer to compute the tables.
* Stores the Huffman tables in the bits_* and val_* arrays in the MJpegContext.
*
* @param m MJpegContext containing the JPEG buffer.
*/
static void mjpeg_build_optimal_huffman(MJpegContext *m)
{
MJpegEncHuffmanContext dc_luminance_ctx;
MJpegEncHuffmanContext dc_chrominance_ctx;
MJpegEncHuffmanContext ac_luminance_ctx;
MJpegEncHuffmanContext ac_chrominance_ctx;
MJpegEncHuffmanContext *ctx[4] = { &dc_luminance_ctx,
&dc_chrominance_ctx,
&ac_luminance_ctx,
&ac_chrominance_ctx };
for (int i = 0; i < 4; i++)
ff_mjpeg_encode_huffman_init(ctx[i]);
for (int i = 0; i < m->huff_ncode; i++) {
int table_id = m->huff_buffer[i].table_id;
int code = m->huff_buffer[i].code;
ff_mjpeg_encode_huffman_increment(ctx[table_id], code);
}
ff_mjpeg_encode_huffman_close(&dc_luminance_ctx,
m->bits_dc_luminance,
m->val_dc_luminance, 12);
ff_mjpeg_encode_huffman_close(&dc_chrominance_ctx,
m->bits_dc_chrominance,
m->val_dc_chrominance, 12);
ff_mjpeg_encode_huffman_close(&ac_luminance_ctx,
m->bits_ac_luminance,
m->val_ac_luminance, 256);
ff_mjpeg_encode_huffman_close(&ac_chrominance_ctx,
m->bits_ac_chrominance,
m->val_ac_chrominance, 256);
ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
m->huff_code_dc_luminance,
m->bits_dc_luminance,
m->val_dc_luminance);
ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
m->huff_code_dc_chrominance,
m->bits_dc_chrominance,
m->val_dc_chrominance);
ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
m->huff_code_ac_luminance,
m->bits_ac_luminance,
m->val_ac_luminance);
ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
m->huff_code_ac_chrominance,
m->bits_ac_chrominance,
m->val_ac_chrominance);
}
/**
* Writes the complete JPEG frame when optimal huffman tables are enabled,
* otherwise writes the stuffing.
*
* Header + values + stuffing.
*
* @param s The MpegEncContext.
* @return int Error code, 0 if successful.
*/
int ff_mjpeg_encode_stuffing(MpegEncContext *s)
{
PutBitContext *pbc = &s->pb;
int mb_y = s->mb_y - !s->mb_x;
int ret;
MJpegContext *m;
m = s->mjpeg_ctx;
if (s->huffman == HUFFMAN_TABLE_OPTIMAL) {
mjpeg_build_optimal_huffman(m);
// Replace the VLCs with the optimal ones.
// The default ones may be used for trellis during quantization.
init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
s->intra_ac_vlc_length =
s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
s->intra_chroma_ac_vlc_length =
s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
s->pred, s->intra_matrix, s->chroma_intra_matrix);
mjpeg_encode_picture_frame(s);
}
ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
put_bits_count(&s->pb) / 4 + 1000);
if (ret < 0) {
av_log(s->avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
goto fail;
}
ff_mjpeg_escape_FF(pbc, s->esc_pos);
if ((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height - 1)
put_marker(pbc, RST0 + (mb_y&7));
s->esc_pos = put_bits_count(pbc) >> 3;
fail:
for (int i = 0; i < 3; i++)
s->last_dc[i] = 128 << s->intra_dc_precision;
return ret;
}
static int alloc_huffman(MpegEncContext *s) static int alloc_huffman(MpegEncContext *s)
{ {
MJpegContext *m = s->mjpeg_ctx; MJpegContext *m = s->mjpeg_ctx;
@ -106,8 +293,8 @@ av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
avpriv_mjpeg_bits_ac_chrominance, avpriv_mjpeg_bits_ac_chrominance,
avpriv_mjpeg_val_ac_chrominance); avpriv_mjpeg_val_ac_chrominance);
ff_init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len); init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
ff_init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len); init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
s->intra_ac_vlc_length = s->intra_ac_vlc_length =
s->intra_ac_vlc_last_length = m->uni_ac_vlc_len; s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
s->intra_chroma_ac_vlc_length = s->intra_chroma_ac_vlc_length =

View File

@ -107,5 +107,6 @@ static inline void put_marker(PutBitContext *p, enum JpegMarker code)
int ff_mjpeg_encode_init(MpegEncContext *s); int ff_mjpeg_encode_init(MpegEncContext *s);
void ff_mjpeg_encode_close(MpegEncContext *s); void ff_mjpeg_encode_close(MpegEncContext *s);
void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64]); void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64]);
int ff_mjpeg_encode_stuffing(MpegEncContext *s);
#endif /* AVCODEC_MJPEGENC_H */ #endif /* AVCODEC_MJPEGENC_H */

View File

@ -23,7 +23,6 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include "libavutil/common.h"
#include "libavutil/pixdesc.h" #include "libavutil/pixdesc.h"
#include "libavutil/pixfmt.h" #include "libavutil/pixfmt.h"
@ -33,35 +32,8 @@
#include "put_bits.h" #include "put_bits.h"
#include "mjpegenc.h" #include "mjpegenc.h"
#include "mjpegenc_common.h" #include "mjpegenc_common.h"
#include "mjpegenc_huffman.h"
#include "mjpeg.h" #include "mjpeg.h"
av_cold void ff_init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len)
{
int i;
for (i = 0; i < 128; i++) {
int level = i - 64;
int run;
if (!level)
continue;
for (run = 0; run < 64; run++) {
int len, code, nbits;
int alevel = FFABS(level);
len = (run >> 4) * huff_size_ac[0xf0];
nbits= av_log2_16bit(alevel) + 1;
code = ((15&run) << 4) | nbits;
len += huff_size_ac[code] + nbits;
uni_ac_vlc_len[UNI_AC_ENC_INDEX(run, i)] = len;
// We ignore EOB as its just a constant which does not change generally
}
}
}
/* table_class: 0 = DC coef, 1 = AC coefs */ /* table_class: 0 = DC coef, 1 = AC coefs */
static int put_huffman_table(PutBitContext *p, int table_class, int table_id, static int put_huffman_table(PutBitContext *p, int table_class, int table_id,
const uint8_t *bits_table, const uint8_t *value_table) const uint8_t *bits_table, const uint8_t *value_table)
@ -359,54 +331,6 @@ end:
} }
} }
/**
* 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);
}
void ff_mjpeg_escape_FF(PutBitContext *pb, int start) void ff_mjpeg_escape_FF(PutBitContext *pb, int start)
{ {
int size; int size;
@ -487,124 +411,6 @@ void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
} }
} }
/**
* Builds all 4 optimal Huffman tables.
*
* Uses the data stored in the JPEG buffer to compute the tables.
* Stores the Huffman tables in the bits_* and val_* arrays in the MJpegContext.
*
* @param m MJpegContext containing the JPEG buffer.
*/
static void ff_mjpeg_build_optimal_huffman(MJpegContext *m)
{
int i, table_id, code;
MJpegEncHuffmanContext dc_luminance_ctx;
MJpegEncHuffmanContext dc_chrominance_ctx;
MJpegEncHuffmanContext ac_luminance_ctx;
MJpegEncHuffmanContext ac_chrominance_ctx;
MJpegEncHuffmanContext *ctx[4] = {&dc_luminance_ctx,
&dc_chrominance_ctx,
&ac_luminance_ctx,
&ac_chrominance_ctx};
for (i = 0; i < 4; i++) {
ff_mjpeg_encode_huffman_init(ctx[i]);
}
for (i = 0; i < m->huff_ncode; i++) {
table_id = m->huff_buffer[i].table_id;
code = m->huff_buffer[i].code;
ff_mjpeg_encode_huffman_increment(ctx[table_id], code);
}
ff_mjpeg_encode_huffman_close(&dc_luminance_ctx,
m->bits_dc_luminance,
m->val_dc_luminance, 12);
ff_mjpeg_encode_huffman_close(&dc_chrominance_ctx,
m->bits_dc_chrominance,
m->val_dc_chrominance, 12);
ff_mjpeg_encode_huffman_close(&ac_luminance_ctx,
m->bits_ac_luminance,
m->val_ac_luminance, 256);
ff_mjpeg_encode_huffman_close(&ac_chrominance_ctx,
m->bits_ac_chrominance,
m->val_ac_chrominance, 256);
ff_mjpeg_build_huffman_codes(m->huff_size_dc_luminance,
m->huff_code_dc_luminance,
m->bits_dc_luminance,
m->val_dc_luminance);
ff_mjpeg_build_huffman_codes(m->huff_size_dc_chrominance,
m->huff_code_dc_chrominance,
m->bits_dc_chrominance,
m->val_dc_chrominance);
ff_mjpeg_build_huffman_codes(m->huff_size_ac_luminance,
m->huff_code_ac_luminance,
m->bits_ac_luminance,
m->val_ac_luminance);
ff_mjpeg_build_huffman_codes(m->huff_size_ac_chrominance,
m->huff_code_ac_chrominance,
m->bits_ac_chrominance,
m->val_ac_chrominance);
}
/**
* Writes the complete JPEG frame when optimal huffman tables are enabled,
* otherwise writes the stuffing.
*
* Header + values + stuffing.
*
* @param s The MpegEncContext.
* @return int Error code, 0 if successful.
*/
int ff_mjpeg_encode_stuffing(MpegEncContext *s)
{
int i;
PutBitContext *pbc = &s->pb;
int mb_y = s->mb_y - !s->mb_x;
int ret;
MJpegContext *m;
m = s->mjpeg_ctx;
if (s->huffman == HUFFMAN_TABLE_OPTIMAL) {
ff_mjpeg_build_optimal_huffman(m);
// Replace the VLCs with the optimal ones.
// The default ones may be used for trellis during quantization.
ff_init_uni_ac_vlc(m->huff_size_ac_luminance, m->uni_ac_vlc_len);
ff_init_uni_ac_vlc(m->huff_size_ac_chrominance, m->uni_chroma_ac_vlc_len);
s->intra_ac_vlc_length =
s->intra_ac_vlc_last_length = m->uni_ac_vlc_len;
s->intra_chroma_ac_vlc_length =
s->intra_chroma_ac_vlc_last_length = m->uni_chroma_ac_vlc_len;
ff_mjpeg_encode_picture_header(s->avctx, &s->pb, &s->intra_scantable,
s->pred, s->intra_matrix, s->chroma_intra_matrix);
ff_mjpeg_encode_picture_frame(s);
}
ret = ff_mpv_reallocate_putbitbuffer(s, put_bits_count(&s->pb) / 8 + 100,
put_bits_count(&s->pb) / 4 + 1000);
if (ret < 0) {
av_log(s->avctx, AV_LOG_ERROR, "Buffer reallocation failed\n");
goto fail;
}
ff_mjpeg_escape_FF(pbc, s->esc_pos);
if((s->avctx->active_thread_type & FF_THREAD_SLICE) && mb_y < s->mb_height - 1)
put_marker(pbc, RST0 + (mb_y&7));
s->esc_pos = put_bits_count(pbc) >> 3;
fail:
for(i=0; i<3; i++)
s->last_dc[i] = 128 << s->intra_dc_precision;
return ret;
}
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits) void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits)
{ {
av_assert1((header_bits & 7) == 0); av_assert1((header_bits & 7) == 0);

View File

@ -25,25 +25,20 @@
#include "avcodec.h" #include "avcodec.h"
#include "idctdsp.h" #include "idctdsp.h"
#include "mpegvideo.h"
#include "put_bits.h" #include "put_bits.h"
void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb, void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
ScanTable *intra_scantable, int pred, ScanTable *intra_scantable, int pred,
uint16_t luma_intra_matrix[64], uint16_t luma_intra_matrix[64],
uint16_t chroma_intra_matrix[64]); uint16_t chroma_intra_matrix[64]);
void ff_mjpeg_encode_picture_frame(MpegEncContext *s);
void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits); void ff_mjpeg_encode_picture_trailer(PutBitContext *pb, int header_bits);
void ff_mjpeg_escape_FF(PutBitContext *pb, int start); void ff_mjpeg_escape_FF(PutBitContext *pb, int start);
void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code, void ff_mjpeg_build_huffman_codes(uint8_t *huff_size, uint16_t *huff_code,
const uint8_t *bits_table, const uint8_t *bits_table,
const uint8_t *val_table); const uint8_t *val_table);
int ff_mjpeg_encode_stuffing(MpegEncContext *s);
void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4]); void ff_mjpeg_init_hvsample(AVCodecContext *avctx, int hsample[4], int vsample[4]);
void ff_mjpeg_encode_dc(PutBitContext *pb, int val, void ff_mjpeg_encode_dc(PutBitContext *pb, int val,
uint8_t *huff_size, uint16_t *huff_code); uint8_t *huff_size, uint16_t *huff_code);
av_cold void ff_init_uni_ac_vlc(const uint8_t huff_size_ac[256], uint8_t *uni_ac_vlc_len);
#endif /* AVCODEC_MJPEGENC_COMMON_H */ #endif /* AVCODEC_MJPEGENC_COMMON_H */