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.

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