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.

639 lines
24KB

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