51 #define CVID_HEADER_SIZE 10
52 #define STRIP_HEADER_SIZE 12
53 #define CHUNK_HEADER_SIZE 4
55 #define MB_SIZE 4 //4x4 MBs
56 #define MB_AREA (MB_SIZE * MB_SIZE)
58 #define VECTOR_MAX 6 // six or four entries per vector depending on format
59 #define CODEBOOK_MAX 256 // size of a codebook
61 #define MAX_STRIPS 32 // Note: having fewer choices regarding the number of strips speeds up encoding (obviously)
62 #define MIN_STRIPS 1 // Note: having more strips speeds up encoding the frame (this is less obvious)
133 #define OFFSET(x) offsetof(CinepakEncContext, x)
134 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
136 {
"max_extra_cb_iterations",
"Max extra codebook recalculation passes, more is better and slower",
138 {
"skip_empty_cb",
"Avoid wasting bytes, ignore vintage MacOS decoder",
140 {
"max_strips",
"Limit strips/frame, vintage compatible is 1..3, otherwise the more the better",
142 {
"min_strips",
"Enforce min strips/frame, more is worse and faster, must be <= max_strips",
144 {
"strip_number_adaptivity",
"How fast the strip number adapts, more is slightly better, much slower",
159 int x, mb_count, strip_buf_size, frame_buf_size;
162 av_log(avctx,
AV_LOG_ERROR,
"width and height must be multiples of four (got %ix%i)\n",
167 if (
s->min_min_strips >
s->max_max_strips) {
168 av_log(avctx,
AV_LOG_ERROR,
"minimum number of strips must not exceed maximum (got %i and %i)\n",
169 s->min_min_strips,
s->max_max_strips);
203 if (!(
s->strip_buf =
av_malloc(strip_buf_size)))
206 if (!(
s->frame_buf =
av_malloc(frame_buf_size)))
216 s->frame_buf_size = frame_buf_size;
221 s->last_frame->data[0] =
s->pict_bufs[0];
222 s->last_frame->linesize[0] =
s->w;
223 s->best_frame->data[0] =
s->pict_bufs[1];
224 s->best_frame->linesize[0] =
s->w;
225 s->scratch_frame->data[0] =
s->pict_bufs[2];
226 s->scratch_frame->linesize[0] =
s->w;
229 s->last_frame->data[1] =
s->last_frame->data[0] +
s->w *
s->h;
230 s->last_frame->data[2] =
s->last_frame->data[1] + ((
s->w *
s->h) >> 2);
231 s->last_frame->linesize[1] =
232 s->last_frame->linesize[2] =
s->w >> 1;
234 s->best_frame->data[1] =
s->best_frame->data[0] +
s->w *
s->h;
235 s->best_frame->data[2] =
s->best_frame->data[1] + ((
s->w *
s->h) >> 2);
236 s->best_frame->linesize[1] =
237 s->best_frame->linesize[2] =
s->w >> 1;
239 s->scratch_frame->data[1] =
s->scratch_frame->data[0] +
s->w *
s->h;
240 s->scratch_frame->data[2] =
s->scratch_frame->data[1] + ((
s->w *
s->h) >> 2);
241 s->scratch_frame->linesize[1] =
242 s->scratch_frame->linesize[2] =
s->w >> 1;
244 s->input_frame->data[0] =
s->pict_bufs[3];
245 s->input_frame->linesize[0] =
s->w;
246 s->input_frame->data[1] =
s->input_frame->data[0] +
s->w *
s->h;
247 s->input_frame->data[2] =
s->input_frame->data[1] + ((
s->w *
s->h) >> 2);
248 s->input_frame->linesize[1] =
249 s->input_frame->linesize[2] =
s->w >> 1;
252 s->min_strips =
s->min_min_strips;
253 s->max_strips =
s->max_max_strips;
260 int *training_set_v1_shrunk,
261 int *training_set_v4_shrunk)
268 int64_t score1, score2, score3;
273 switch (
info->mode) {
276 ret +=
s->lambda * 8 * mb_count;
279 for (x = 0; x < mb_count; x++) {
292 *training_set_v1_shrunk = *training_set_v4_shrunk = 0;
293 for (x = 0; x < mb_count; x++) {
303 for (x = 0; x < mb_count; x++) {
308 if (score1 <= score2) {
322 int v1_shrunk = 0, v4_shrunk = 0;
323 for (x = 0; x < mb_count; x++) {
330 }
else if (
mb->best_encoding ==
ENC_V1) {
348 *training_set_v1_shrunk = v1_shrunk;
349 *training_set_v4_shrunk = v4_shrunk;
351 for (x = 0; x < mb_count; x++) {
357 if (score1 <= score2 && score1 <= score3) {
360 }
else if (score2 <= score3) {
384 int chunk_type_yuv,
int chunk_type_gray,
388 int incremental_codebook_replacement_mode = 0;
393 chunk_type_yuv + (incremental_codebook_replacement_mode ? 1 : 0) :
394 chunk_type_gray + (incremental_codebook_replacement_mode ? 1 : 0),
396 (incremental_codebook_replacement_mode ? (
size + 31) / 32 * 4 : 0));
403 if (incremental_codebook_replacement_mode) {
406 for (x = 0; x <
size; x++) {
413 for (y = 0; y < entry_size; y++)
414 buf[
ret++] =
codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
415 if ((
flags & 0xffffffff) == 0xffffffff) {
423 for (x = 0; x <
size; x++)
424 for (y = 0; y < entry_size; y++)
425 buf[
ret++] =
codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
432 uint8_t *
const in_data[4],
const int in_linesize[4],
433 uint8_t *out_data[4],
int out_linesize[4])
435 out_data[0] = in_data[0] + x + y * in_linesize[0];
436 out_linesize[0] = in_linesize[0];
439 out_data[1] = in_data[1] + (x >> 1) + (y >> 1) * in_linesize[1];
440 out_linesize[1] = in_linesize[1];
442 out_data[2] = in_data[2] + (x >> 1) + (y >> 1) * in_linesize[2];
443 out_linesize[2] = in_linesize[2];
455 data[0][ linesize[0]] =
456 data[0][1 + linesize[0]] =
info->v1_codebook[v1_vector * entry_size];
460 data[0][2 + linesize[0]] =
461 data[0][3 + linesize[0]] =
info->v1_codebook[v1_vector * entry_size + 1];
463 data[0][ 2 * linesize[0]] =
464 data[0][1 + 2 * linesize[0]] =
465 data[0][ 3 * linesize[0]] =
466 data[0][1 + 3 * linesize[0]] =
info->v1_codebook[v1_vector * entry_size + 2];
468 data[0][2 + 2 * linesize[0]] =
469 data[0][3 + 2 * linesize[0]] =
470 data[0][2 + 3 * linesize[0]] =
471 data[0][3 + 3 * linesize[0]] =
info->v1_codebook[v1_vector * entry_size + 3];
476 data[1][ linesize[1]] =
477 data[1][1 + linesize[1]] =
info->v1_codebook[v1_vector * entry_size + 4];
481 data[2][ linesize[2]] =
482 data[2][1 + linesize[2]] =
info->v1_codebook[v1_vector * entry_size + 5];
492 for (
i = y = 0; y < 4; y += 2) {
493 for (x = 0; x < 4; x += 2,
i++) {
494 data[0][x + y * linesize[0]] =
info->v4_codebook[v4_vector[
i] * entry_size];
495 data[0][x + 1 + y * linesize[0]] =
info->v4_codebook[v4_vector[
i] * entry_size + 1];
496 data[0][x + (y + 1) * linesize[0]] =
info->v4_codebook[v4_vector[
i] * entry_size + 2];
497 data[0][x + 1 + (y + 1) * linesize[0]] =
info->v4_codebook[v4_vector[
i] * entry_size + 3];
500 data[1][(x >> 1) + (y >> 1) * linesize[1]] =
info->v4_codebook[v4_vector[
i] * entry_size + 4];
501 data[2][(x >> 1) + (y >> 1) * linesize[2]] =
info->v4_codebook[v4_vector[
i] * entry_size + 5];
508 uint8_t *a_data[4],
int a_linesize[4],
509 uint8_t *b_data[4],
int b_linesize[4])
514 memcpy(a_data[0] + y * a_linesize[0], b_data[0] + y * b_linesize[0],
518 for (p = 1; p <= 2; p++)
519 for (y = 0; y <
MB_SIZE / 2; y++)
520 memcpy(a_data[p] + y * a_linesize[p],
521 b_data[p] + y * b_linesize[p],
527 uint8_t *scratch_data[4],
int scratch_linesize[4],
528 uint8_t *last_data[4],
int last_linesize[4],
531 int x, y, z,
bits, temp_size, header_ofs,
ret = 0, mb_count =
s->w *
h /
MB_AREA;
532 int needs_extra_bit, should_write_temp;
534 unsigned char temp[64];
536 uint8_t *sub_scratch_data[4] = { 0 }, *sub_last_data[4] = { 0 };
537 int sub_scratch_linesize[4] = { 0 }, sub_last_linesize[4] = { 0 };
543 if (
info->v4_size || !
s->skip_empty_cb)
546 if (
info->v1_size || !
s->skip_empty_cb)
550 for (z = y = 0; y <
h; y +=
MB_SIZE)
551 for (x = 0; x <
s->w; x +=
MB_SIZE, z++) {
555 sub_scratch_data, sub_scratch_linesize);
559 sub_last_data, sub_last_linesize);
560 copy_mb(
s, sub_scratch_data, sub_scratch_linesize,
561 sub_last_data, sub_last_linesize);
570 switch (
info->mode) {
574 for (x = 0; x < mb_count; x++)
575 buf[
ret++] =
s->mb[x].v1_vector;
583 for (x = 0; x < mb_count; x += 32) {
585 for (y = x; y <
FFMIN(x + 32, mb_count); y++)
586 if (
s->mb[y].best_encoding ==
ENC_V4)
587 flags |= 1
U << (31 - y + x);
592 for (y = x; y <
FFMIN(x + 32, mb_count); y++) {
596 buf[
ret++] =
mb->v1_vector;
598 for (z = 0; z < 4; z++)
599 buf[
ret++] =
mb->v4_vector[z];
612 for (x = 0; x < mb_count; x++) {
616 should_write_temp = 0;
630 if (
mb->best_encoding ==
ENC_SKIP || needs_extra_bit) {
631 memcpy(&buf[
ret],
temp, temp_size);
635 should_write_temp = 1;
638 if (needs_extra_bit) {
644 temp[temp_size++] =
mb->v1_vector;
645 else if (
mb->best_encoding ==
ENC_V4)
646 for (z = 0; z < 4; z++)
647 temp[temp_size++] =
mb->v4_vector[z];
649 if (should_write_temp) {
650 memcpy(&buf[
ret],
temp, temp_size);
659 memcpy(&buf[
ret],
temp, temp_size);
673 uint8_t *a_data[4],
int a_linesize[4],
674 uint8_t *b_data[4],
int b_linesize[4])
676 int x, y, p,
d,
ret = 0;
679 for (x = 0; x <
MB_SIZE; x++) {
680 d = a_data[0][x + y * a_linesize[0]] - b_data[0][x + y * b_linesize[0]];
685 for (p = 1; p <= 2; p++) {
686 for (y = 0; y <
MB_SIZE / 2; y++)
687 for (x = 0; x <
MB_SIZE / 2; x++) {
688 d = a_data[p][x + y * a_linesize[p]] - b_data[p][x + y * b_linesize[p]];
698 #define CERTAIN(x) ((x) != ENC_UNCERTAIN)
703 int x, y,
i, j, k, x2, y2, x3, y3, plane,
shift, mbn;
707 uint8_t vq_pict_buf[(
MB_AREA * 3) / 2];
708 uint8_t *sub_data[4], *vq_data[4];
709 int sub_linesize[4], vq_linesize[4];
712 for (mbn =
i = y = 0; y <
h; y +=
MB_SIZE) {
713 for (x = 0; x <
s->w; x +=
MB_SIZE, ++mbn) {
718 if (
s->mb[mbn].best_encoding != encoding)
722 base =
s->codebook_input +
i * entry_size;
725 for (j = y2 = 0; y2 < entry_size; y2 += 2)
726 for (x2 = 0; x2 < 4; x2 += 2, j++) {
727 plane = y2 < 4 ? 0 : 1 + (x2 >> 1);
728 shift = y2 < 4 ? 0 : 1;
732 data[plane][((x + x3) >>
shift) + 1 + ((y + y3) >>
shift) * linesize[plane]] +
733 data[plane][((x + x3) >>
shift) + (((y + y3) >>
shift) + 1) * linesize[plane]] +
734 data[plane][((x + x3) >>
shift) + 1 + (((y + y3) >>
shift) + 1) * linesize[plane]]) >> 2;
738 for (j = y2 = 0; y2 <
MB_SIZE; y2 += 2) {
739 for (x2 = 0; x2 <
MB_SIZE; x2 += 2)
740 for (k = 0; k < entry_size; k++, j++) {
741 plane = k >= 4 ? k - 3 : 0;
747 x3 = x + x2 + (k & 1);
748 y3 = y + y2 + (k >> 1);
751 base[j] =
data[plane][x3 + y3 * linesize[plane]];
765 size, 1,
s->codebook_closest, &
s->randctx, 0);
770 vq_data[0] = vq_pict_buf;
772 vq_data[1] = &vq_pict_buf[
MB_AREA];
773 vq_data[2] = vq_data[1] + (
MB_AREA >> 2);
779 for (x = 0; x <
s->w; x +=
MB_SIZE, j++) {
782 if (
CERTAIN(encoding) &&
mb->best_encoding != encoding)
789 mb->v1_vector =
s->codebook_closest[
i];
795 vq_data, vq_linesize);
797 for (k = 0; k < 4; k++)
798 mb->v4_vector[k] =
s->codebook_closest[
i + k];
804 vq_data, vq_linesize);
815 uint8_t *last_data[4],
int last_linesize[4],
816 uint8_t *
data[4],
int linesize[4],
820 uint8_t *sub_last_data [4], *sub_pict_data [4];
821 int sub_last_linesize[4], sub_pict_linesize[4];
824 for (x = 0; x <
s->w; x +=
MB_SIZE,
i++) {
826 sub_last_data, sub_last_linesize);
828 sub_pict_data, sub_pict_linesize);
830 s->mb[
i].skip_error =
832 sub_last_data, sub_last_linesize,
833 sub_pict_data, sub_pict_linesize);
844 buf[0] = keyframe ? 0x10 : 0x11;
848 unsigned char *buf,
int strip_size)
861 uint8_t *last_data[4],
int last_linesize[4],
862 uint8_t *
data[4],
int linesize[4],
863 uint8_t *scratch_data[4],
int scratch_linesize[4],
864 unsigned char *buf, int64_t *best_score,
int *no_skip)
870 int v1enough, v1_size, v4enough, v4_size;
871 int new_v1_size, new_v4_size;
872 int v1shrunk, v4shrunk;
882 #define SMALLEST_CODEBOOK 1
884 for (v4enough = 0, v4_size = 0; v4_size <= v1_size && !v4enough; v4_size = v4_size ? v4_size << 2 : v1_size >= SMALLEST_CODEBOOK << 2 ? v1_size >> 2 :
SMALLEST_CODEBOOK) {
893 info.v1_size = v1_size;
898 info.v1_size = new_v1_size;
899 if (
info.v1_size < v1_size)
910 info.v4_size = v4_size;
915 info.v4_size = new_v4_size;
916 if (
info.v4_size < v4_size)
925 &v1shrunk, &v4shrunk);
928 int extra_iterations_limit =
s->max_extra_cb_iterations;
931 info.v1_size = v1_size;
935 if (new_v1_size <
info.v1_size)
936 info.v1_size = new_v1_size;
938 info.v4_size = v4_size;
942 if (new_v4_size <
info.v4_size)
943 info.v4_size = new_v4_size;
950 &v1shrunk, &v4shrunk);
952 if ((!v1shrunk && !v4shrunk) || !extra_iterations_limit--)
956 info.v1_size = v1_size;
960 if (new_v1_size <
info.v1_size)
961 info.v1_size = new_v1_size;
964 info.v4_size = v4_size;
968 if (new_v4_size <
info.v4_size)
969 info.v4_size = new_v4_size;
974 if (best_size == 0 || score < *best_score) {
977 scratch_data, scratch_linesize,
978 last_data, last_linesize, &
info,
991 memcpy(buf,
s->strip_buf, best_size);
997 int num_strips,
int data_size,
int isakeyframe)
999 buf[0] = isakeyframe ? 0 : 1;
1009 int isakeyframe,
unsigned char *buf,
int buf_size,
int *got_keyframe)
1011 int num_strips, strip,
i, y, nexty,
size, temp_size, best_size;
1012 uint8_t *last_data [4], *
data [4], *scratch_data [4];
1013 int last_linesize[4], linesize[4], scratch_linesize[4];
1014 int64_t best_score = 0, score, score_temp;
1015 int best_nstrips, best_strip_offsets[
MAX_STRIPS];
1020 for (y = 0; y <
s->h; y += 2)
1021 for (x = 0; x <
s->w; x += 2) {
1022 const uint8_t *ir[2];
1027 s->input_frame->data,
s->input_frame->linesize,
1028 scratch_data, scratch_linesize);
1030 for (
i = 0;
i < 4; ++
i) {
1034 rr = ir[i2][i1 * 3 + 0];
1035 gg = ir[i2][i1 * 3 + 1];
1036 bb = ir[i2][i1 * 3 + 2];
1043 rr = (2396625 * rr + 4793251 * gg + 1198732 * bb) >> 23;
1048 scratch_data[0][i1 + i2 * scratch_linesize[0]] = rr;
1054 rr = (-299683 *
r - 599156 *
g + 898839 *
b) >> 23;
1059 scratch_data[1][0] = rr + 128;
1062 rr = (748893 *
r - 599156 *
g - 149737 *
b) >> 23;
1067 scratch_data[2][0] = rr + 128;
1073 for (num_strips =
s->min_strips; num_strips <= s->max_strips && num_strips <= s->
h /
MB_SIZE; num_strips++) {
1075 int all_no_skip = 1;
1079 for (y = 0, strip = 1; y <
s->h; strip++, y = nexty) {
1080 int strip_height, no_skip;
1083 nexty = strip *
s->h / num_strips;
1086 nexty += 4 - (nexty & 3);
1088 strip_height = nexty - y;
1089 if (strip_height <= 0) {
1090 av_log(
s->avctx,
AV_LOG_INFO,
"skipping zero height strip %i of %i\n", strip, num_strips);
1096 s->input_frame->data,
s->input_frame->linesize,
1103 s->last_frame->data,
s->last_frame->linesize,
1104 last_data, last_linesize);
1106 s->scratch_frame->data,
s->scratch_frame->linesize,
1107 scratch_data, scratch_linesize);
1109 if ((temp_size =
rd_strip(
s, y, strip_height, isakeyframe,
1110 last_data, last_linesize,
data, linesize,
1111 scratch_data, scratch_linesize,
1112 s->frame_buf + strip_offsets[strip-1],
1113 &score_temp, &no_skip)) < 0)
1116 score += score_temp;
1118 all_no_skip &= no_skip;
1121 if (best_score == 0 || score < best_score) {
1126 memcpy(buf,
s->frame_buf, best_size);
1127 best_nstrips = num_strips;
1128 *got_keyframe = all_no_skip;
1129 memcpy(best_strip_offsets, strip_offsets,
sizeof(strip_offsets));
1133 if (num_strips - best_nstrips > 4)
1138 for (
i = 0;
i < best_nstrips;
i++) {
1145 if (!
s->strip_number_delta_range) {
1146 if (best_nstrips ==
s->max_strips) {
1147 s->max_strips = best_nstrips + 1;
1148 if (
s->max_strips >=
s->max_max_strips)
1149 s->max_strips =
s->max_max_strips;
1151 s->max_strips = best_nstrips;
1153 s->min_strips =
s->max_strips - 1;
1154 if (
s->min_strips <
s->min_min_strips)
1155 s->min_strips =
s->min_min_strips;
1157 s->max_strips = best_nstrips +
s->strip_number_delta_range;
1158 if (
s->max_strips >=
s->max_max_strips)
1159 s->max_strips =
s->max_max_strips;
1160 s->min_strips = best_nstrips -
s->strip_number_delta_range;
1161 if (
s->min_strips <
s->min_min_strips)
1162 s->min_strips =
s->min_min_strips;
1172 int ret, got_keyframe;
1218 .
p.
name =
"cinepak",