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
23KB

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