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 "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.out_format = FMT_H263;
  246. // s->m.unrestricted_mv = 1;
  247. s->m.lambda = f->quality;
  248. s->m.qscale = s->m.lambda * 139 +
  249. FF_LAMBDA_SCALE * 64 >>
  250. FF_LAMBDA_SHIFT + 7;
  251. s->m.lambda2 = s->m.lambda * s->m.lambda +
  252. FF_LAMBDA_SCALE / 2 >>
  253. FF_LAMBDA_SHIFT;
  254. if (!s->motion_val8[plane]) {
  255. s->motion_val8[plane] = av_mallocz((s->m.b8_stride *
  256. block_height * 2 + 2) *
  257. 2 * sizeof(int16_t));
  258. s->motion_val16[plane] = av_mallocz((s->m.mb_stride *
  259. (block_height + 2) + 1) *
  260. 2 * sizeof(int16_t));
  261. }
  262. s->m.mb_type = s->mb_type;
  263. // dummies, to avoid segfaults
  264. s->m.current_picture.mb_mean = (uint8_t *)s->dummy;
  265. s->m.current_picture.mb_var = (uint16_t *)s->dummy;
  266. s->m.current_picture.mc_mb_var = (uint16_t *)s->dummy;
  267. s->m.current_picture.mb_type = s->dummy;
  268. s->m.current_picture.motion_val[0] = s->motion_val8[plane] + 2;
  269. s->m.p_mv_table = s->motion_val16[plane] +
  270. s->m.mb_stride + 1;
  271. s->m.mecc = s->mecc; // move
  272. ff_init_me(&s->m);
  273. s->m.me.dia_size = s->avctx->dia_size;
  274. s->m.first_slice_line = 1;
  275. for (y = 0; y < block_height; y++) {
  276. s->m.new_picture.f->data[0] = src - y * 16 * stride; // ugly
  277. s->m.mb_y = y;
  278. for (i = 0; i < 16 && i + 16 * y < height; i++) {
  279. memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
  280. width);
  281. for (x = width; x < 16 * block_width; x++)
  282. src[i * stride + x] = src[i * stride + x - 1];
  283. }
  284. for (; i < 16 && i + 16 * y < 16 * block_height; i++)
  285. memcpy(&src[i * stride], &src[(i - 1) * stride],
  286. 16 * block_width);
  287. for (x = 0; x < block_width; x++) {
  288. s->m.mb_x = x;
  289. ff_init_block_index(&s->m);
  290. ff_update_block_index(&s->m);
  291. ff_estimate_p_frame_motion(&s->m, x, y);
  292. }
  293. s->m.first_slice_line = 0;
  294. }
  295. ff_fix_long_p_mvs(&s->m);
  296. ff_fix_long_mvs(&s->m, NULL, 0, s->m.p_mv_table, s->m.f_code,
  297. CANDIDATE_MB_TYPE_INTER, 0);
  298. }
  299. s->m.first_slice_line = 1;
  300. for (y = 0; y < block_height; y++) {
  301. for (i = 0; i < 16 && i + 16 * y < height; i++) {
  302. memcpy(&src[i * stride], &src_plane[(i + 16 * y) * src_stride],
  303. width);
  304. for (x = width; x < 16 * block_width; x++)
  305. src[i * stride + x] = src[i * stride + x - 1];
  306. }
  307. for (; i < 16 && i + 16 * y < 16 * block_height; i++)
  308. memcpy(&src[i * stride], &src[(i - 1) * stride], 16 * block_width);
  309. s->m.mb_y = y;
  310. for (x = 0; x < block_width; x++) {
  311. uint8_t reorder_buffer[3][6][7 * 32];
  312. int count[3][6];
  313. int offset = y * 16 * stride + x * 16;
  314. uint8_t *decoded = decoded_plane + offset;
  315. uint8_t *ref = ref_plane + offset;
  316. int score[4] = { 0, 0, 0, 0 }, best;
  317. uint8_t *temp = s->scratchbuf;
  318. if (s->pb.buf_end - s->pb.buf -
  319. (put_bits_count(&s->pb) >> 3) < 3000) { // FIXME: check size
  320. av_log(s->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  321. return -1;
  322. }
  323. s->m.mb_x = x;
  324. ff_init_block_index(&s->m);
  325. ff_update_block_index(&s->m);
  326. if (f->pict_type == AV_PICTURE_TYPE_I ||
  327. (s->m.mb_type[x + y * s->m.mb_stride] &
  328. CANDIDATE_MB_TYPE_INTRA)) {
  329. for (i = 0; i < 6; i++)
  330. init_put_bits(&s->reorder_pb[i], reorder_buffer[0][i],
  331. 7 * 32);
  332. if (f->pict_type == AV_PICTURE_TYPE_P) {
  333. const uint8_t *vlc = ff_svq1_block_type_vlc[SVQ1_BLOCK_INTRA];
  334. put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
  335. score[0] = vlc[1] * lambda;
  336. }
  337. score[0] += encode_block(s, src + 16 * x, NULL, temp, stride,
  338. 5, 64, lambda, 1);
  339. for (i = 0; i < 6; i++) {
  340. count[0][i] = put_bits_count(&s->reorder_pb[i]);
  341. flush_put_bits(&s->reorder_pb[i]);
  342. }
  343. } else
  344. score[0] = INT_MAX;
  345. best = 0;
  346. if (f->pict_type == AV_PICTURE_TYPE_P) {
  347. const uint8_t *vlc = ff_svq1_block_type_vlc[SVQ1_BLOCK_INTER];
  348. int mx, my, pred_x, pred_y, dxy;
  349. int16_t *motion_ptr;
  350. motion_ptr = ff_h263_pred_motion(&s->m, 0, 0, &pred_x, &pred_y);
  351. if (s->m.mb_type[x + y * s->m.mb_stride] &
  352. CANDIDATE_MB_TYPE_INTER) {
  353. for (i = 0; i < 6; i++)
  354. init_put_bits(&s->reorder_pb[i], reorder_buffer[1][i],
  355. 7 * 32);
  356. put_bits(&s->reorder_pb[5], vlc[1], vlc[0]);
  357. s->m.pb = s->reorder_pb[5];
  358. mx = motion_ptr[0];
  359. my = motion_ptr[1];
  360. assert(mx >= -32 && mx <= 31);
  361. assert(my >= -32 && my <= 31);
  362. assert(pred_x >= -32 && pred_x <= 31);
  363. assert(pred_y >= -32 && pred_y <= 31);
  364. ff_h263_encode_motion(&s->m, mx - pred_x, 1);
  365. ff_h263_encode_motion(&s->m, my - pred_y, 1);
  366. s->reorder_pb[5] = s->m.pb;
  367. score[1] += lambda * put_bits_count(&s->reorder_pb[5]);
  368. dxy = (mx & 1) + 2 * (my & 1);
  369. s->hdsp.put_pixels_tab[0][dxy](temp + 16,
  370. ref + (mx >> 1) +
  371. stride * (my >> 1),
  372. stride, 16);
  373. score[1] += encode_block(s, src + 16 * x, temp + 16,
  374. decoded, stride, 5, 64, lambda, 0);
  375. best = score[1] <= score[0];
  376. vlc = ff_svq1_block_type_vlc[SVQ1_BLOCK_SKIP];
  377. score[2] = s->mecc.sse[0](NULL, src + 16 * x, ref,
  378. stride, 16);
  379. score[2] += vlc[1] * lambda;
  380. if (score[2] < score[best] && mx == 0 && my == 0) {
  381. best = 2;
  382. s->hdsp.put_pixels_tab[0][0](decoded, ref, stride, 16);
  383. for (i = 0; i < 6; i++)
  384. count[2][i] = 0;
  385. put_bits(&s->pb, vlc[1], vlc[0]);
  386. }
  387. }
  388. if (best == 1) {
  389. for (i = 0; i < 6; i++) {
  390. count[1][i] = put_bits_count(&s->reorder_pb[i]);
  391. flush_put_bits(&s->reorder_pb[i]);
  392. }
  393. } else {
  394. motion_ptr[0] =
  395. motion_ptr[1] =
  396. motion_ptr[2] =
  397. motion_ptr[3] =
  398. motion_ptr[0 + 2 * s->m.b8_stride] =
  399. motion_ptr[1 + 2 * s->m.b8_stride] =
  400. motion_ptr[2 + 2 * s->m.b8_stride] =
  401. motion_ptr[3 + 2 * s->m.b8_stride] = 0;
  402. }
  403. }
  404. s->rd_total += score[best];
  405. for (i = 5; i >= 0; i--)
  406. avpriv_copy_bits(&s->pb, reorder_buffer[best][i],
  407. count[best][i]);
  408. if (best == 0)
  409. s->hdsp.put_pixels_tab[0][0](decoded, temp, stride, 16);
  410. }
  411. s->m.first_slice_line = 0;
  412. }
  413. return 0;
  414. }
  415. static av_cold int svq1_encode_end(AVCodecContext *avctx)
  416. {
  417. SVQ1EncContext *const s = avctx->priv_data;
  418. int i;
  419. av_log(avctx, AV_LOG_DEBUG, "RD: %f\n",
  420. s->rd_total / (double)(avctx->width * avctx->height *
  421. avctx->frame_number));
  422. s->m.mb_type = NULL;
  423. ff_mpv_common_end(&s->m);
  424. av_freep(&s->m.me.scratchpad);
  425. av_freep(&s->m.me.map);
  426. av_freep(&s->m.me.score_map);
  427. av_freep(&s->mb_type);
  428. av_freep(&s->dummy);
  429. av_freep(&s->scratchbuf);
  430. for (i = 0; i < 3; i++) {
  431. av_freep(&s->motion_val8[i]);
  432. av_freep(&s->motion_val16[i]);
  433. }
  434. av_frame_free(&s->current_picture);
  435. av_frame_free(&s->last_picture);
  436. av_frame_free(&avctx->coded_frame);
  437. return 0;
  438. }
  439. static av_cold int svq1_encode_init(AVCodecContext *avctx)
  440. {
  441. SVQ1EncContext *const s = avctx->priv_data;
  442. int ret;
  443. ff_hpeldsp_init(&s->hdsp, avctx->flags);
  444. ff_me_cmp_init(&s->mecc, avctx);
  445. ff_mpegvideoencdsp_init(&s->m.mpvencdsp, avctx);
  446. avctx->coded_frame = av_frame_alloc();
  447. s->current_picture = av_frame_alloc();
  448. s->last_picture = av_frame_alloc();
  449. if (!avctx->coded_frame || !s->current_picture || !s->last_picture) {
  450. svq1_encode_end(avctx);
  451. return AVERROR(ENOMEM);
  452. }
  453. s->frame_width = avctx->width;
  454. s->frame_height = avctx->height;
  455. s->y_block_width = (s->frame_width + 15) / 16;
  456. s->y_block_height = (s->frame_height + 15) / 16;
  457. s->c_block_width = (s->frame_width / 4 + 15) / 16;
  458. s->c_block_height = (s->frame_height / 4 + 15) / 16;
  459. s->avctx = avctx;
  460. s->m.avctx = avctx;
  461. if ((ret = ff_mpv_common_init(&s->m)) < 0) {
  462. svq1_encode_end(avctx);
  463. return ret;
  464. }
  465. s->m.picture_structure = PICT_FRAME;
  466. s->m.me.temp =
  467. s->m.me.scratchpad = av_mallocz((avctx->width + 64) *
  468. 2 * 16 * 2 * sizeof(uint8_t));
  469. s->m.me.map = av_mallocz(ME_MAP_SIZE * sizeof(uint32_t));
  470. s->m.me.score_map = av_mallocz(ME_MAP_SIZE * sizeof(uint32_t));
  471. s->mb_type = av_mallocz((s->y_block_width + 1) *
  472. s->y_block_height * sizeof(int16_t));
  473. s->dummy = av_mallocz((s->y_block_width + 1) *
  474. s->y_block_height * sizeof(int32_t));
  475. s->ssd_int8_vs_int16 = ssd_int8_vs_int16_c;
  476. if (ARCH_PPC)
  477. ff_svq1enc_init_ppc(s);
  478. if (ARCH_X86)
  479. ff_svq1enc_init_x86(s);
  480. ff_h263_encode_init(&s->m); // mv_penalty
  481. return 0;
  482. }
  483. static int svq1_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  484. const AVFrame *pict, int *got_packet)
  485. {
  486. SVQ1EncContext *const s = avctx->priv_data;
  487. AVFrame *const p = avctx->coded_frame;
  488. int i, ret;
  489. if (!pkt->data &&
  490. (ret = av_new_packet(pkt, s->y_block_width * s->y_block_height *
  491. MAX_MB_BYTES * 3 + FF_MIN_BUFFER_SIZE)) < 0) {
  492. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  493. return ret;
  494. }
  495. if (avctx->pix_fmt != AV_PIX_FMT_YUV410P) {
  496. av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
  497. return -1;
  498. }
  499. if (!s->current_picture->data[0]) {
  500. ret = ff_get_buffer(avctx, s->current_picture, 0);
  501. if (ret < 0)
  502. return ret;
  503. }
  504. if (!s->last_picture->data[0]) {
  505. ret = ff_get_buffer(avctx, s->last_picture, 0);
  506. if (ret < 0)
  507. return ret;
  508. }
  509. if (!s->scratchbuf) {
  510. s->scratchbuf = av_malloc(s->current_picture->linesize[0] * 16 * 2);
  511. if (!s->scratchbuf)
  512. return AVERROR(ENOMEM);
  513. }
  514. FFSWAP(AVFrame*, s->current_picture, s->last_picture);
  515. init_put_bits(&s->pb, pkt->data, pkt->size);
  516. p->pict_type = avctx->gop_size && avctx->frame_number % avctx->gop_size ?
  517. AV_PICTURE_TYPE_P : AV_PICTURE_TYPE_I;
  518. p->key_frame = p->pict_type == AV_PICTURE_TYPE_I;
  519. p->quality = pict->quality;
  520. svq1_write_header(s, p->pict_type);
  521. for (i = 0; i < 3; i++)
  522. if (svq1_encode_plane(s, i,
  523. pict->data[i],
  524. s->last_picture->data[i],
  525. s->current_picture->data[i],
  526. s->frame_width / (i ? 4 : 1),
  527. s->frame_height / (i ? 4 : 1),
  528. pict->linesize[i],
  529. s->current_picture->linesize[i]) < 0)
  530. return -1;
  531. // avpriv_align_put_bits(&s->pb);
  532. while (put_bits_count(&s->pb) & 31)
  533. put_bits(&s->pb, 1, 0);
  534. flush_put_bits(&s->pb);
  535. pkt->size = put_bits_count(&s->pb) / 8;
  536. if (p->pict_type == AV_PICTURE_TYPE_I)
  537. pkt->flags |= AV_PKT_FLAG_KEY;
  538. *got_packet = 1;
  539. return 0;
  540. }
  541. AVCodec ff_svq1_encoder = {
  542. .name = "svq1",
  543. .long_name = NULL_IF_CONFIG_SMALL("Sorenson Vector Quantizer 1 / Sorenson Video 1 / SVQ1"),
  544. .type = AVMEDIA_TYPE_VIDEO,
  545. .id = AV_CODEC_ID_SVQ1,
  546. .priv_data_size = sizeof(SVQ1EncContext),
  547. .init = svq1_encode_init,
  548. .encode2 = svq1_encode_frame,
  549. .close = svq1_encode_end,
  550. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV410P,
  551. AV_PIX_FMT_NONE },
  552. };