You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

632 lines
24KB

  1. /*
  2. * SVQ1 Encoder
  3. * Copyright (C) 2004 Mike Melanson <melanson@pcisys.net>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Sorenson Vector Quantizer #1 (SVQ1) video codec.
  24. * For more information of the SVQ1 algorithm, visit:
  25. * http://www.pcisys.net/~melanson/codecs/
  26. */
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include "h263.h"
  30. #include "internal.h"
  31. #include "libavutil/avassert.h"
  32. #include "svq1.h"
  33. #include "svq1enc_cb.h"
  34. typedef struct SVQ1Context {
  35. /* FIXME: Needed for motion estimation, should not be used for anything
  36. * else, the idea is to make the motion estimation eventually independent
  37. * of MpegEncContext, so this will be removed then. */
  38. MpegEncContext m;
  39. AVCodecContext *avctx;
  40. DSPContext dsp;
  41. AVFrame picture;
  42. AVFrame current_picture;
  43. AVFrame last_picture;
  44. PutBitContext pb;
  45. GetBitContext gb;
  46. /* why ooh why this sick breadth first order,
  47. * everything is slower and more complex */
  48. PutBitContext reorder_pb[6];
  49. int frame_width;
  50. int frame_height;
  51. /* Y plane block dimensions */
  52. int y_block_width;
  53. int y_block_height;
  54. /* U & V plane (C planes) block dimensions */
  55. int c_block_width;
  56. int c_block_height;
  57. uint16_t *mb_type;
  58. uint32_t *dummy;
  59. int16_t (*motion_val8[3])[2];
  60. int16_t (*motion_val16[3])[2];
  61. int64_t rd_total;
  62. uint8_t *scratchbuf;
  63. } SVQ1Context;
  64. static void svq1_write_header(SVQ1Context *s, int frame_type)
  65. {
  66. int i;
  67. /* frame code */
  68. put_bits(&s->pb, 22, 0x20);
  69. /* temporal reference (sure hope this is a "don't care") */
  70. put_bits(&s->pb, 8, 0x00);
  71. /* frame type */
  72. put_bits(&s->pb, 2, frame_type - 1);
  73. if (frame_type == AV_PICTURE_TYPE_I) {
  74. /* no checksum since frame code is 0x20 */
  75. /* no embedded string either */
  76. /* output 5 unknown bits (2 + 2 + 1) */
  77. put_bits(&s->pb, 5, 2); /* 2 needed by quicktime decoder */
  78. i = ff_match_2uint16((void*)ff_svq1_frame_size_table,
  79. FF_ARRAY_ELEMS(ff_svq1_frame_size_table),
  80. s->frame_width, s->frame_height);
  81. put_bits(&s->pb, 3, i);
  82. if (i == 7) {
  83. put_bits(&s->pb, 12, s->frame_width);
  84. put_bits(&s->pb, 12, s->frame_height);
  85. }
  86. }
  87. /* no checksum or extra data (next 2 bits get 0) */
  88. put_bits(&s->pb, 2, 0);
  89. }
  90. #define QUALITY_THRESHOLD 100
  91. #define THRESHOLD_MULTIPLIER 0.6
  92. static int encode_block(SVQ1Context *s, uint8_t *src, uint8_t *ref,
  93. uint8_t *decoded, int stride, int level,
  94. int threshold, int lambda, int intra)
  95. {
  96. int count, y, x, i, j, split, best_mean, best_score, best_count;
  97. int best_vector[6];
  98. int block_sum[7] = { 0, 0, 0, 0, 0, 0 };
  99. int w = 2 << (level + 2 >> 1);
  100. int h = 2 << (level + 1 >> 1);
  101. int size = w * h;
  102. int16_t block[7][256];
  103. const int8_t *codebook_sum, *codebook;
  104. const uint16_t(*mean_vlc)[2];
  105. const uint8_t(*multistage_vlc)[2];
  106. best_score = 0;
  107. // FIXME: Optimize, this does not need to be done multiple times.
  108. if (intra) {
  109. codebook_sum = svq1_intra_codebook_sum[level];
  110. codebook = ff_svq1_intra_codebooks[level];
  111. mean_vlc = ff_svq1_intra_mean_vlc;
  112. multistage_vlc = ff_svq1_intra_multistage_vlc[level];
  113. for (y = 0; y < h; y++) {
  114. for (x = 0; x < w; x++) {
  115. int v = src[x + y * stride];
  116. block[0][x + w * y] = v;
  117. best_score += v * v;
  118. block_sum[0] += v;
  119. }
  120. }
  121. } else {
  122. codebook_sum = svq1_inter_codebook_sum[level];
  123. codebook = ff_svq1_inter_codebooks[level];
  124. mean_vlc = ff_svq1_inter_mean_vlc + 256;
  125. multistage_vlc = ff_svq1_inter_multistage_vlc[level];
  126. for (y = 0; y < h; y++) {
  127. for (x = 0; x < w; x++) {
  128. int v = src[x + y * stride] - ref[x + y * stride];
  129. block[0][x + w * y] = v;
  130. best_score += v * v;
  131. block_sum[0] += v;
  132. }
  133. }
  134. }
  135. best_count = 0;
  136. best_score -= (int)((unsigned)block_sum[0] * block_sum[0] >> (level + 3));
  137. best_mean = block_sum[0] + (size >> 1) >> (level + 3);
  138. if (level < 4) {
  139. for (count = 1; count < 7; count++) {
  140. int best_vector_score = INT_MAX;
  141. int best_vector_sum = -999, best_vector_mean = -999;
  142. const int stage = count - 1;
  143. const int8_t *vector;
  144. for (i = 0; i < 16; i++) {
  145. int sum = codebook_sum[stage * 16 + i];
  146. int sqr, diff, score;
  147. vector = codebook + stage * size * 16 + i * size;
  148. sqr = s->dsp.ssd_int8_vs_int16(vector, block[stage], size);
  149. diff = block_sum[stage] - sum;
  150. score = sqr - (diff * (int64_t)diff >> (level + 3)); // FIXME: 64bit slooow
  151. if (score < best_vector_score) {
  152. int mean = diff + (size >> 1) >> (level + 3);
  153. av_assert2(mean > -300 && mean < 300);
  154. mean = av_clip(mean, intra ? 0 : -256, 255);
  155. best_vector_score = score;
  156. best_vector[stage] = i;
  157. best_vector_sum = sum;
  158. best_vector_mean = mean;
  159. }
  160. }
  161. av_assert0(best_vector_mean != -999);
  162. vector = codebook + stage * size * 16 + best_vector[stage] * size;
  163. for (j = 0; j < size; j++)
  164. block[stage + 1][j] = block[stage][j] - vector[j];
  165. block_sum[stage + 1] = block_sum[stage] - best_vector_sum;
  166. best_vector_score += lambda *
  167. (+1 + 4 * count +
  168. multistage_vlc[1 + count][1]
  169. + mean_vlc[best_vector_mean][1]);
  170. if (best_vector_score < best_score) {
  171. best_score = best_vector_score;
  172. best_count = count;
  173. best_mean = best_vector_mean;
  174. }
  175. }
  176. }
  177. split = 0;
  178. if (best_score > threshold && level) {
  179. int score = 0;
  180. int offset = level & 1 ? stride * h / 2 : w / 2;
  181. PutBitContext backup[6];
  182. for (i = level - 1; i >= 0; i--)
  183. backup[i] = s->reorder_pb[i];
  184. score += encode_block(s, src, ref, decoded, stride, level - 1,
  185. threshold >> 1, lambda, intra);
  186. score += encode_block(s, src + offset, ref + offset, decoded + offset,
  187. stride, level - 1, threshold >> 1, lambda, intra);
  188. score += lambda;
  189. if (score < best_score) {
  190. best_score = score;
  191. split = 1;
  192. } else {
  193. for (i = level - 1; i >= 0; i--)
  194. s->reorder_pb[i] = backup[i];
  195. }
  196. }
  197. if (level > 0)
  198. put_bits(&s->reorder_pb[level], 1, split);
  199. if (!split) {
  200. av_assert1(best_mean >= 0 && best_mean < 256 || !intra);
  201. av_assert1(best_mean >= -256 && best_mean < 256);
  202. av_assert1(best_count >= 0 && best_count < 7);
  203. av_assert1(level < 4 || best_count == 0);
  204. /* output the encoding */
  205. put_bits(&s->reorder_pb[level],
  206. multistage_vlc[1 + best_count][1],
  207. multistage_vlc[1 + best_count][0]);
  208. put_bits(&s->reorder_pb[level], mean_vlc[best_mean][1],
  209. mean_vlc[best_mean][0]);
  210. for (i = 0; i < best_count; i++) {
  211. av_assert2(best_vector[i] >= 0 && best_vector[i] < 16);
  212. put_bits(&s->reorder_pb[level], 4, best_vector[i]);
  213. }
  214. for (y = 0; y < h; y++)
  215. for (x = 0; x < w; x++)
  216. decoded[x + y * stride] = src[x + y * stride] -
  217. block[best_count][x + w * y] +
  218. best_mean;
  219. }
  220. return best_score;
  221. }
  222. static int svq1_encode_plane(SVQ1Context *s, int plane,
  223. unsigned char *src_plane,
  224. unsigned char *ref_plane,
  225. unsigned char *decoded_plane,
  226. int width, int height, int src_stride, int stride)
  227. {
  228. int x, y;
  229. int i;
  230. int block_width, block_height;
  231. int level;
  232. int threshold[6];
  233. uint8_t *src = s->scratchbuf + stride * 16;
  234. const int lambda = (s->picture.quality * s->picture.quality) >>
  235. (2 * FF_LAMBDA_SHIFT);
  236. /* figure out the acceptable level thresholds in advance */
  237. threshold[5] = QUALITY_THRESHOLD;
  238. for (level = 4; level >= 0; level--)
  239. threshold[level] = threshold[level + 1] * THRESHOLD_MULTIPLIER;
  240. block_width = (width + 15) / 16;
  241. block_height = (height + 15) / 16;
  242. if (s->picture.pict_type == AV_PICTURE_TYPE_P) {
  243. s->m.avctx = s->avctx;
  244. s->m.current_picture_ptr = &s->m.current_picture;
  245. s->m.last_picture_ptr = &s->m.last_picture;
  246. s->m.last_picture.f.data[0] = ref_plane;
  247. s->m.linesize =
  248. s->m.last_picture.f.linesize[0] =
  249. s->m.new_picture.f.linesize[0] =
  250. s->m.current_picture.f.linesize[0] = stride;
  251. s->m.width = width;
  252. s->m.height = height;
  253. s->m.mb_width = block_width;
  254. s->m.mb_height = block_height;
  255. s->m.mb_stride = s->m.mb_width + 1;
  256. s->m.b8_stride = 2 * s->m.mb_width + 1;
  257. s->m.f_code = 1;
  258. s->m.pict_type = s->picture.pict_type;
  259. s->m.me_method = s->avctx->me_method;
  260. s->m.me.scene_change_score = 0;
  261. s->m.flags = s->avctx->flags;
  262. // s->m.out_format = FMT_H263;
  263. // s->m.unrestricted_mv = 1;
  264. s->m.lambda = s->picture.quality;
  265. s->m.qscale = s->m.lambda * 139 +
  266. FF_LAMBDA_SCALE * 64 >>
  267. FF_LAMBDA_SHIFT + 7;
  268. s->m.lambda2 = s->m.lambda * s->m.lambda +
  269. FF_LAMBDA_SCALE / 2 >>
  270. FF_LAMBDA_SHIFT;
  271. if (!s->motion_val8[plane]) {
  272. s->motion_val8[plane] = av_mallocz((s->m.b8_stride *
  273. block_height * 2 + 2) *
  274. 2 * sizeof(int16_t));
  275. s->motion_val16[plane] = av_mallocz((s->m.mb_stride *
  276. (block_height + 2) + 1) *
  277. 2 * sizeof(int16_t));
  278. }
  279. s->m.mb_type = s->mb_type;
  280. // dummies, to avoid segfaults
  281. s->m.current_picture.mb_mean = (uint8_t *)s->dummy;
  282. s->m.current_picture.mb_var = (uint16_t *)s->dummy;
  283. s->m.current_picture.mc_mb_var = (uint16_t *)s->dummy;
  284. s->m.current_picture.mb_type = s->dummy;
  285. s->m.current_picture.motion_val[0] = s->motion_val8[plane] + 2;
  286. s->m.p_mv_table = s->motion_val16[plane] +
  287. s->m.mb_stride + 1;
  288. s->m.dsp = s->dsp; // move
  289. ff_init_me(&s->m);
  290. s->m.me.dia_size = s->avctx->dia_size;
  291. s->m.first_slice_line = 1;
  292. for (y = 0; y < block_height; y++) {
  293. s->m.new_picture.f.data[0] = src - y * 16 * stride; // ugly
  294. s->m.mb_y = y;
  295. for (i = 0; i < 16 && i + 16 * y < height; i++) {
  296. memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
  297. width);
  298. for (x = width; x < 16 * block_width; x++)
  299. src[i * stride + x] = src[i * stride + x - 1];
  300. }
  301. for (; i < 16 && i + 16 * y < 16 * block_height; i++)
  302. memcpy(&src[i * stride], &src[(i - 1) * stride],
  303. 16 * block_width);
  304. for (x = 0; x < block_width; x++) {
  305. s->m.mb_x = x;
  306. ff_init_block_index(&s->m);
  307. ff_update_block_index(&s->m);
  308. ff_estimate_p_frame_motion(&s->m, x, y);
  309. }
  310. s->m.first_slice_line = 0;
  311. }
  312. ff_fix_long_p_mvs(&s->m);
  313. ff_fix_long_mvs(&s->m, NULL, 0, s->m.p_mv_table, s->m.f_code,
  314. CANDIDATE_MB_TYPE_INTER, 0);
  315. }
  316. s->m.first_slice_line = 1;
  317. for (y = 0; y < block_height; y++) {
  318. for (i = 0; i < 16 && i + 16 * y < height; i++) {
  319. memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
  320. width);
  321. for (x = width; x < 16 * block_width; x++)
  322. src[i * stride + x] = src[i * stride + x - 1];
  323. }
  324. for (; i < 16 && i + 16 * y < 16 * block_height; i++)
  325. memcpy(&src[i * stride], &src[(i - 1) * stride], 16 * block_width);
  326. s->m.mb_y = y;
  327. for (x = 0; x < block_width; x++) {
  328. uint8_t reorder_buffer[3][6][7 * 32];
  329. int count[3][6];
  330. int offset = y * 16 * stride + x * 16;
  331. uint8_t *decoded = decoded_plane + offset;
  332. uint8_t *ref = ref_plane + offset;
  333. int score[4] = { 0, 0, 0, 0 }, best;
  334. uint8_t *temp = s->scratchbuf;
  335. if (s->pb.buf_end - s->pb.buf -
  336. (put_bits_count(&s->pb) >> 3) < 3000) { // FIXME: check size
  337. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  338. return -1;
  339. }
  340. s->m.mb_x = x;
  341. ff_init_block_index(&s->m);
  342. ff_update_block_index(&s->m);
  343. if (s->picture.pict_type == AV_PICTURE_TYPE_I ||
  344. (s->m.mb_type[x + y * s->m.mb_stride] &
  345. CANDIDATE_MB_TYPE_INTRA)) {
  346. for (i = 0; i < 6; i++)
  347. init_put_bits(&s->reorder_pb[i], reorder_buffer[0][i],
  348. 7 * 32);
  349. if (s->picture.pict_type == AV_PICTURE_TYPE_P) {
  350. const uint8_t *vlc = ff_svq1_block_type_vlc[SVQ1_BLOCK_INTRA];
  351. put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
  352. score[0] = vlc[1] * lambda;
  353. }
  354. score[0] += encode_block(s, src + 16 * x, NULL, temp, stride,
  355. 5, 64, lambda, 1);
  356. for (i = 0; i < 6; i++) {
  357. count[0][i] = put_bits_count(&s->reorder_pb[i]);
  358. flush_put_bits(&s->reorder_pb[i]);
  359. }
  360. } else
  361. score[0] = INT_MAX;
  362. best = 0;
  363. if (s->picture.pict_type == AV_PICTURE_TYPE_P) {
  364. const uint8_t *vlc = ff_svq1_block_type_vlc[SVQ1_BLOCK_INTER];
  365. int mx, my, pred_x, pred_y, dxy;
  366. int16_t *motion_ptr;
  367. motion_ptr = ff_h263_pred_motion(&s->m, 0, 0, &pred_x, &pred_y);
  368. if (s->m.mb_type[x + y * s->m.mb_stride] &
  369. CANDIDATE_MB_TYPE_INTER) {
  370. for (i = 0; i < 6; i++)
  371. init_put_bits(&s->reorder_pb[i], reorder_buffer[1][i],
  372. 7 * 32);
  373. put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
  374. s->m.pb = s->reorder_pb[5];
  375. mx = motion_ptr[0];
  376. my = motion_ptr[1];
  377. av_assert1(mx >= -32 && mx <= 31);
  378. av_assert1(my >= -32 && my <= 31);
  379. av_assert1(pred_x >= -32 && pred_x <= 31);
  380. av_assert1(pred_y >= -32 && pred_y <= 31);
  381. ff_h263_encode_motion(&s->m, mx - pred_x, 1);
  382. ff_h263_encode_motion(&s->m, my - pred_y, 1);
  383. s->reorder_pb[5] = s->m.pb;
  384. score[1] += lambda * put_bits_count(&s->reorder_pb[5]);
  385. dxy = (mx & 1) + 2 * (my & 1);
  386. s->dsp.put_pixels_tab[0][dxy](temp + 16,
  387. ref + (mx >> 1) +
  388. stride * (my >> 1),
  389. stride, 16);
  390. score[1] += encode_block(s, src + 16 * x, temp + 16,
  391. decoded, stride, 5, 64, lambda, 0);
  392. best = score[1] <= score[0];
  393. vlc = ff_svq1_block_type_vlc[SVQ1_BLOCK_SKIP];
  394. score[2] = s->dsp.sse[0](NULL, src + 16 * x, ref,
  395. stride, 16);
  396. score[2] += vlc[1] * lambda;
  397. if (score[2] < score[best] && mx == 0 && my == 0) {
  398. best = 2;
  399. s->dsp.put_pixels_tab[0][0](decoded, ref, stride, 16);
  400. for (i = 0; i < 6; i++)
  401. count[2][i] = 0;
  402. put_bits(&s->pb, vlc[1], vlc[0]);
  403. }
  404. }
  405. if (best == 1) {
  406. for (i = 0; i < 6; i++) {
  407. count[1][i] = put_bits_count(&s->reorder_pb[i]);
  408. flush_put_bits(&s->reorder_pb[i]);
  409. }
  410. } else {
  411. motion_ptr[0] =
  412. motion_ptr[1] =
  413. motion_ptr[2] =
  414. motion_ptr[3] =
  415. motion_ptr[0 + 2 * s->m.b8_stride] =
  416. motion_ptr[1 + 2 * s->m.b8_stride] =
  417. motion_ptr[2 + 2 * s->m.b8_stride] =
  418. motion_ptr[3 + 2 * s->m.b8_stride] = 0;
  419. }
  420. }
  421. s->rd_total += score[best];
  422. for (i = 5; i >= 0; i--)
  423. avpriv_copy_bits(&s->pb, reorder_buffer[best][i],
  424. count[best][i]);
  425. if (best == 0)
  426. s->dsp.put_pixels_tab[0][0](decoded, temp, stride, 16);
  427. }
  428. s->m.first_slice_line = 0;
  429. }
  430. return 0;
  431. }
  432. static av_cold int svq1_encode_init(AVCodecContext *avctx)
  433. {
  434. SVQ1Context *const s = avctx->priv_data;
  435. ff_dsputil_init(&s->dsp, avctx);
  436. avctx->coded_frame = &s->picture;
  437. s->frame_width = avctx->width;
  438. s->frame_height = avctx->height;
  439. s->y_block_width = (s->frame_width + 15) / 16;
  440. s->y_block_height = (s->frame_height + 15) / 16;
  441. s->c_block_width = (s->frame_width / 4 + 15) / 16;
  442. s->c_block_height = (s->frame_height / 4 + 15) / 16;
  443. s->avctx = avctx;
  444. s->m.avctx = avctx;
  445. s->m.picture_structure = PICT_FRAME;
  446. s->m.me.temp =
  447. s->m.me.scratchpad = av_mallocz((avctx->width + 64) *
  448. 2 * 16 * 2 * sizeof(uint8_t));
  449. s->m.me.map = av_mallocz(ME_MAP_SIZE * sizeof(uint32_t));
  450. s->m.me.score_map = av_mallocz(ME_MAP_SIZE * sizeof(uint32_t));
  451. s->mb_type = av_mallocz((s->y_block_width + 1) *
  452. s->y_block_height * sizeof(int16_t));
  453. s->dummy = av_mallocz((s->y_block_width + 1) *
  454. s->y_block_height * sizeof(int32_t));
  455. ff_h263_encode_init(&s->m); // mv_penalty
  456. return 0;
  457. }
  458. static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  459. const AVFrame *pict, int *got_packet)
  460. {
  461. SVQ1Context *const s = avctx->priv_data;
  462. AVFrame *const p = &s->picture;
  463. AVFrame temp;
  464. int i, ret;
  465. if ((ret = ff_alloc_packet2(avctx, pkt, s->y_block_width * s->y_block_height *
  466. MAX_MB_BYTES*3 + FF_MIN_BUFFER_SIZE)) < 0)
  467. return ret;
  468. if (avctx->pix_fmt != AV_PIX_FMT_YUV410P) {
  469. av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
  470. return -1;
  471. }
  472. if (!s->current_picture.data[0]) {
  473. if ((ret = ff_get_buffer(avctx, &s->current_picture, 0))< 0 ||
  474. (ret = ff_get_buffer(avctx, &s->last_picture, 0)) < 0) {
  475. return ret;
  476. }
  477. s->scratchbuf = av_malloc(s->current_picture.linesize[0] * 16 * 2);
  478. }
  479. temp = s->current_picture;
  480. s->current_picture = s->last_picture;
  481. s->last_picture = temp;
  482. init_put_bits(&s->pb, pkt->data, pkt->size);
  483. *p = *pict;
  484. p->pict_type = avctx->gop_size && avctx->frame_number % avctx->gop_size ?
  485. AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  486. p->key_frame = p->pict_type == AV_PICTURE_TYPE_I;
  487. svq1_write_header(s, p->pict_type);
  488. for (i = 0; i < 3; i++)
  489. if (svq1_encode_plane(s, i,
  490. s->picture.data[i],
  491. s->last_picture.data[i],
  492. s->current_picture.data[i],
  493. s->frame_width / (i ? 4 : 1),
  494. s->frame_height / (i ? 4 : 1),
  495. s->picture.linesize[i],
  496. s->current_picture.linesize[i]) < 0)
  497. return -1;
  498. // avpriv_align_put_bits(&s->pb);
  499. while (put_bits_count(&s->pb) & 31)
  500. put_bits(&s->pb, 1, 0);
  501. flush_put_bits(&s->pb);
  502. pkt->size = put_bits_count(&s->pb) / 8;
  503. if (p->pict_type == AV_PICTURE_TYPE_I)
  504. pkt->flags |= AV_PKT_FLAG_KEY;
  505. *got_packet = 1;
  506. return 0;
  507. }
  508. static av_cold int svq1_encode_end(AVCodecContext *avctx)
  509. {
  510. SVQ1Context *const s = avctx->priv_data;
  511. int i;
  512. av_log(avctx, AV_LOG_DEBUG, "RD: %f\n",
  513. s->rd_total / (double)(avctx->width * avctx->height *
  514. avctx->frame_number));
  515. av_freep(&s->m.me.scratchpad);
  516. av_freep(&s->m.me.map);
  517. av_freep(&s->m.me.score_map);
  518. av_freep(&s->mb_type);
  519. av_freep(&s->dummy);
  520. av_freep(&s->scratchbuf);
  521. for (i = 0; i < 3; i++) {
  522. av_freep(&s->motion_val8[i]);
  523. av_freep(&s->motion_val16[i]);
  524. }
  525. av_frame_unref(&s->current_picture);
  526. av_frame_unref(&s->last_picture);
  527. return 0;
  528. }
  529. AVCodec ff_svq1_encoder = {
  530. .name = "svq1",
  531. .type = AVMEDIA_TYPE_VIDEO,
  532. .id = AV_CODEC_ID_SVQ1,
  533. .priv_data_size = sizeof(SVQ1Context),
  534. .init = svq1_encode_init,
  535. .encode2 = svq1_encode_frame,
  536. .close = svq1_encode_end,
  537. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV410P,
  538. AV_PIX_FMT_NONE },
  539. .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
  540. };