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