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