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;
222 s->last_frame->data[0] =
s->pict_bufs[0];
223 s->last_frame->linesize[0] =
s->w;
224 s->best_frame->data[0] =
s->pict_bufs[1];
225 s->best_frame->linesize[0] =
s->w;
226 s->scratch_frame->data[0] =
s->pict_bufs[2];
227 s->scratch_frame->linesize[0] =
s->w;
230 s->last_frame->data[1] =
s->last_frame->data[0] +
s->w *
s->h;
231 s->last_frame->data[2] =
s->last_frame->data[1] + ((
s->w *
s->h) >> 2);
232 s->last_frame->linesize[1] =
233 s->last_frame->linesize[2] =
s->w >> 1;
235 s->best_frame->data[1] =
s->best_frame->data[0] +
s->w *
s->h;
236 s->best_frame->data[2] =
s->best_frame->data[1] + ((
s->w *
s->h) >> 2);
237 s->best_frame->linesize[1] =
238 s->best_frame->linesize[2] =
s->w >> 1;
240 s->scratch_frame->data[1] =
s->scratch_frame->data[0] +
s->w *
s->h;
241 s->scratch_frame->data[2] =
s->scratch_frame->data[1] + ((
s->w *
s->h) >> 2);
242 s->scratch_frame->linesize[1] =
243 s->scratch_frame->linesize[2] =
s->w >> 1;
245 s->input_frame->data[0] =
s->pict_bufs[3];
246 s->input_frame->linesize[0] =
s->w;
247 s->input_frame->data[1] =
s->input_frame->data[0] +
s->w *
s->h;
248 s->input_frame->data[2] =
s->input_frame->data[1] + ((
s->w *
s->h) >> 2);
249 s->input_frame->linesize[1] =
250 s->input_frame->linesize[2] =
s->w >> 1;
253 s->min_strips =
s->min_min_strips;
254 s->max_strips =
s->max_max_strips;
261 int *training_set_v1_shrunk,
262 int *training_set_v4_shrunk)
269 int64_t score1, score2, score3;
274 switch (
info->mode) {
277 ret +=
s->lambda * 8 * mb_count;
280 for (x = 0; x < mb_count; x++) {
293 *training_set_v1_shrunk = *training_set_v4_shrunk = 0;
294 for (x = 0; x < mb_count; x++) {
304 for (x = 0; x < mb_count; x++) {
309 if (score1 <= score2) {
323 int v1_shrunk = 0, v4_shrunk = 0;
324 for (x = 0; x < mb_count; x++) {
331 }
else if (
mb->best_encoding ==
ENC_V1) {
349 *training_set_v1_shrunk = v1_shrunk;
350 *training_set_v4_shrunk = v4_shrunk;
352 for (x = 0; x < mb_count; x++) {
358 if (score1 <= score2 && score1 <= score3) {
361 }
else if (score2 <= score3) {
385 int chunk_type_yuv,
int chunk_type_gray,
389 int incremental_codebook_replacement_mode = 0;
394 chunk_type_yuv + (incremental_codebook_replacement_mode ? 1 : 0) :
395 chunk_type_gray + (incremental_codebook_replacement_mode ? 1 : 0),
397 (incremental_codebook_replacement_mode ? (
size + 31) / 32 * 4 : 0));
404 if (incremental_codebook_replacement_mode) {
407 for (x = 0; x <
size; x++) {
414 for (y = 0; y < entry_size; y++)
415 buf[
ret++] =
codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
416 if ((
flags & 0xffffffff) == 0xffffffff) {
424 for (x = 0; x <
size; x++)
425 for (y = 0; y < entry_size; y++)
426 buf[
ret++] =
codebook[y + x * entry_size] ^ (y >= 4 ? 0x80 : 0);
433 uint8_t * in_data[4],
int in_linesize[4],
434 uint8_t *out_data[4],
int out_linesize[4])
436 out_data[0] = in_data[0] + x + y * in_linesize[0];
437 out_linesize[0] = in_linesize[0];
440 out_data[1] = in_data[1] + (x >> 1) + (y >> 1) * in_linesize[1];
441 out_linesize[1] = in_linesize[1];
443 out_data[2] = in_data[2] + (x >> 1) + (y >> 1) * in_linesize[2];
444 out_linesize[2] = in_linesize[2];
456 data[0][ linesize[0]] =
457 data[0][1 + linesize[0]] =
info->v1_codebook[v1_vector * entry_size];
461 data[0][2 + linesize[0]] =
462 data[0][3 + linesize[0]] =
info->v1_codebook[v1_vector * entry_size + 1];
464 data[0][ 2 * linesize[0]] =
465 data[0][1 + 2 * linesize[0]] =
466 data[0][ 3 * linesize[0]] =
467 data[0][1 + 3 * linesize[0]] =
info->v1_codebook[v1_vector * entry_size + 2];
469 data[0][2 + 2 * linesize[0]] =
470 data[0][3 + 2 * linesize[0]] =
471 data[0][2 + 3 * linesize[0]] =
472 data[0][3 + 3 * linesize[0]] =
info->v1_codebook[v1_vector * entry_size + 3];
477 data[1][ linesize[1]] =
478 data[1][1 + linesize[1]] =
info->v1_codebook[v1_vector * entry_size + 4];
482 data[2][ linesize[2]] =
483 data[2][1 + linesize[2]] =
info->v1_codebook[v1_vector * entry_size + 5];
493 for (
i = y = 0; y < 4; y += 2) {
494 for (x = 0; x < 4; x += 2,
i++) {
495 data[0][x + y * linesize[0]] =
info->v4_codebook[v4_vector[
i] * entry_size];
496 data[0][x + 1 + y * linesize[0]] =
info->v4_codebook[v4_vector[
i] * entry_size + 1];
497 data[0][x + (y + 1) * linesize[0]] =
info->v4_codebook[v4_vector[
i] * entry_size + 2];
498 data[0][x + 1 + (y + 1) * linesize[0]] =
info->v4_codebook[v4_vector[
i] * entry_size + 3];
501 data[1][(x >> 1) + (y >> 1) * linesize[1]] =
info->v4_codebook[v4_vector[
i] * entry_size + 4];
502 data[2][(x >> 1) + (y >> 1) * linesize[2]] =
info->v4_codebook[v4_vector[
i] * entry_size + 5];
509 uint8_t *a_data[4],
int a_linesize[4],
510 uint8_t *b_data[4],
int b_linesize[4])
515 memcpy(a_data[0] + y * a_linesize[0], b_data[0] + y * b_linesize[0],
519 for (p = 1; p <= 2; p++)
520 for (y = 0; y <
MB_SIZE / 2; y++)
521 memcpy(a_data[p] + y * a_linesize[p],
522 b_data[p] + y * b_linesize[p],
528 uint8_t *scratch_data[4],
int scratch_linesize[4],
529 uint8_t *last_data[4],
int last_linesize[4],
532 int x, y, z,
bits, temp_size, header_ofs,
ret = 0, mb_count =
s->w *
h /
MB_AREA;
533 int needs_extra_bit, should_write_temp;
535 unsigned char temp[64];
537 uint8_t *sub_scratch_data[4] = { 0 }, *sub_last_data[4] = { 0 };
538 int sub_scratch_linesize[4] = { 0 }, sub_last_linesize[4] = { 0 };
544 if (
info->v4_size || !
s->skip_empty_cb)
547 if (
info->v1_size || !
s->skip_empty_cb)
551 for (z = y = 0; y <
h; y +=
MB_SIZE)
552 for (x = 0; x <
s->w; x +=
MB_SIZE, z++) {
556 sub_scratch_data, sub_scratch_linesize);
560 sub_last_data, sub_last_linesize);
561 copy_mb(
s, sub_scratch_data, sub_scratch_linesize,
562 sub_last_data, sub_last_linesize);
571 switch (
info->mode) {
575 for (x = 0; x < mb_count; x++)
576 buf[
ret++] =
s->mb[x].v1_vector;
584 for (x = 0; x < mb_count; x += 32) {
586 for (y = x; y <
FFMIN(x + 32, mb_count); y++)
587 if (
s->mb[y].best_encoding ==
ENC_V4)
588 flags |= 1
U << (31 - y + x);
593 for (y = x; y <
FFMIN(x + 32, mb_count); y++) {
597 buf[
ret++] =
mb->v1_vector;
599 for (z = 0; z < 4; z++)
600 buf[
ret++] =
mb->v4_vector[z];
613 for (x = 0; x < mb_count; x++) {
617 should_write_temp = 0;
631 if (
mb->best_encoding ==
ENC_SKIP || needs_extra_bit) {
632 memcpy(&buf[
ret],
temp, temp_size);
636 should_write_temp = 1;
639 if (needs_extra_bit) {
645 temp[temp_size++] =
mb->v1_vector;
646 else if (
mb->best_encoding ==
ENC_V4)
647 for (z = 0; z < 4; z++)
648 temp[temp_size++] =
mb->v4_vector[z];
650 if (should_write_temp) {
651 memcpy(&buf[
ret],
temp, temp_size);
660 memcpy(&buf[
ret],
temp, temp_size);
674 uint8_t *a_data[4],
int a_linesize[4],
675 uint8_t *b_data[4],
int b_linesize[4])
677 int x, y, p,
d,
ret = 0;
680 for (x = 0; x <
MB_SIZE; x++) {
681 d = a_data[0][x + y * a_linesize[0]] - b_data[0][x + y * b_linesize[0]];
686 for (p = 1; p <= 2; p++) {
687 for (y = 0; y <
MB_SIZE / 2; y++)
688 for (x = 0; x <
MB_SIZE / 2; x++) {
689 d = a_data[p][x + y * a_linesize[p]] - b_data[p][x + y * b_linesize[p]];
699 #define CERTAIN(x) ((x) != ENC_UNCERTAIN)
704 int x, y,
i, j, k, x2, y2, x3, y3, plane,
shift, mbn;
708 uint8_t vq_pict_buf[(
MB_AREA * 3) / 2];
709 uint8_t *sub_data[4], *vq_data[4];
710 int sub_linesize[4], vq_linesize[4];
713 for (mbn =
i = y = 0; y <
h; y +=
MB_SIZE) {
714 for (x = 0; x <
s->w; x +=
MB_SIZE, ++mbn) {
719 if (
s->mb[mbn].best_encoding != encoding)
723 base =
s->codebook_input +
i * entry_size;
726 for (j = y2 = 0; y2 < entry_size; y2 += 2)
727 for (x2 = 0; x2 < 4; x2 += 2, j++) {
728 plane = y2 < 4 ? 0 : 1 + (x2 >> 1);
729 shift = y2 < 4 ? 0 : 1;
733 data[plane][((x + x3) >>
shift) + 1 + ((y + y3) >>
shift) * linesize[plane]] +
734 data[plane][((x + x3) >>
shift) + (((y + y3) >>
shift) + 1) * linesize[plane]] +
735 data[plane][((x + x3) >>
shift) + 1 + (((y + y3) >>
shift) + 1) * linesize[plane]]) >> 2;
739 for (j = y2 = 0; y2 <
MB_SIZE; y2 += 2) {
740 for (x2 = 0; x2 <
MB_SIZE; x2 += 2)
741 for (k = 0; k < entry_size; k++, j++) {
742 plane = k >= 4 ? k - 3 : 0;
748 x3 = x + x2 + (k & 1);
749 y3 = y + y2 + (k >> 1);
752 base[j] =
data[plane][x3 + y3 * linesize[plane]];
766 size, 1,
s->codebook_closest, &
s->randctx, 0);
771 vq_data[0] = vq_pict_buf;
773 vq_data[1] = &vq_pict_buf[
MB_AREA];
774 vq_data[2] = vq_data[1] + (
MB_AREA >> 2);
780 for (x = 0; x <
s->w; x +=
MB_SIZE, j++) {
783 if (
CERTAIN(encoding) &&
mb->best_encoding != encoding)
790 mb->v1_vector =
s->codebook_closest[
i];
796 vq_data, vq_linesize);
798 for (k = 0; k < 4; k++)
799 mb->v4_vector[k] =
s->codebook_closest[
i + k];
805 vq_data, vq_linesize);
816 uint8_t *last_data[4],
int last_linesize[4],
817 uint8_t *
data[4],
int linesize[4],
821 uint8_t *sub_last_data [4], *sub_pict_data [4];
822 int sub_last_linesize[4], sub_pict_linesize[4];
825 for (x = 0; x <
s->w; x +=
MB_SIZE,
i++) {
827 sub_last_data, sub_last_linesize);
829 sub_pict_data, sub_pict_linesize);
831 s->mb[
i].skip_error =
833 sub_last_data, sub_last_linesize,
834 sub_pict_data, sub_pict_linesize);
839 unsigned char *buf,
int strip_size)
846 buf[0] = keyframe ? 0x10 : 0x11;
857 uint8_t *last_data[4],
int last_linesize[4],
858 uint8_t *
data[4],
int linesize[4],
859 uint8_t *scratch_data[4],
int scratch_linesize[4],
860 unsigned char *buf, int64_t *best_score)
866 int v1enough, v1_size, v4enough, v4_size;
867 int new_v1_size, new_v4_size;
868 int v1shrunk, v4shrunk;
878 #define SMALLEST_CODEBOOK 1
880 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) {
889 info.v1_size = v1_size;
894 info.v1_size = new_v1_size;
895 if (
info.v1_size < v1_size)
906 info.v4_size = v4_size;
911 info.v4_size = new_v4_size;
912 if (
info.v4_size < v4_size)
921 &v1shrunk, &v4shrunk);
924 int extra_iterations_limit =
s->max_extra_cb_iterations;
927 info.v1_size = v1_size;
931 if (new_v1_size <
info.v1_size)
932 info.v1_size = new_v1_size;
934 info.v4_size = v4_size;
938 if (new_v4_size <
info.v4_size)
939 info.v4_size = new_v4_size;
946 &v1shrunk, &v4shrunk);
948 if ((!v1shrunk && !v4shrunk) || !extra_iterations_limit--)
952 info.v1_size = v1_size;
956 if (new_v1_size <
info.v1_size)
957 info.v1_size = new_v1_size;
960 info.v4_size = v4_size;
964 if (new_v4_size <
info.v4_size)
965 info.v4_size = new_v4_size;
970 if (best_size == 0 || score < *best_score) {
973 scratch_data, scratch_linesize,
974 last_data, last_linesize, &
info,
984 memcpy(buf,
s->strip_buf, best_size);
990 int num_strips,
int data_size,
int isakeyframe)
992 buf[0] = isakeyframe ? 0 : 1;
1002 int isakeyframe,
unsigned char *buf,
int buf_size)
1004 int num_strips, strip,
i, y, nexty,
size, temp_size, best_size;
1005 uint8_t *last_data [4], *
data [4], *scratch_data [4];
1006 int last_linesize[4], linesize[4], scratch_linesize[4];
1007 int64_t best_score = 0, score, score_temp;
1013 for (y = 0; y <
s->h; y += 2)
1014 for (x = 0; x <
s->w; x += 2) {
1017 ir[0] =
frame->data[0] + x * 3 + y *
frame->linesize[0];
1018 ir[1] = ir[0] +
frame->linesize[0];
1020 s->input_frame->data,
s->input_frame->linesize,
1021 scratch_data, scratch_linesize);
1023 for (
i = 0;
i < 4; ++
i) {
1027 rr = ir[i2][i1 * 3 + 0];
1028 gg = ir[i2][i1 * 3 + 1];
1029 bb = ir[i2][i1 * 3 + 2];
1036 rr = (2396625 * rr + 4793251 * gg + 1198732 * bb) >> 23;
1041 scratch_data[0][i1 + i2 * scratch_linesize[0]] = rr;
1047 rr = (-299683 *
r - 599156 *
g + 898839 *
b) >> 23;
1052 scratch_data[1][0] = rr + 128;
1055 rr = (748893 *
r - 599156 *
g - 149737 *
b) >> 23;
1060 scratch_data[2][0] = rr + 128;
1066 for (num_strips =
s->min_strips; num_strips <= s->max_strips && num_strips <= s->
h /
MB_SIZE; num_strips++) {
1070 for (y = 0, strip = 1; y <
s->h; strip++, y = nexty) {
1073 nexty = strip *
s->h / num_strips;
1076 nexty += 4 - (nexty & 3);
1078 strip_height = nexty - y;
1079 if (strip_height <= 0) {
1080 av_log(
s->avctx,
AV_LOG_INFO,
"skipping zero height strip %i of %i\n", strip, num_strips);
1086 s->input_frame->data,
s->input_frame->linesize,
1090 (uint8_t **)
frame->data, (
int *)
frame->linesize,
1093 s->last_frame->data,
s->last_frame->linesize,
1094 last_data, last_linesize);
1096 s->scratch_frame->data,
s->scratch_frame->linesize,
1097 scratch_data, scratch_linesize);
1099 if ((temp_size =
rd_strip(
s, y, strip_height, isakeyframe,
1100 last_data, last_linesize,
data, linesize,
1101 scratch_data, scratch_linesize,
1106 score += score_temp;
1110 if (best_score == 0 || score < best_score) {
1115 memcpy(buf,
s->frame_buf, best_size);
1116 best_nstrips = num_strips;
1120 if (num_strips - best_nstrips > 4)
1127 if (!
s->strip_number_delta_range) {
1128 if (best_nstrips ==
s->max_strips) {
1129 s->max_strips = best_nstrips + 1;
1130 if (
s->max_strips >=
s->max_max_strips)
1131 s->max_strips =
s->max_max_strips;
1133 s->max_strips = best_nstrips;
1135 s->min_strips =
s->max_strips - 1;
1136 if (
s->min_strips <
s->min_min_strips)
1137 s->min_strips =
s->min_min_strips;
1139 s->max_strips = best_nstrips +
s->strip_number_delta_range;
1140 if (
s->max_strips >=
s->max_max_strips)
1141 s->max_strips =
s->max_max_strips;
1142 s->min_strips = best_nstrips -
s->strip_number_delta_range;
1143 if (
s->min_strips <
s->min_min_strips)
1144 s->min_strips =
s->min_min_strips;
1162 if (
s->curframe == 0)
1168 if (++
s->curframe >=
s->keyint)