1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-21 10:55:51 +02:00

Merge commit 'ed3b2a999d189771c9b0ada9a9200117595fa474'

* commit 'ed3b2a999d189771c9b0ada9a9200117595fa474':
  cinepakenc: misc small changes
  cinepakenc: K&R formatting cosmetics
  cinepakenc: Stop using AVPicture
  cinepakenc: Drop broken debug code
  cinepakenc: add option handling for flexibility
  cinepakenc: fixes and improvements
  Add Cinepak encoder

See
59dbc36f49
762c4dc082
bf23642dcc
0ab25dac2f

Merged-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2017-11-07 18:12:17 -03:00
commit 5371274329

View File

@ -4,78 +4,49 @@
*
* Fixes and improvements, vintage decoders compatibility
* (c) 2013, 2014 Rl, Aetey Global Technologies AB
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
/*
* TODO:
* - optimize: color space conversion (move conversion to libswscale), ...
* MAYBE:
* - "optimally" split the frame into several non-regular areas
* using a separate codebook pair for each area and approximating
* the area by several rectangular strips (generally not full width ones)
* (use quadtree splitting? a simple fixed-granularity grid?)
*
*
* version 2014-01-23 Rl
* - added option handling for flexibility
*
* version 2014-01-21 Rl
* - believe it or not, now we get even smaller files, with better quality
* (which means I missed an optimization earlier :)
*
* version 2014-01-20 Rl
* - made the encoder compatible with vintage decoders
* and added some yet unused code for possible future
* incremental codebook updates
* - fixed a small memory leak
*
* version 2013-04-28 Rl
* - bugfixed codebook optimization logic
*
* version 2013-02-14 Rl
* "Valentine's Day" version:
* - made strip division more robust
* - minimized bruteforcing the number of strips,
* (costs some R/D but speeds up compession a lot), the heuristic
* assumption is that score as a function of the number of strips has
* one wide minimum which moves slowly, of course not fully true
* - simplified codebook generation,
* the old code was meant for other optimizations than we actually do
* - optimized the codebook generation / error estimation for MODE_MC
*
* version 2013-02-12 Rl
* - separated codebook training sets, avoided the transfer of wasted bytes,
* which yields both better quality and smaller files
* - now using the correct colorspace (TODO: move conversion to libswscale)
*
* version 2013-02-08 Rl
* - fixes/optimization in multistrip encoding and codebook size choice,
* quality/bitrate is now better than that of the binary proprietary encoder
*/
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
#include "libavutil/lfg.h"
#include "elbg.h"
#include "internal.h"
#include <string.h>
#include "libavutil/avassert.h"
#include "libavutil/common.h"
#include "libavutil/internal.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/lfg.h"
#include "libavutil/opt.h"
#include "avcodec.h"
#include "elbg.h"
#include "internal.h"
#define CVID_HEADER_SIZE 10
#define STRIP_HEADER_SIZE 12
#define CHUNK_HEADER_SIZE 4
@ -97,7 +68,7 @@ OTHER DEALINGS IN THE SOFTWARE.
// NOTE the decoder in ffmpeg has its own arbitrary limitation on the number
// of strips, currently 32
typedef enum {
typedef enum CinepakMode {
MODE_V1_ONLY = 0,
MODE_V1_V4,
MODE_MC,
@ -105,7 +76,7 @@ typedef enum {
MODE_COUNT,
} CinepakMode;
typedef enum {
typedef enum mb_encoding {
ENC_V1,
ENC_V4,
ENC_SKIP,
@ -113,7 +84,7 @@ typedef enum {
ENC_UNCERTAIN
} mb_encoding;
typedef struct {
typedef struct mb_info {
int v1_vector; // index into v1 codebook
int v1_error; // error when using V1 encoding
int v4_vector[4]; // indices into v4 codebook
@ -122,7 +93,7 @@ typedef struct {
mb_encoding best_encoding; // last result from calculate_mode_score()
} mb_info;
typedef struct {
typedef struct strip_info {
int v1_codebook[CODEBOOK_MAX * VECTOR_MAX];
int v4_codebook[CODEBOOK_MAX * VECTOR_MAX];
int v1_size;
@ -130,7 +101,7 @@ typedef struct {
CinepakMode mode;
} strip_info;
typedef struct {
typedef struct CinepakEncContext {
const AVClass *class;
AVCodecContext *avctx;
unsigned char *pict_bufs[4], *strip_buf, *frame_buf;
@ -149,11 +120,6 @@ typedef struct {
mb_info *mb; // MB RD state
int min_strips; // the current limit
int max_strips; // the current limit
#ifdef CINEPAKENC_DEBUG
mb_info *best_mb; //TODO: remove. only used for printing stats
int num_v1_mode, num_v4_mode, num_mc_mode;
int num_v1_encs, num_v4_encs, num_skips;
#endif
// options
int max_extra_cb_iterations;
int skip_empty_cb;
@ -165,11 +131,16 @@ typedef struct {
#define OFFSET(x) offsetof(CinepakEncContext, x)
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = {
{ "max_extra_cb_iterations", "Max extra codebook recalculation passes, more is better and slower", OFFSET(max_extra_cb_iterations), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, INT_MAX, VE },
{ "skip_empty_cb", "Avoid wasting bytes, ignore vintage MacOS decoder", OFFSET(skip_empty_cb), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
{ "max_strips", "Limit strips/frame, vintage compatible is 1..3, otherwise the more the better", OFFSET(max_max_strips), AV_OPT_TYPE_INT, { .i64 = 3 }, MIN_STRIPS, MAX_STRIPS, VE },
{ "min_strips", "Enforce min strips/frame, more is worse and faster, must be <= max_strips", OFFSET(min_min_strips), AV_OPT_TYPE_INT, { .i64 = MIN_STRIPS }, MIN_STRIPS, MAX_STRIPS, VE },
{ "strip_number_adaptivity", "How fast the strip number adapts, more is slightly better, much slower", OFFSET(strip_number_delta_range), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_STRIPS-MIN_STRIPS, VE },
{ "max_extra_cb_iterations", "Max extra codebook recalculation passes, more is better and slower",
OFFSET(max_extra_cb_iterations), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, INT_MAX, VE },
{ "skip_empty_cb", "Avoid wasting bytes, ignore vintage MacOS decoder",
OFFSET(skip_empty_cb), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
{ "max_strips", "Limit strips/frame, vintage compatible is 1..3, otherwise the more the better",
OFFSET(max_max_strips), AV_OPT_TYPE_INT, { .i64 = 3 }, MIN_STRIPS, MAX_STRIPS, VE },
{ "min_strips", "Enforce min strips/frame, more is worse and faster, must be <= max_strips",
OFFSET(min_min_strips), AV_OPT_TYPE_INT, { .i64 = MIN_STRIPS }, MIN_STRIPS, MAX_STRIPS, VE },
{ "strip_number_adaptivity", "How fast the strip number adapts, more is slightly better, much slower",
OFFSET(strip_number_delta_range), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_STRIPS - MIN_STRIPS, VE },
{ NULL },
};
@ -192,7 +163,7 @@ static av_cold int cinepak_encode_init(AVCodecContext *avctx)
}
if (s->min_min_strips > s->max_max_strips) {
av_log(avctx, AV_LOG_ERROR, "minimal number of strips can not exceed maximal (got %i and %i)\n",
av_log(avctx, AV_LOG_ERROR, "minimum number of strips must not exceed maximum (got %i and %i)\n",
s->min_min_strips, s->max_max_strips);
return AVERROR(EINVAL);
}
@ -207,10 +178,10 @@ static av_cold int cinepak_encode_init(AVCodecContext *avctx)
if (!(s->input_frame = av_frame_alloc()))
goto enomem;
if (!(s->codebook_input = av_malloc(sizeof(int) * (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4) * (avctx->width * avctx->height) >> 2)))
if (!(s->codebook_input = av_malloc_array((avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4) * (avctx->width * avctx->height) >> 2, sizeof(*s->codebook_input))))
goto enomem;
if (!(s->codebook_closest = av_malloc(sizeof(int) * (avctx->width * avctx->height) >> 2)))
if (!(s->codebook_closest = av_malloc_array((avctx->width * avctx->height) >> 2, sizeof(*s->codebook_closest))))
goto enomem;
for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
@ -236,11 +207,6 @@ static av_cold int cinepak_encode_init(AVCodecContext *avctx)
if (!(s->mb = av_malloc_array(mb_count, sizeof(mb_info))))
goto enomem;
#ifdef CINEPAKENC_DEBUG
if (!(s->best_mb = av_malloc_array(mb_count, sizeof(mb_info))))
goto enomem;
#endif
av_lfg_init(&s->randctx, 1);
s->avctx = avctx;
s->w = avctx->width;
@ -261,30 +227,30 @@ static av_cold int cinepak_encode_init(AVCodecContext *avctx)
if (s->pix_fmt == AV_PIX_FMT_RGB24) {
s->last_frame->data[1] = s->last_frame->data[0] + s->w * s->h;
s->last_frame->data[2] = s->last_frame->data[1] + ((s->w * s->h) >> 2);
s->last_frame->linesize[1] = s->last_frame->linesize[2] = s->w >> 1;
s->last_frame->linesize[1] =
s->last_frame->linesize[2] = s->w >> 1;
s->best_frame->data[1] = s->best_frame->data[0] + s->w * s->h;
s->best_frame->data[2] = s->best_frame->data[1] + ((s->w * s->h) >> 2);
s->best_frame->linesize[1] = s->best_frame->linesize[2] = s->w >> 1;
s->best_frame->linesize[1] =
s->best_frame->linesize[2] = s->w >> 1;
s->scratch_frame->data[1] = s->scratch_frame->data[0] + s->w * s->h;
s->scratch_frame->data[2] = s->scratch_frame->data[1] + ((s->w * s->h) >> 2);
s->scratch_frame->linesize[1] = s->scratch_frame->linesize[2] = s->w >> 1;
s->scratch_frame->linesize[1] =
s->scratch_frame->linesize[2] = s->w >> 1;
s->input_frame->data[0] = s->pict_bufs[3];
s->input_frame->linesize[0] = s->w;
s->input_frame->data[1] = s->input_frame->data[0] + s->w * s->h;
s->input_frame->data[2] = s->input_frame->data[1] + ((s->w * s->h) >> 2);
s->input_frame->linesize[1] = s->input_frame->linesize[2] = s->w >> 1;
s->input_frame->linesize[1] =
s->input_frame->linesize[2] = s->w >> 1;
}
s->min_strips = s->min_min_strips;
s->max_strips = s->max_max_strips;
#ifdef CINEPAKENC_DEBUG
s->num_v1_mode = s->num_v4_mode = s->num_mc_mode = s->num_v1_encs = s->num_v4_encs = s->num_skips = 0;
#endif
return 0;
enomem:
@ -298,9 +264,6 @@ enomem:
av_freep(&s->strip_buf);
av_freep(&s->frame_buf);
av_freep(&s->mb);
#ifdef CINEPAKENC_DEBUG
av_freep(&s->best_mb);
#endif
for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
av_freep(&s->pict_bufs[x]);
@ -308,11 +271,10 @@ enomem:
return AVERROR(ENOMEM);
}
static int64_t calculate_mode_score(CinepakEncContext *s, int h, strip_info *info, int report, int *training_set_v1_shrunk, int *training_set_v4_shrunk
#ifdef CINEPAK_REPORT_SERR
, int64_t *serr
#endif
)
static int64_t calculate_mode_score(CinepakEncContext *s, int h,
strip_info *info, int report,
int *training_set_v1_shrunk,
int *training_set_v4_shrunk)
{
// score = FF_LAMBDA_SCALE * error + lambda * bits
int x;
@ -324,12 +286,6 @@ static int64_t calculate_mode_score(CinepakEncContext *s, int h, strip_info *inf
(info->v4_size ? CHUNK_HEADER_SIZE + info->v4_size * entry_size : 0) +
CHUNK_HEADER_SIZE) << 3;
//av_log(s->avctx, AV_LOG_INFO, "sizes %3i %3i -> %9"PRId64" score mb_count %i", info->v1_size, info->v4_size, ret, mb_count);
#ifdef CINEPAK_REPORT_SERR
*serr = 0;
#endif
switch (info->mode) {
case MODE_V1_ONLY:
// one byte per MB
@ -339,9 +295,6 @@ static int64_t calculate_mode_score(CinepakEncContext *s, int h, strip_info *inf
for (x = 0; x < mb_count; x++) {
mb = &s->mb[x];
ret += FF_LAMBDA_SCALE * mb->v1_error;
#ifdef CINEPAK_REPORT_SERR
*serr += mb->v1_error;
#endif
// this function is never called for report in MODE_V1_ONLY
// if (!report)
mb->best_encoding = ENC_V1;
@ -361,9 +314,6 @@ static int64_t calculate_mode_score(CinepakEncContext *s, int h, strip_info *inf
else
score1 = s->lambda * 33 + FF_LAMBDA_SCALE * (mberr = mb->v4_error);
ret += score1;
#ifdef CINEPAK_REPORT_SERR
*serr += mberr;
#endif
}
} else { // find best mode per block
for (x = 0; x < mb_count; x++) {
@ -373,15 +323,9 @@ static int64_t calculate_mode_score(CinepakEncContext *s, int h, strip_info *inf
if (score1 <= score2) {
ret += score1;
#ifdef CINEPAK_REPORT_SERR
*serr += mb->v1_error;
#endif
mb->best_encoding = ENC_V1;
} else {
ret += score2;
#ifdef CINEPAK_REPORT_SERR
*serr += mb->v4_error;
#endif
mb->best_encoding = ENC_V4;
}
}
@ -399,36 +343,21 @@ static int64_t calculate_mode_score(CinepakEncContext *s, int h, strip_info *inf
score1 = s->lambda * 1 + FF_LAMBDA_SCALE * mb->skip_error;
if (mb->best_encoding == ENC_SKIP) {
ret += score1;
#ifdef CINEPAK_REPORT_SERR
*serr += mb->skip_error;
#endif
} else if (mb->best_encoding == ENC_V1) {
if ((score2 = s->lambda * 10 + FF_LAMBDA_SCALE * mb->v1_error) >= score1) {
mb->best_encoding = ENC_SKIP;
++v1_shrunk;
ret += score1;
#ifdef CINEPAK_REPORT_SERR
*serr += mb->skip_error;
#endif
} else {
ret += score2;
#ifdef CINEPAK_REPORT_SERR
*serr += mb->v1_error;
#endif
}
} else {
if ((score3 = s->lambda * 34 + FF_LAMBDA_SCALE * mb->v4_error) >= score1) {
mb->best_encoding = ENC_SKIP;
++v4_shrunk;
ret += score1;
#ifdef CINEPAK_REPORT_SERR
*serr += mb->skip_error;
#endif
} else {
ret += score3;
#ifdef CINEPAK_REPORT_SERR
*serr += mb->v4_error;
#endif
}
}
}
@ -443,21 +372,12 @@ static int64_t calculate_mode_score(CinepakEncContext *s, int h, strip_info *inf
if (score1 <= score2 && score1 <= score3) {
ret += score1;
#ifdef CINEPAK_REPORT_SERR
*serr += mb->skip_error;
#endif
mb->best_encoding = ENC_SKIP;
} else if (score2 <= score3) {
ret += score2;
#ifdef CINEPAK_REPORT_SERR
*serr += mb->v1_error;
#endif
mb->best_encoding = ENC_V1;
} else {
ret += score3;
#ifdef CINEPAK_REPORT_SERR
*serr += mb->v4_error;
#endif
mb->best_encoding = ENC_V4;
}
}
@ -476,7 +396,9 @@ static int write_chunk_header(unsigned char *buf, int chunk_type, int chunk_size
return CHUNK_HEADER_SIZE;
}
static int encode_codebook(CinepakEncContext *s, int *codebook, int size, int chunk_type_yuv, int chunk_type_gray, unsigned char *buf)
static int encode_codebook(CinepakEncContext *s, int *codebook, int size,
int chunk_type_yuv, int chunk_type_gray,
unsigned char *buf)
{
int x, y, ret, entry_size = s->pix_fmt == AV_PIX_FMT_RGB24 ? 6 : 4;
int incremental_codebook_replacement_mode = 0; // hardcoded here,
@ -486,8 +408,8 @@ static int encode_codebook(CinepakEncContext *s, int *codebook, int size, int ch
s->pix_fmt == AV_PIX_FMT_RGB24 ?
chunk_type_yuv + (incremental_codebook_replacement_mode ? 1 : 0) :
chunk_type_gray + (incremental_codebook_replacement_mode ? 1 : 0),
entry_size * size
+ (incremental_codebook_replacement_mode?(size+31)/32*4:0) );
entry_size * size +
(incremental_codebook_replacement_mode ? (size + 31) / 32 * 4 : 0));
// we do codebook encoding according to the "intra" mode
// but we keep the "dead" code for reference in case we will want
@ -604,21 +526,18 @@ static void copy_mb(CinepakEncContext *s,
{
int y, p;
for(y = 0; y < MB_SIZE; y++) {
for (y = 0; y < MB_SIZE; y++)
memcpy(a_data[0] + y * a_linesize[0], b_data[0] + y * b_linesize[0],
MB_SIZE);
}
if (s->pix_fmt == AV_PIX_FMT_RGB24) {
for(p = 1; p <= 2; p++) {
for(y = 0; y < MB_SIZE/2; y++) {
for (p = 1; p <= 2; p++)
for (y = 0; y < MB_SIZE / 2; y++)
memcpy(a_data[p] + y * a_linesize[p],
b_data[p] + y * b_linesize[p],
MB_SIZE / 2);
}
}
}
}
static int encode_mode(CinepakEncContext *s, int h,
uint8_t *scratch_data[4], int scratch_linesize[4],
@ -643,7 +562,7 @@ static int encode_mode(CinepakEncContext *s, int h,
ret += encode_codebook(s, info->v1_codebook, info->v1_size, 0x22, 0x26, buf + ret);
// update scratch picture
for(z = y = 0; y < h; y += MB_SIZE) {
for (z = y = 0; y < h; y += MB_SIZE)
for (x = 0; x < s->w; x += MB_SIZE, z++) {
mb = &s->mb[z];
@ -651,8 +570,7 @@ static int encode_mode(CinepakEncContext *s, int h,
sub_scratch_data, sub_scratch_linesize);
if (info->mode == MODE_MC && mb->best_encoding == ENC_SKIP) {
get_sub_picture(s, x, y,
last_data, last_linesize,
get_sub_picture(s, x, y, last_data, last_linesize,
sub_last_data, sub_last_linesize);
copy_mb(s, sub_scratch_data, sub_scratch_linesize,
sub_last_data, sub_last_linesize);
@ -663,11 +581,9 @@ static int encode_mode(CinepakEncContext *s, int h,
decode_v4_vector(s, sub_scratch_data, sub_scratch_linesize,
mb->v4_vector, info);
}
}
switch (info->mode) {
case MODE_V1_ONLY:
//av_log(s->avctx, AV_LOG_INFO, "mb_count = %i\n", mb_count);
ret += write_chunk_header(buf + ret, 0x32, mb_count);
for (x = 0; x < mb_count; x++)
@ -774,32 +690,29 @@ static int compute_mb_distortion(CinepakEncContext *s,
{
int x, y, p, d, ret = 0;
for(y = 0; y < MB_SIZE; y++) {
for (y = 0; y < MB_SIZE; y++)
for (x = 0; x < MB_SIZE; x++) {
d = a_data[0][x + y * a_linesize[0]] - b_data[0][x + y * b_linesize[0]];
ret += d * d;
}
}
if (s->pix_fmt == AV_PIX_FMT_RGB24) {
for (p = 1; p <= 2; p++) {
for(y = 0; y < MB_SIZE/2; y++) {
for (y = 0; y < MB_SIZE / 2; y++)
for (x = 0; x < MB_SIZE / 2; x++) {
d = a_data[p][x + y * a_linesize[p]] - b_data[p][x + y * b_linesize[p]];
ret += d * d;
}
}
}
}
return ret;
}
// return the possibly adjusted size of the codebook
#define CERTAIN(x) ((x) != ENC_UNCERTAIN)
static int quantize(CinepakEncContext *s, int h,
uint8_t *data[4], int linesize[4],
int v1mode, strip_info *info,
static int quantize(CinepakEncContext *s, int h, uint8_t *data[4],
int linesize[4], int v1mode, strip_info *info,
mb_encoding encoding)
{
int x, y, i, j, k, x2, y2, x3, y3, plane, shift, mbn;
@ -817,13 +730,14 @@ static int quantize(CinepakEncContext *s, int h,
if (CERTAIN(encoding)) {
// use for the training only the blocks known to be to be encoded [sic:-]
if(s->mb[mbn].best_encoding != encoding) continue;
if (s->mb[mbn].best_encoding != encoding)
continue;
}
base = s->codebook_input + i * entry_size;
if (v1mode) {
// subsample
for(j = y2 = 0; y2 < entry_size; y2 += 2) {
for (j = y2 = 0; y2 < entry_size; y2 += 2)
for (x2 = 0; x2 < 4; x2 += 2, j++) {
plane = y2 < 4 ? 0 : 1 + (x2 >> 1);
shift = y2 < 4 ? 0 : 1;
@ -834,11 +748,10 @@ static int quantize(CinepakEncContext *s, int h,
data[plane][((x + x3) >> shift) + (((y + y3) >> shift) + 1) * linesize[plane]] +
data[plane][((x + x3) >> shift) + 1 + (((y + y3) >> shift) + 1) * linesize[plane]]) >> 2;
}
}
} else {
// copy
for (j = y2 = 0; y2 < MB_SIZE; y2 += 2) {
for(x2 = 0; x2 < MB_SIZE; x2 += 2) {
for (x2 = 0; x2 < MB_SIZE; x2 += 2)
for (k = 0; k < entry_size; k++, j++) {
plane = k >= 4 ? k - 3 : 0;
@ -854,20 +767,14 @@ static int quantize(CinepakEncContext *s, int h,
}
}
}
}
i += v1mode ? 1 : 4;
}
}
// if(i < mbn*(v1mode ? 1 : 4)) {
// av_log(s->avctx, AV_LOG_INFO, "reducing training set for %s from %i to %i (encoding %i)\n", v1mode?"v1":"v4", mbn*(v1mode ? 1 : 4), i, encoding);
// }
if (i == 0) // empty training set, nothing to do
return 0;
if(i < size) {
//av_log(s->avctx, (CERTAIN(encoding) ? AV_LOG_ERROR : AV_LOG_INFO), "WOULD WASTE: %s cbsize %i bigger than training set size %i (encoding %i)\n", v1mode?"v1":"v4", size, i, encoding);
if (i < size)
size = i;
}
avpriv_init_elbg(s->codebook_input, entry_size, i, codebook, size, 1, s->codebook_closest, &s->randctx);
avpriv_do_elbg(s->codebook_input, entry_size, i, codebook, size, 1, s->codebook_closest, &s->randctx);
@ -877,10 +784,11 @@ static int quantize(CinepakEncContext *s, int h,
vq_linesize[0] = MB_SIZE;
vq_data[1] = &vq_pict_buf[MB_AREA];
vq_data[2] = vq_data[1] + (MB_AREA >> 2);
vq_linesize[1] = vq_linesize[2] = MB_SIZE >> 1;
vq_linesize[1] =
vq_linesize[2] = MB_SIZE >> 1;
// copy indices
for(i = j = y = 0; y < h; y += MB_SIZE) {
for (i = j = y = 0; y < h; y += MB_SIZE)
for (x = 0; x < s->w; x += MB_SIZE, j++) {
mb_info *mb = &s->mb[j];
// skip uninteresting blocks if we know their preferred encoding
@ -912,12 +820,9 @@ static int quantize(CinepakEncContext *s, int h,
}
i += v1mode ? 1 : 4;
}
}
// check that we did it right in the beginning of the function
av_assert0(i >= size); // training set is no smaller than the codebook
//av_log(s->avctx, AV_LOG_INFO, "isv1 %i size= %i i= %i error %"PRId64"\n", v1mode, size, i, total_error);
return size;
}
@ -930,21 +835,22 @@ static void calculate_skip_errors(CinepakEncContext *s, int h,
uint8_t *sub_last_data [4], *sub_pict_data [4];
int sub_last_linesize[4], sub_pict_linesize[4];
for(i = y = 0; y < h; y += MB_SIZE) {
for (i = y = 0; y < h; y += MB_SIZE)
for (x = 0; x < s->w; x += MB_SIZE, i++) {
get_sub_picture(s, x, y, last_data, last_linesize,
sub_last_data, sub_last_linesize);
get_sub_picture(s, x, y, data, linesize,
sub_pict_data, sub_pict_linesize);
s->mb[i].skip_error = compute_mb_distortion(s,
s->mb[i].skip_error =
compute_mb_distortion(s,
sub_last_data, sub_last_linesize,
sub_pict_data, sub_pict_linesize);
}
}
}
static void write_strip_header(CinepakEncContext *s, int y, int h, int keyframe, unsigned char *buf, int strip_size)
static void write_strip_header(CinepakEncContext *s, int y, int h, int keyframe,
unsigned char *buf, int strip_size)
{
// actually we are exclusively using intra strip coding (how much can we win
// otherwise? how to choose which part of a codebook to update?),
@ -959,23 +865,15 @@ static void write_strip_header(CinepakEncContext *s, int y, int h, int keyframe,
// AV_WB16(&buf[8], y + h); /* using absolute y values works -- rl */
AV_WB16(&buf[8], h); /* using relative values works as well -- rl */
AV_WB16(&buf[10], s->w);
//av_log(s->avctx, AV_LOG_INFO, "write_strip_header() %x keyframe=%d\n", buf[0], keyframe);
}
static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe,
uint8_t *last_data[4], int last_linesize[4],
uint8_t *data[4], int linesize[4],
uint8_t *scratch_data[4], int scratch_linesize[4],
unsigned char *buf, int64_t *best_score
#ifdef CINEPAK_REPORT_SERR
, int64_t *best_serr
#endif
)
unsigned char *buf, int64_t *best_score)
{
int64_t score = 0;
#ifdef CINEPAK_REPORT_SERR
int64_t serr;
#endif
int best_size = 0;
strip_info info;
// for codebook optimization:
@ -1029,11 +927,7 @@ static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe,
info.mode = mode;
// choose the best encoding per block, based on current experience
score = calculate_mode_score(s, h, &info, 0,
&v1shrunk, &v4shrunk
#ifdef CINEPAK_REPORT_SERR
, &serr
#endif
);
&v1shrunk, &v4shrunk);
if (mode != MODE_V1_ONLY) {
int extra_iterations_limit = s->max_extra_cb_iterations;
@ -1041,111 +935,60 @@ static int rd_strip(CinepakEncContext *s, int y, int h, int keyframe,
// we assume we _may_ come here with more blocks to encode than before
info.v1_size = v1_size;
new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
if(new_v1_size < info.v1_size){
//av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: cut v1 codebook to %i entries\n", mode, v1_size, v4_size, new_v1_size);
if (new_v1_size < info.v1_size)
info.v1_size = new_v1_size;
}
// we assume we _may_ come here with more blocks to encode than before
info.v4_size = v4_size;
new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
if(new_v4_size < info.v4_size) {
//av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: cut v4 codebook to %i entries at first iteration\n", mode, v1_size, v4_size, new_v4_size);
if (new_v4_size < info.v4_size)
info.v4_size = new_v4_size;
}
// calculate the resulting score
// (do not move blocks to codebook encodings now, as some blocks may have
// got bigger errors despite a smaller training set - but we do not
// ever grow the training sets back)
for (;;) {
score = calculate_mode_score(s, h, &info, 1,
&v1shrunk, &v4shrunk
#ifdef CINEPAK_REPORT_SERR
, &serr
#endif
);
&v1shrunk, &v4shrunk);
// do we have a reason to reiterate? if so, have we reached the limit?
if((!v1shrunk && !v4shrunk) || !extra_iterations_limit--) break;
if ((!v1shrunk && !v4shrunk) || !extra_iterations_limit--)
break;
// recompute the codebooks, omitting the extra blocks
if (v1shrunk) {
info.v1_size = v1_size;
new_v1_size = quantize(s, h, data, linesize, 1, &info, ENC_V1);
if(new_v1_size < info.v1_size){
//av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: cut v1 codebook to %i entries\n", mode, v1_size, v4_size, new_v1_size);
if (new_v1_size < info.v1_size)
info.v1_size = new_v1_size;
}
}
if (v4shrunk) {
info.v4_size = v4_size;
new_v4_size = quantize(s, h, data, linesize, 0, &info, ENC_V4);
if(new_v4_size < info.v4_size) {
//av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: cut v4 codebook to %i entries\n", mode, v1_size, v4_size, new_v4_size);
if (new_v4_size < info.v4_size)
info.v4_size = new_v4_size;
}
}
}
}
//av_log(s->avctx, AV_LOG_INFO, "%3i %3i score = %"PRId64"\n", v1_size, v4_size, score);
if (best_size == 0 || score < *best_score) {
*best_score = score;
#ifdef CINEPAK_REPORT_SERR
*best_serr = serr;
#endif
best_size = encode_mode(s, h,
scratch_data, scratch_linesize,
last_data, last_linesize, &info,
s->strip_buf + STRIP_HEADER_SIZE);
//av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: %18"PRId64" %i B", mode, info.v1_size, info.v4_size, score, best_size);
//av_log(s->avctx, AV_LOG_INFO, "\n");
#ifdef CINEPAK_REPORT_SERR
av_log(s->avctx, AV_LOG_INFO, "mode %i, %3i, %3i: %18"PRId64" %i B\n", mode, v1_size, v4_size, serr, best_size);
#endif
#ifdef CINEPAKENC_DEBUG
//save MB encoding choices
memcpy(s->best_mb, s->mb, mb_count*sizeof(mb_info));
#endif
//memcpy(strip_temp + STRIP_HEADER_SIZE, strip_temp, best_size);
write_strip_header(s, y, h, keyframe, s->strip_buf, best_size);
}
}
}
}
#ifdef CINEPAKENC_DEBUG
//gather stats. this will only work properly of MAX_STRIPS == 1
if(best_info.mode == MODE_V1_ONLY) {
s->num_v1_mode++;
s->num_v1_encs += s->w*h/MB_AREA;
} else {
if(best_info.mode == MODE_V1_V4)
s->num_v4_mode++;
else
s->num_mc_mode++;
int x;
for(x = 0; x < s->w*h/MB_AREA; x++)
if(s->best_mb[x].best_encoding == ENC_V1)
s->num_v1_encs++;
else if(s->best_mb[x].best_encoding == ENC_V4)
s->num_v4_encs++;
else
s->num_skips++;
}
#endif
best_size += STRIP_HEADER_SIZE;
memcpy(buf, s->strip_buf, best_size);
return best_size;
}
static int write_cvid_header(CinepakEncContext *s, unsigned char *buf, int num_strips, int data_size, int isakeyframe)
static int write_cvid_header(CinepakEncContext *s, unsigned char *buf,
int num_strips, int data_size, int isakeyframe)
{
buf[0] = isakeyframe ? 0 : 1;
AV_WB24(&buf[1], data_size + CVID_HEADER_SIZE);
@ -1159,22 +1002,19 @@ static int write_cvid_header(CinepakEncContext *s, unsigned char *buf, int num_s
static int rd_frame(CinepakEncContext *s, const AVFrame *frame,
int isakeyframe, unsigned char *buf, int buf_size)
{
int num_strips, strip, i, y, nexty, size, temp_size;
int num_strips, strip, i, y, nexty, size, temp_size, best_size;
uint8_t *last_data [4], *data [4], *scratch_data [4];
int last_linesize[4], linesize[4], scratch_linesize[4];
int64_t best_score = 0, score, score_temp;
#ifdef CINEPAK_REPORT_SERR
int64_t best_serr = 0, serr, serr_temp;
#endif
int best_nstrips = -1, best_size = -1; // mark as uninitialzed
int best_nstrips;
if (s->pix_fmt == AV_PIX_FMT_RGB24) {
int x;
// build a copy of the given frame in the correct colorspace
for(y = 0; y < s->h; y += 2) {
for (y = 0; y < s->h; y += 2)
for (x = 0; x < s->w; x += 2) {
uint8_t *ir[2]; int32_t r, g, b, rr, gg, bb;
uint8_t *ir[2];
int32_t r, g, b, rr, gg, bb;
ir[0] = frame->data[0] + x * 3 + y * frame->linesize[0];
ir[1] = ir[0] + frame->linesize[0];
get_sub_picture(s, x, y,
@ -1183,17 +1023,22 @@ static int rd_frame(CinepakEncContext *s, const AVFrame *frame,
r = g = b = 0;
for (i = 0; i < 4; ++i) {
int i1, i2;
i1 = (i&1); i2 = (i>=2);
i1 = (i & 1);
i2 = (i >= 2);
rr = ir[i2][i1 * 3 + 0];
gg = ir[i2][i1 * 3 + 1];
bb = ir[i2][i1 * 3 + 2];
r += rr; g += gg; b += bb;
r += rr;
g += gg;
b += bb;
// using fixed point arithmetic for portable repeatability, scaling by 2^23
// "Y"
// rr = 0.2857 * rr + 0.5714 * gg + 0.1429 * bb;
rr = (2396625 * rr + 4793251 * gg + 1198732 * bb) >> 23;
if( rr < 0) rr = 0;
else if (rr > 255) rr = 255;
if (rr < 0)
rr = 0;
else if (rr > 255)
rr = 255;
scratch_data[0][i1 + i2 * scratch_linesize[0]] = rr;
}
// let us scale down as late as possible
@ -1201,27 +1046,27 @@ static int rd_frame(CinepakEncContext *s, const AVFrame *frame,
// "U"
// rr = -0.1429 * r - 0.2857 * g + 0.4286 * b;
rr = (-299683 * r - 599156 * g + 898839 * b) >> 23;
if( rr < -128) rr = -128;
else if (rr > 127) rr = 127;
if (rr < -128)
rr = -128;
else if (rr > 127)
rr = 127;
scratch_data[1][0] = rr + 128; // quantize needs unsigned
// "V"
// rr = 0.3571 * r - 0.2857 * g - 0.0714 * b;
rr = (748893 * r - 599156 * g - 149737 * b) >> 23;
if( rr < -128) rr = -128;
else if (rr > 127) rr = 127;
if (rr < -128)
rr = -128;
else if (rr > 127)
rr = 127;
scratch_data[2][0] = rr + 128; // quantize needs unsigned
}
}
}
// would be nice but quite certainly incompatible with vintage players:
// support encoding zero strips (meaning skip the whole frame)
for (num_strips = s->min_strips; num_strips <= s->max_strips && num_strips <= s->h / MB_SIZE; num_strips++) {
score = 0;
size = 0;
#ifdef CINEPAK_REPORT_SERR
serr = 0;
#endif
for (y = 0, strip = 1; y < s->h; strip++, y = nexty) {
int strip_height;
@ -1255,32 +1100,17 @@ static int rd_frame(CinepakEncContext *s, const AVFrame *frame,
if ((temp_size = rd_strip(s, y, strip_height, isakeyframe,
last_data, last_linesize, data, linesize,
scratch_data, scratch_linesize,
s->frame_buf + size + CVID_HEADER_SIZE, &score_temp
#ifdef CINEPAK_REPORT_SERR
, &serr_temp
#endif
)) < 0)
s->frame_buf + size + CVID_HEADER_SIZE,
&score_temp)) < 0)
return temp_size;
score += score_temp;
#ifdef CINEPAK_REPORT_SERR
serr += serr_temp;
#endif
size += temp_size;
//av_log(s->avctx, AV_LOG_INFO, "strip %d, isakeyframe=%d", strip, isakeyframe);
//av_log(s->avctx, AV_LOG_INFO, "\n");
}
if (best_score == 0 || score < best_score) {
best_score = score;
#ifdef CINEPAK_REPORT_SERR
best_serr = serr;
#endif
best_size = size + write_cvid_header(s, s->frame_buf, num_strips, size, isakeyframe);
//av_log(s->avctx, AV_LOG_INFO, "best number of strips so far: %2i, %12"PRId64", %i B\n", num_strips, score, best_size);
#ifdef CINEPAK_REPORT_SERR
av_log(s->avctx, AV_LOG_INFO, "best number of strips so far: %2i, %12"PRId64", %i B\n", num_strips, serr, best_size);
#endif
FFSWAP(AVFrame *, s->best_frame, s->scratch_frame);
memcpy(buf, s->frame_buf, best_size);
@ -1292,8 +1122,6 @@ static int rd_frame(CinepakEncContext *s, const AVFrame *frame,
break;
}
av_assert0(best_nstrips >= 0 && best_size >= 0);
// let the number of strips slowly adapt to the changes in the contents,
// compared to full bruteforcing every time this will occasionally lead
// to some r/d performance loss but makes encoding up to several times faster
@ -1359,23 +1187,16 @@ static av_cold int cinepak_encode_end(AVCodecContext *avctx)
av_freep(&s->strip_buf);
av_freep(&s->frame_buf);
av_freep(&s->mb);
#ifdef CINEPAKENC_DEBUG
av_freep(&s->best_mb);
#endif
for (x = 0; x < (avctx->pix_fmt == AV_PIX_FMT_RGB24 ? 4 : 3); x++)
av_freep(&s->pict_bufs[x]);
#ifdef CINEPAKENC_DEBUG
av_log(avctx, AV_LOG_INFO, "strip coding stats: %i V1 mode, %i V4 mode, %i MC mode (%i V1 encs, %i V4 encs, %i skips)\n",
s->num_v1_mode, s->num_v4_mode, s->num_mc_mode, s->num_v1_encs, s->num_v4_encs, s->num_skips);
#endif
return 0;
}
AVCodec ff_cinepak_encoder = {
.name = "cinepak",
.long_name = NULL_IF_CONFIG_SMALL("Cinepak"),
.type = AVMEDIA_TYPE_VIDEO,
.id = AV_CODEC_ID_CINEPAK,
.priv_data_size = sizeof(CinepakEncContext),
@ -1383,6 +1204,5 @@ AVCodec ff_cinepak_encoder = {
.encode2 = cinepak_encode_frame,
.close = cinepak_encode_end,
.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_RGB24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE },
.long_name = NULL_IF_CONFIG_SMALL("Cinepak"),
.priv_class = &cinepak_class,
};