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.

1358 lines
41KB

  1. /*
  2. * The simplest mpeg encoder (well, it was the simplest!)
  3. * Copyright (c) 2000,2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include <string.h>
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #include "mpegvideo.h"
  26. #ifdef USE_FASTMEMCPY
  27. #include "fastmemcpy.h"
  28. #endif
  29. static void encode_picture(MpegEncContext *s, int picture_number);
  30. static void rate_control_init(MpegEncContext *s);
  31. static int rate_estimate_qscale(MpegEncContext *s);
  32. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  33. DCTELEM *block, int n, int qscale);
  34. static void dct_unquantize_h263_c(MpegEncContext *s,
  35. DCTELEM *block, int n, int qscale);
  36. static int dct_quantize(MpegEncContext *s, DCTELEM *block, int n, int qscale);
  37. static int dct_quantize_mmx(MpegEncContext *s,
  38. DCTELEM *block, int n,
  39. int qscale);
  40. #define EDGE_WIDTH 16
  41. /* enable all paranoid tests for rounding, overflows, etc... */
  42. //#define PARANOID
  43. //#define DEBUG
  44. /* for jpeg fast DCT */
  45. #define CONST_BITS 14
  46. static const unsigned short aanscales[64] = {
  47. /* precomputed values scaled up by 14 bits */
  48. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  49. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  50. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  51. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  52. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  53. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  54. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  55. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
  56. };
  57. static UINT8 h263_chroma_roundtab[16] = {
  58. 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
  59. };
  60. /* default motion estimation */
  61. int motion_estimation_method = ME_LOG;
  62. /* XXX: should use variable shift ? */
  63. #define QMAT_SHIFT_MMX 19
  64. #define QMAT_SHIFT 25
  65. static void convert_matrix(int *qmat, const UINT16 *quant_matrix, int qscale)
  66. {
  67. int i;
  68. if (av_fdct == jpeg_fdct_ifast) {
  69. for(i=0;i<64;i++) {
  70. /* 16 <= qscale * quant_matrix[i] <= 7905 */
  71. /* 19952 <= aanscales[i] * qscale * quant_matrix[i] <= 249205026 */
  72. qmat[i] = (int)((1ULL << (QMAT_SHIFT + 11)) / (aanscales[i] * qscale * quant_matrix[i]));
  73. }
  74. } else {
  75. for(i=0;i<64;i++) {
  76. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  77. So 16 <= qscale * quant_matrix[i] <= 7905
  78. so (1 << QMAT_SHIFT) / 16 >= qmat[i] >= (1 << QMAT_SHIFT) / 7905
  79. */
  80. qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  81. }
  82. }
  83. }
  84. /* init common structure for both encoder and decoder */
  85. int MPV_common_init(MpegEncContext *s)
  86. {
  87. int c_size, i;
  88. UINT8 *pict;
  89. if (s->out_format == FMT_H263)
  90. s->dct_unquantize = dct_unquantize_h263_c;
  91. else
  92. s->dct_unquantize = dct_unquantize_mpeg1_c;
  93. #ifdef HAVE_MMX
  94. MPV_common_init_mmx(s);
  95. #endif
  96. s->mb_width = (s->width + 15) / 16;
  97. s->mb_height = (s->height + 15) / 16;
  98. s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
  99. for(i=0;i<3;i++) {
  100. int w, h, shift, pict_start;
  101. w = s->linesize;
  102. h = s->mb_height * 16 + 2 * EDGE_WIDTH;
  103. shift = (i == 0) ? 0 : 1;
  104. c_size = (w >> shift) * (h >> shift);
  105. pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
  106. pict = av_mallocz(c_size);
  107. if (pict == NULL)
  108. goto fail;
  109. s->last_picture_base[i] = pict;
  110. s->last_picture[i] = pict + pict_start;
  111. pict = av_mallocz(c_size);
  112. if (pict == NULL)
  113. goto fail;
  114. s->next_picture_base[i] = pict;
  115. s->next_picture[i] = pict + pict_start;
  116. if (s->has_b_frames) {
  117. pict = av_mallocz(c_size);
  118. if (pict == NULL)
  119. goto fail;
  120. s->aux_picture_base[i] = pict;
  121. s->aux_picture[i] = pict + pict_start;
  122. }
  123. }
  124. if (s->out_format == FMT_H263) {
  125. int size;
  126. /* MV prediction */
  127. size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  128. s->motion_val = malloc(size * 2 * sizeof(INT16));
  129. if (s->motion_val == NULL)
  130. goto fail;
  131. memset(s->motion_val, 0, size * 2 * sizeof(INT16));
  132. }
  133. if (s->h263_pred) {
  134. int y_size, c_size, i, size;
  135. /* dc values */
  136. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  137. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  138. size = y_size + 2 * c_size;
  139. s->dc_val[0] = malloc(size * sizeof(INT16));
  140. if (s->dc_val[0] == NULL)
  141. goto fail;
  142. s->dc_val[1] = s->dc_val[0] + y_size;
  143. s->dc_val[2] = s->dc_val[1] + c_size;
  144. for(i=0;i<size;i++)
  145. s->dc_val[0][i] = 1024;
  146. /* ac values */
  147. s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
  148. if (s->ac_val[0] == NULL)
  149. goto fail;
  150. s->ac_val[1] = s->ac_val[0] + y_size;
  151. s->ac_val[2] = s->ac_val[1] + c_size;
  152. /* cbp values */
  153. s->coded_block = av_mallocz(y_size);
  154. if (!s->coded_block)
  155. goto fail;
  156. }
  157. /* default structure is frame */
  158. s->picture_structure = PICT_FRAME;
  159. /* init default q matrix (only for mpeg and mjpeg) */
  160. for(i=0;i<64;i++) {
  161. s->intra_matrix[i] = default_intra_matrix[i];
  162. s->chroma_intra_matrix[i] = default_intra_matrix[i];
  163. s->non_intra_matrix[i] = default_non_intra_matrix[i];
  164. s->chroma_non_intra_matrix[i] = default_non_intra_matrix[i];
  165. }
  166. /* init macroblock skip table */
  167. if (!s->encoding) {
  168. s->mbskip_table = av_mallocz(s->mb_width * s->mb_height);
  169. if (!s->mbskip_table)
  170. goto fail;
  171. }
  172. s->context_initialized = 1;
  173. return 0;
  174. fail:
  175. if (s->motion_val)
  176. free(s->motion_val);
  177. if (s->dc_val[0])
  178. free(s->dc_val[0]);
  179. if (s->ac_val[0])
  180. free(s->ac_val[0]);
  181. if (s->coded_block)
  182. free(s->coded_block);
  183. if (s->mbskip_table)
  184. free(s->mbskip_table);
  185. for(i=0;i<3;i++) {
  186. if (s->last_picture_base[i])
  187. free(s->last_picture_base[i]);
  188. if (s->next_picture_base[i])
  189. free(s->next_picture_base[i]);
  190. if (s->aux_picture_base[i])
  191. free(s->aux_picture_base[i]);
  192. }
  193. return -1;
  194. }
  195. /* init common structure for both encoder and decoder */
  196. void MPV_common_end(MpegEncContext *s)
  197. {
  198. int i;
  199. if (s->motion_val)
  200. free(s->motion_val);
  201. if (s->h263_pred) {
  202. free(s->dc_val[0]);
  203. free(s->ac_val[0]);
  204. free(s->coded_block);
  205. }
  206. if (s->mbskip_table)
  207. free(s->mbskip_table);
  208. for(i=0;i<3;i++) {
  209. free(s->last_picture_base[i]);
  210. free(s->next_picture_base[i]);
  211. if (s->has_b_frames)
  212. free(s->aux_picture_base[i]);
  213. }
  214. s->context_initialized = 0;
  215. }
  216. /* init video encoder */
  217. int MPV_encode_init(AVCodecContext *avctx)
  218. {
  219. MpegEncContext *s = avctx->priv_data;
  220. s->bit_rate = avctx->bit_rate;
  221. s->frame_rate = avctx->frame_rate;
  222. s->width = avctx->width;
  223. s->height = avctx->height;
  224. s->gop_size = avctx->gop_size;
  225. if (s->gop_size <= 1) {
  226. s->intra_only = 1;
  227. s->gop_size = 12;
  228. } else {
  229. s->intra_only = 0;
  230. }
  231. s->full_search = motion_estimation_method;
  232. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  233. switch(avctx->codec->id) {
  234. case CODEC_ID_MPEG1VIDEO:
  235. s->out_format = FMT_MPEG1;
  236. break;
  237. case CODEC_ID_MJPEG:
  238. s->out_format = FMT_MJPEG;
  239. s->intra_only = 1; /* force intra only for jpeg */
  240. if (mjpeg_init(s) < 0)
  241. return -1;
  242. break;
  243. case CODEC_ID_H263:
  244. if (h263_get_picture_format(s->width, s->height) == 7)
  245. return -1;
  246. s->out_format = FMT_H263;
  247. break;
  248. case CODEC_ID_H263P:
  249. s->out_format = FMT_H263;
  250. s->h263_plus = 1;
  251. /* XXX: not unrectricted mv yet */
  252. break;
  253. case CODEC_ID_RV10:
  254. s->out_format = FMT_H263;
  255. s->h263_rv10 = 1;
  256. break;
  257. case CODEC_ID_OPENDIVX:
  258. s->out_format = FMT_H263;
  259. s->h263_pred = 1;
  260. s->unrestricted_mv = 1;
  261. break;
  262. case CODEC_ID_MSMPEG4:
  263. s->out_format = FMT_H263;
  264. s->h263_msmpeg4 = 1;
  265. s->h263_pred = 1;
  266. s->unrestricted_mv = 1;
  267. break;
  268. default:
  269. return -1;
  270. }
  271. if (s->out_format == FMT_H263)
  272. h263_encode_init_vlc(s);
  273. s->encoding = 1;
  274. /* init */
  275. if (MPV_common_init(s) < 0)
  276. return -1;
  277. /* rate control init */
  278. rate_control_init(s);
  279. s->picture_number = 0;
  280. s->fake_picture_number = 0;
  281. /* motion detector init */
  282. s->f_code = 1;
  283. return 0;
  284. }
  285. int MPV_encode_end(AVCodecContext *avctx)
  286. {
  287. MpegEncContext *s = avctx->priv_data;
  288. #ifdef STATS
  289. print_stats();
  290. #endif
  291. MPV_common_end(s);
  292. if (s->out_format == FMT_MJPEG)
  293. mjpeg_close(s);
  294. return 0;
  295. }
  296. /* draw the edges of width 'w' of an image of size width, height */
  297. static void draw_edges(UINT8 *buf, int wrap, int width, int height, int w)
  298. {
  299. UINT8 *ptr, *last_line;
  300. int i;
  301. last_line = buf + (height - 1) * wrap;
  302. for(i=0;i<w;i++) {
  303. /* top and bottom */
  304. memcpy(buf - (i + 1) * wrap, buf, width);
  305. memcpy(last_line + (i + 1) * wrap, last_line, width);
  306. }
  307. /* left and right */
  308. ptr = buf;
  309. for(i=0;i<height;i++) {
  310. memset(ptr - w, ptr[0], w);
  311. memset(ptr + width, ptr[width-1], w);
  312. ptr += wrap;
  313. }
  314. /* corners */
  315. for(i=0;i<w;i++) {
  316. memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  317. memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  318. memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  319. memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  320. }
  321. }
  322. /* generic function for encode/decode called before a frame is coded/decoded */
  323. void MPV_frame_start(MpegEncContext *s)
  324. {
  325. int i;
  326. UINT8 *tmp;
  327. s->mb_skiped = 0;
  328. if (s->pict_type == B_TYPE) {
  329. for(i=0;i<3;i++) {
  330. s->current_picture[i] = s->aux_picture[i];
  331. }
  332. } else {
  333. for(i=0;i<3;i++) {
  334. /* swap next and last */
  335. tmp = s->last_picture[i];
  336. s->last_picture[i] = s->next_picture[i];
  337. s->next_picture[i] = tmp;
  338. s->current_picture[i] = tmp;
  339. }
  340. }
  341. }
  342. /* generic function for encode/decode called after a frame has been coded/decoded */
  343. void MPV_frame_end(MpegEncContext *s)
  344. {
  345. /* draw edge for correct motion prediction if outside */
  346. if (s->pict_type != B_TYPE) {
  347. draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
  348. draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  349. draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  350. }
  351. }
  352. int MPV_encode_picture(AVCodecContext *avctx,
  353. unsigned char *buf, int buf_size, void *data)
  354. {
  355. MpegEncContext *s = avctx->priv_data;
  356. AVPicture *pict = data;
  357. int i, j;
  358. if (s->fixed_qscale)
  359. s->qscale = avctx->quality;
  360. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  361. if (!s->intra_only) {
  362. /* first picture of GOP is intra */
  363. if ((s->picture_number % s->gop_size) == 0)
  364. s->pict_type = I_TYPE;
  365. else
  366. s->pict_type = P_TYPE;
  367. } else {
  368. s->pict_type = I_TYPE;
  369. }
  370. avctx->key_frame = (s->pict_type == I_TYPE);
  371. MPV_frame_start(s);
  372. for(i=0;i<3;i++) {
  373. UINT8 *src = pict->data[i];
  374. UINT8 *dest = s->current_picture[i];
  375. int src_wrap = pict->linesize[i];
  376. int dest_wrap = s->linesize;
  377. int w = s->width;
  378. int h = s->height;
  379. if (i >= 1) {
  380. dest_wrap >>= 1;
  381. w >>= 1;
  382. h >>= 1;
  383. }
  384. for(j=0;j<h;j++) {
  385. memcpy(dest, src, w);
  386. dest += dest_wrap;
  387. src += src_wrap;
  388. }
  389. s->new_picture[i] = s->current_picture[i];
  390. }
  391. encode_picture(s, s->picture_number);
  392. MPV_frame_end(s);
  393. s->picture_number++;
  394. if (s->out_format == FMT_MJPEG)
  395. mjpeg_picture_trailer(s);
  396. flush_put_bits(&s->pb);
  397. s->total_bits += (s->pb.buf_ptr - s->pb.buf) * 8;
  398. avctx->quality = s->qscale;
  399. return s->pb.buf_ptr - s->pb.buf;
  400. }
  401. static inline int clip(int a, int amin, int amax)
  402. {
  403. if (a < amin)
  404. return amin;
  405. else if (a > amax)
  406. return amax;
  407. else
  408. return a;
  409. }
  410. /* apply one mpeg motion vector to the three components */
  411. static inline void mpeg_motion(MpegEncContext *s,
  412. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  413. int dest_offset,
  414. UINT8 **ref_picture, int src_offset,
  415. int field_based, op_pixels_func *pix_op,
  416. int motion_x, int motion_y, int h)
  417. {
  418. UINT8 *ptr;
  419. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  420. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  421. src_x = s->mb_x * 16 + (motion_x >> 1);
  422. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  423. /* WARNING: do no forget half pels */
  424. height = s->height >> field_based;
  425. src_x = clip(src_x, -16, s->width);
  426. if (src_x == s->width)
  427. dxy &= ~1;
  428. src_y = clip(src_y, -16, height);
  429. if (src_y == height)
  430. dxy &= ~2;
  431. linesize = s->linesize << field_based;
  432. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  433. dest_y += dest_offset;
  434. pix_op[dxy](dest_y, ptr, linesize, h);
  435. pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
  436. if (s->out_format == FMT_H263) {
  437. dxy = 0;
  438. if ((motion_x & 3) != 0)
  439. dxy |= 1;
  440. if ((motion_y & 3) != 0)
  441. dxy |= 2;
  442. mx = motion_x >> 2;
  443. my = motion_y >> 2;
  444. } else {
  445. mx = motion_x / 2;
  446. my = motion_y / 2;
  447. dxy = ((my & 1) << 1) | (mx & 1);
  448. mx >>= 1;
  449. my >>= 1;
  450. }
  451. src_x = s->mb_x * 8 + mx;
  452. src_y = s->mb_y * (8 >> field_based) + my;
  453. src_x = clip(src_x, -8, s->width >> 1);
  454. if (src_x == (s->width >> 1))
  455. dxy &= ~1;
  456. src_y = clip(src_y, -8, height >> 1);
  457. if (src_y == (height >> 1))
  458. dxy &= ~2;
  459. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  460. ptr = ref_picture[1] + offset;
  461. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  462. ptr = ref_picture[2] + offset;
  463. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  464. }
  465. static inline void MPV_motion(MpegEncContext *s,
  466. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  467. int dir, UINT8 **ref_picture,
  468. op_pixels_func *pix_op)
  469. {
  470. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  471. int mb_x, mb_y, i;
  472. UINT8 *ptr, *dest;
  473. mb_x = s->mb_x;
  474. mb_y = s->mb_y;
  475. switch(s->mv_type) {
  476. case MV_TYPE_16X16:
  477. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  478. ref_picture, 0,
  479. 0, pix_op,
  480. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  481. break;
  482. case MV_TYPE_8X8:
  483. for(i=0;i<4;i++) {
  484. motion_x = s->mv[dir][i][0];
  485. motion_y = s->mv[dir][i][1];
  486. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  487. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  488. src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
  489. /* WARNING: do no forget half pels */
  490. src_x = clip(src_x, -16, s->width);
  491. if (src_x == s->width)
  492. dxy &= ~1;
  493. src_y = clip(src_y, -16, s->height);
  494. if (src_y == s->height)
  495. dxy &= ~2;
  496. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  497. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  498. pix_op[dxy](dest, ptr, s->linesize, 8);
  499. }
  500. /* In case of 8X8, we construct a single chroma motion vector
  501. with a special rounding */
  502. mx = 0;
  503. my = 0;
  504. for(i=0;i<4;i++) {
  505. mx += s->mv[dir][i][0];
  506. my += s->mv[dir][i][1];
  507. }
  508. if (mx >= 0)
  509. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  510. else {
  511. mx = -mx;
  512. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  513. }
  514. if (my >= 0)
  515. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  516. else {
  517. my = -my;
  518. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  519. }
  520. dxy = ((my & 1) << 1) | (mx & 1);
  521. mx >>= 1;
  522. my >>= 1;
  523. src_x = mb_x * 8 + mx;
  524. src_y = mb_y * 8 + my;
  525. src_x = clip(src_x, -8, s->width/2);
  526. if (src_x == s->width/2)
  527. dxy &= ~1;
  528. src_y = clip(src_y, -8, s->height/2);
  529. if (src_y == s->height/2)
  530. dxy &= ~2;
  531. offset = (src_y * (s->linesize >> 1)) + src_x;
  532. ptr = ref_picture[1] + offset;
  533. pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
  534. ptr = ref_picture[2] + offset;
  535. pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
  536. break;
  537. case MV_TYPE_FIELD:
  538. if (s->picture_structure == PICT_FRAME) {
  539. /* top field */
  540. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  541. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  542. 1, pix_op,
  543. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  544. /* bottom field */
  545. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  546. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  547. 1, pix_op,
  548. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  549. } else {
  550. }
  551. break;
  552. }
  553. }
  554. /* put block[] to dest[] */
  555. static inline void put_dct(MpegEncContext *s,
  556. DCTELEM *block, int i, UINT8 *dest, int line_size)
  557. {
  558. if (!s->mpeg2)
  559. s->dct_unquantize(s, block, i, s->qscale);
  560. ff_idct (block);
  561. put_pixels_clamped(block, dest, line_size);
  562. }
  563. /* add block[] to dest[] */
  564. static inline void add_dct(MpegEncContext *s,
  565. DCTELEM *block, int i, UINT8 *dest, int line_size)
  566. {
  567. if (s->block_last_index[i] >= 0) {
  568. if (!s->mpeg2)
  569. s->dct_unquantize(s, block, i, s->qscale);
  570. ff_idct (block);
  571. add_pixels_clamped(block, dest, line_size);
  572. }
  573. }
  574. /* generic function called after a macroblock has been parsed by the
  575. decoder or after it has been encoded by the encoder.
  576. Important variables used:
  577. s->mb_intra : true if intra macroblock
  578. s->mv_dir : motion vector direction
  579. s->mv_type : motion vector type
  580. s->mv : motion vector
  581. s->interlaced_dct : true if interlaced dct used (mpeg2)
  582. */
  583. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  584. {
  585. int mb_x, mb_y, motion_x, motion_y;
  586. int dct_linesize, dct_offset;
  587. op_pixels_func *op_pix;
  588. mb_x = s->mb_x;
  589. mb_y = s->mb_y;
  590. /* update DC predictors for P macroblocks */
  591. if (!s->mb_intra) {
  592. if (s->h263_pred) {
  593. int wrap, x, y, v;
  594. wrap = 2 * s->mb_width + 2;
  595. v = 1024;
  596. x = 2 * mb_x + 1;
  597. y = 2 * mb_y + 1;
  598. s->dc_val[0][(x) + (y) * wrap] = v;
  599. s->dc_val[0][(x + 1) + (y) * wrap] = v;
  600. s->dc_val[0][(x) + (y + 1) * wrap] = v;
  601. s->dc_val[0][(x + 1) + (y + 1) * wrap] = v;
  602. /* ac pred */
  603. memset(s->ac_val[0][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
  604. memset(s->ac_val[0][(x + 1) + (y) * wrap], 0, 16 * sizeof(INT16));
  605. memset(s->ac_val[0][(x) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
  606. memset(s->ac_val[0][(x + 1) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
  607. if (s->h263_msmpeg4) {
  608. s->coded_block[(x) + (y) * wrap] = 0;
  609. s->coded_block[(x + 1) + (y) * wrap] = 0;
  610. s->coded_block[(x) + (y + 1) * wrap] = 0;
  611. s->coded_block[(x + 1) + (y + 1) * wrap] = 0;
  612. }
  613. /* chroma */
  614. wrap = s->mb_width + 2;
  615. x = mb_x + 1;
  616. y = mb_y + 1;
  617. s->dc_val[1][(x) + (y) * wrap] = v;
  618. s->dc_val[2][(x) + (y) * wrap] = v;
  619. /* ac pred */
  620. memset(s->ac_val[1][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
  621. memset(s->ac_val[2][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
  622. } else {
  623. s->last_dc[0] = 128 << s->intra_dc_precision;
  624. s->last_dc[1] = 128 << s->intra_dc_precision;
  625. s->last_dc[2] = 128 << s->intra_dc_precision;
  626. }
  627. }
  628. /* update motion predictor */
  629. if (s->out_format == FMT_H263) {
  630. int x, y, wrap;
  631. x = 2 * mb_x + 1;
  632. y = 2 * mb_y + 1;
  633. wrap = 2 * s->mb_width + 2;
  634. if (s->mb_intra) {
  635. motion_x = 0;
  636. motion_y = 0;
  637. goto motion_init;
  638. } else if (s->mv_type == MV_TYPE_16X16) {
  639. motion_x = s->mv[0][0][0];
  640. motion_y = s->mv[0][0][1];
  641. motion_init:
  642. /* no update if 8X8 because it has been done during parsing */
  643. s->motion_val[(x) + (y) * wrap][0] = motion_x;
  644. s->motion_val[(x) + (y) * wrap][1] = motion_y;
  645. s->motion_val[(x + 1) + (y) * wrap][0] = motion_x;
  646. s->motion_val[(x + 1) + (y) * wrap][1] = motion_y;
  647. s->motion_val[(x) + (y + 1) * wrap][0] = motion_x;
  648. s->motion_val[(x) + (y + 1) * wrap][1] = motion_y;
  649. s->motion_val[(x + 1) + (y + 1) * wrap][0] = motion_x;
  650. s->motion_val[(x + 1) + (y + 1) * wrap][1] = motion_y;
  651. }
  652. }
  653. if (!s->intra_only) {
  654. UINT8 *dest_y, *dest_cb, *dest_cr;
  655. UINT8 *mbskip_ptr;
  656. /* avoid copy if macroblock skipped in last frame too */
  657. if (!s->encoding && s->pict_type != B_TYPE) {
  658. mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
  659. if (s->mb_skiped) {
  660. s->mb_skiped = 0;
  661. /* if previous was skipped too, then nothing to do ! */
  662. if (*mbskip_ptr != 0)
  663. goto the_end;
  664. *mbskip_ptr = 1; /* indicate that this time we skiped it */
  665. } else {
  666. *mbskip_ptr = 0; /* not skipped */
  667. }
  668. }
  669. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
  670. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  671. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  672. if (s->interlaced_dct) {
  673. dct_linesize = s->linesize * 2;
  674. dct_offset = s->linesize;
  675. } else {
  676. dct_linesize = s->linesize;
  677. dct_offset = s->linesize * 8;
  678. }
  679. if (!s->mb_intra) {
  680. /* motion handling */
  681. if (!s->no_rounding)
  682. op_pix = put_pixels_tab;
  683. else
  684. op_pix = put_no_rnd_pixels_tab;
  685. if (s->mv_dir & MV_DIR_FORWARD) {
  686. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix);
  687. if (!s->no_rounding)
  688. op_pix = avg_pixels_tab;
  689. else
  690. op_pix = avg_no_rnd_pixels_tab;
  691. }
  692. if (s->mv_dir & MV_DIR_BACKWARD) {
  693. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix);
  694. }
  695. /* add dct residue */
  696. add_dct(s, block[0], 0, dest_y, dct_linesize);
  697. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  698. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  699. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  700. add_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  701. add_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  702. } else {
  703. /* dct only in intra block */
  704. put_dct(s, block[0], 0, dest_y, dct_linesize);
  705. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  706. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  707. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  708. put_dct(s, block[4], 4, dest_cb, s->linesize >> 1);
  709. put_dct(s, block[5], 5, dest_cr, s->linesize >> 1);
  710. }
  711. }
  712. the_end:
  713. emms_c();
  714. }
  715. static void encode_picture(MpegEncContext *s, int picture_number)
  716. {
  717. int mb_x, mb_y, wrap;
  718. UINT8 *ptr;
  719. int i, motion_x, motion_y;
  720. s->picture_number = picture_number;
  721. if (!s->fixed_qscale)
  722. s->qscale = rate_estimate_qscale(s);
  723. /* precompute matrix */
  724. if (s->out_format == FMT_MJPEG) {
  725. /* for mjpeg, we do include qscale in the matrix */
  726. s->intra_matrix[0] = default_intra_matrix[0];
  727. for(i=1;i<64;i++)
  728. s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
  729. convert_matrix(s->q_intra_matrix, s->intra_matrix, 8);
  730. } else {
  731. convert_matrix(s->q_intra_matrix, s->intra_matrix, s->qscale);
  732. convert_matrix(s->q_non_intra_matrix, s->non_intra_matrix, s->qscale);
  733. }
  734. switch(s->out_format) {
  735. case FMT_MJPEG:
  736. mjpeg_picture_header(s);
  737. break;
  738. case FMT_H263:
  739. if (s->h263_msmpeg4)
  740. msmpeg4_encode_picture_header(s, picture_number);
  741. else if (s->h263_pred)
  742. mpeg4_encode_picture_header(s, picture_number);
  743. else if (s->h263_rv10)
  744. rv10_encode_picture_header(s, picture_number);
  745. else
  746. h263_encode_picture_header(s, picture_number);
  747. break;
  748. case FMT_MPEG1:
  749. mpeg1_encode_picture_header(s, picture_number);
  750. break;
  751. }
  752. /* init last dc values */
  753. /* note: quant matrix value (8) is implied here */
  754. s->last_dc[0] = 128;
  755. s->last_dc[1] = 128;
  756. s->last_dc[2] = 128;
  757. s->mb_incr = 1;
  758. s->last_mv[0][0][0] = 0;
  759. s->last_mv[0][0][1] = 0;
  760. s->mv_type = MV_TYPE_16X16;
  761. s->mv_dir = MV_DIR_FORWARD;
  762. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  763. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  764. s->mb_x = mb_x;
  765. s->mb_y = mb_y;
  766. /* compute motion vector and macro block type (intra or non intra) */
  767. motion_x = 0;
  768. motion_y = 0;
  769. if (s->pict_type == P_TYPE) {
  770. s->mb_intra = estimate_motion(s, mb_x, mb_y,
  771. &motion_x,
  772. &motion_y);
  773. } else {
  774. s->mb_intra = 1;
  775. }
  776. /* get the pixels */
  777. wrap = s->linesize;
  778. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  779. get_pixels(s->block[0], ptr, wrap);
  780. get_pixels(s->block[1], ptr + 8, wrap);
  781. get_pixels(s->block[2], ptr + 8 * wrap, wrap);
  782. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  783. wrap = s->linesize >> 1;
  784. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  785. get_pixels(s->block[4], ptr, wrap);
  786. wrap = s->linesize >> 1;
  787. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  788. get_pixels(s->block[5], ptr, wrap);
  789. /* subtract previous frame if non intra */
  790. if (!s->mb_intra) {
  791. int dxy, offset, mx, my;
  792. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  793. ptr = s->last_picture[0] +
  794. ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
  795. (mb_x * 16 + (motion_x >> 1));
  796. sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
  797. sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
  798. sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
  799. sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
  800. if (s->out_format == FMT_H263) {
  801. /* special rounding for h263 */
  802. dxy = 0;
  803. if ((motion_x & 3) != 0)
  804. dxy |= 1;
  805. if ((motion_y & 3) != 0)
  806. dxy |= 2;
  807. mx = motion_x >> 2;
  808. my = motion_y >> 2;
  809. } else {
  810. mx = motion_x / 2;
  811. my = motion_y / 2;
  812. dxy = ((my & 1) << 1) | (mx & 1);
  813. mx >>= 1;
  814. my >>= 1;
  815. }
  816. offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
  817. ptr = s->last_picture[1] + offset;
  818. sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
  819. ptr = s->last_picture[2] + offset;
  820. sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
  821. }
  822. emms_c();
  823. /* DCT & quantize */
  824. if (s->h263_msmpeg4) {
  825. msmpeg4_dc_scale(s);
  826. } else if (s->h263_pred) {
  827. h263_dc_scale(s);
  828. } else {
  829. /* default quantization values */
  830. s->y_dc_scale = 8;
  831. s->c_dc_scale = 8;
  832. }
  833. for(i=0;i<6;i++) {
  834. int last_index;
  835. if (av_fdct == jpeg_fdct_ifast)
  836. last_index = dct_quantize(s, s->block[i], i, s->qscale);
  837. else
  838. last_index = dct_quantize_mmx(s, s->block[i], i, s->qscale);
  839. s->block_last_index[i] = last_index;
  840. }
  841. /* huffman encode */
  842. switch(s->out_format) {
  843. case FMT_MPEG1:
  844. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  845. break;
  846. case FMT_H263:
  847. if (s->h263_msmpeg4)
  848. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  849. else
  850. h263_encode_mb(s, s->block, motion_x, motion_y);
  851. break;
  852. case FMT_MJPEG:
  853. mjpeg_encode_mb(s, s->block);
  854. break;
  855. }
  856. /* decompress blocks so that we keep the state of the decoder */
  857. s->mv[0][0][0] = motion_x;
  858. s->mv[0][0][1] = motion_y;
  859. MPV_decode_mb(s, s->block);
  860. }
  861. }
  862. }
  863. static int dct_quantize(MpegEncContext *s,
  864. DCTELEM *block, int n,
  865. int qscale)
  866. {
  867. int i, j, level, last_non_zero, q;
  868. const int *qmat;
  869. av_fdct (block);
  870. if (s->mb_intra) {
  871. if (n < 4)
  872. q = s->y_dc_scale;
  873. else
  874. q = s->c_dc_scale;
  875. q = q << 3;
  876. /* note: block[0] is assumed to be positive */
  877. block[0] = (block[0] + (q >> 1)) / q;
  878. i = 1;
  879. last_non_zero = 0;
  880. if (s->out_format == FMT_H263) {
  881. qmat = s->q_non_intra_matrix;
  882. } else {
  883. qmat = s->q_intra_matrix;
  884. }
  885. } else {
  886. i = 0;
  887. last_non_zero = -1;
  888. qmat = s->q_non_intra_matrix;
  889. }
  890. for(;i<64;i++) {
  891. j = zigzag_direct[i];
  892. level = block[j];
  893. level = level * qmat[j];
  894. #ifdef PARANOID
  895. {
  896. static int count = 0;
  897. int level1, level2, qmat1;
  898. double val;
  899. if (qmat == s->q_non_intra_matrix) {
  900. qmat1 = default_non_intra_matrix[j] * s->qscale;
  901. } else {
  902. qmat1 = default_intra_matrix[j] * s->qscale;
  903. }
  904. if (av_fdct != jpeg_fdct_ifast)
  905. val = ((double)block[j] * 8.0) / (double)qmat1;
  906. else
  907. val = ((double)block[j] * 8.0 * 2048.0) /
  908. ((double)qmat1 * aanscales[j]);
  909. level1 = (int)val;
  910. level2 = level / (1 << (QMAT_SHIFT - 3));
  911. if (level1 != level2) {
  912. fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
  913. count, level2, level1, block[j], qmat1, qmat[j],
  914. val);
  915. count++;
  916. }
  917. }
  918. #endif
  919. /* XXX: slight error for the low range. Test should be equivalent to
  920. (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
  921. (QMAT_SHIFT - 3)))
  922. */
  923. if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
  924. level) {
  925. level = level / (1 << (QMAT_SHIFT - 3));
  926. /* XXX: currently, this code is not optimal. the range should be:
  927. mpeg1: -255..255
  928. mpeg2: -2048..2047
  929. h263: -128..127
  930. mpeg4: -2048..2047
  931. */
  932. if (level > 127)
  933. level = 127;
  934. else if (level < -128)
  935. level = -128;
  936. block[j] = level;
  937. last_non_zero = i;
  938. } else {
  939. block[j] = 0;
  940. }
  941. }
  942. return last_non_zero;
  943. }
  944. static int dct_quantize_mmx(MpegEncContext *s,
  945. DCTELEM *block, int n,
  946. int qscale)
  947. {
  948. int i, j, level, last_non_zero, q;
  949. const int *qmat;
  950. av_fdct (block);
  951. /* we need this permutation so that we correct the IDCT
  952. permutation. will be moved into DCT code */
  953. block_permute(block);
  954. if (s->mb_intra) {
  955. if (n < 4)
  956. q = s->y_dc_scale;
  957. else
  958. q = s->c_dc_scale;
  959. /* note: block[0] is assumed to be positive */
  960. block[0] = (block[0] + (q >> 1)) / q;
  961. i = 1;
  962. last_non_zero = 0;
  963. if (s->out_format == FMT_H263) {
  964. qmat = s->q_non_intra_matrix;
  965. } else {
  966. qmat = s->q_intra_matrix;
  967. }
  968. } else {
  969. i = 0;
  970. last_non_zero = -1;
  971. qmat = s->q_non_intra_matrix;
  972. }
  973. for(;i<64;i++) {
  974. j = zigzag_direct[i];
  975. level = block[j];
  976. level = level * qmat[j];
  977. /* XXX: slight error for the low range. Test should be equivalent to
  978. (level <= -(1 << (QMAT_SHIFT_MMX - 3)) || level >= (1 <<
  979. (QMAT_SHIFT_MMX - 3)))
  980. */
  981. if (((level << (31 - (QMAT_SHIFT_MMX - 3))) >> (31 - (QMAT_SHIFT_MMX - 3))) !=
  982. level) {
  983. level = level / (1 << (QMAT_SHIFT_MMX - 3));
  984. /* XXX: currently, this code is not optimal. the range should be:
  985. mpeg1: -255..255
  986. mpeg2: -2048..2047
  987. h263: -128..127
  988. mpeg4: -2048..2047
  989. */
  990. if (level > 127)
  991. level = 127;
  992. else if (level < -128)
  993. level = -128;
  994. block[j] = level;
  995. last_non_zero = i;
  996. } else {
  997. block[j] = 0;
  998. }
  999. }
  1000. return last_non_zero;
  1001. }
  1002. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  1003. DCTELEM *block, int n, int qscale)
  1004. {
  1005. int i, level;
  1006. const UINT16 *quant_matrix;
  1007. if (s->mb_intra) {
  1008. if (n < 4)
  1009. block[0] = block[0] * s->y_dc_scale;
  1010. else
  1011. block[0] = block[0] * s->c_dc_scale;
  1012. /* XXX: only mpeg1 */
  1013. quant_matrix = s->intra_matrix;
  1014. for(i=1;i<64;i++) {
  1015. level = block[i];
  1016. if (level) {
  1017. if (level < 0) {
  1018. level = -level;
  1019. level = (int)(level * qscale * quant_matrix[i]) >> 3;
  1020. level = (level - 1) | 1;
  1021. level = -level;
  1022. } else {
  1023. level = (int)(level * qscale * quant_matrix[i]) >> 3;
  1024. level = (level - 1) | 1;
  1025. }
  1026. #ifdef PARANOID
  1027. if (level < -2048 || level > 2047)
  1028. fprintf(stderr, "unquant error %d %d\n", i, level);
  1029. #endif
  1030. block[i] = level;
  1031. }
  1032. }
  1033. } else {
  1034. i = 0;
  1035. quant_matrix = s->non_intra_matrix;
  1036. for(;i<64;i++) {
  1037. level = block[i];
  1038. if (level) {
  1039. if (level < 0) {
  1040. level = -level;
  1041. level = (((level << 1) + 1) * qscale *
  1042. ((int) (quant_matrix[i]))) >> 4;
  1043. level = (level - 1) | 1;
  1044. level = -level;
  1045. } else {
  1046. level = (((level << 1) + 1) * qscale *
  1047. ((int) (quant_matrix[i]))) >> 4;
  1048. level = (level - 1) | 1;
  1049. }
  1050. #ifdef PARANOID
  1051. if (level < -2048 || level > 2047)
  1052. fprintf(stderr, "unquant error %d %d\n", i, level);
  1053. #endif
  1054. block[i] = level;
  1055. }
  1056. }
  1057. }
  1058. }
  1059. static void dct_unquantize_h263_c(MpegEncContext *s,
  1060. DCTELEM *block, int n, int qscale)
  1061. {
  1062. int i, level, qmul, qadd;
  1063. if (s->mb_intra) {
  1064. if (n < 4)
  1065. block[0] = block[0] * s->y_dc_scale;
  1066. else
  1067. block[0] = block[0] * s->c_dc_scale;
  1068. i = 1;
  1069. } else {
  1070. i = 0;
  1071. }
  1072. qmul = s->qscale << 1;
  1073. qadd = (s->qscale - 1) | 1;
  1074. for(;i<64;i++) {
  1075. level = block[i];
  1076. if (level) {
  1077. if (level < 0) {
  1078. level = level * qmul - qadd;
  1079. } else {
  1080. level = level * qmul + qadd;
  1081. }
  1082. #ifdef PARANOID
  1083. if (level < -2048 || level > 2047)
  1084. fprintf(stderr, "unquant error %d %d\n", i, level);
  1085. #endif
  1086. block[i] = level;
  1087. }
  1088. }
  1089. }
  1090. /* rate control */
  1091. /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
  1092. #define I_FRAME_SIZE_RATIO 3.0
  1093. #define QSCALE_K 20
  1094. static void rate_control_init(MpegEncContext *s)
  1095. {
  1096. s->wanted_bits = 0;
  1097. if (s->intra_only) {
  1098. s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
  1099. s->P_frame_bits = s->I_frame_bits;
  1100. } else {
  1101. s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
  1102. (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
  1103. s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
  1104. }
  1105. #if defined(DEBUG)
  1106. printf("I_frame_size=%d P_frame_size=%d\n",
  1107. s->I_frame_bits, s->P_frame_bits);
  1108. #endif
  1109. }
  1110. /*
  1111. * This heuristic is rather poor, but at least we do not have to
  1112. * change the qscale at every macroblock.
  1113. */
  1114. static int rate_estimate_qscale(MpegEncContext *s)
  1115. {
  1116. long long total_bits = s->total_bits;
  1117. float q;
  1118. int qscale, diff, qmin;
  1119. if (s->pict_type == I_TYPE) {
  1120. s->wanted_bits += s->I_frame_bits;
  1121. } else {
  1122. s->wanted_bits += s->P_frame_bits;
  1123. }
  1124. diff = s->wanted_bits - total_bits;
  1125. q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
  1126. /* adjust for I frame */
  1127. if (s->pict_type == I_TYPE && !s->intra_only) {
  1128. q /= I_FRAME_SIZE_RATIO;
  1129. }
  1130. /* using a too small Q scale leeds to problems in mpeg1 and h263
  1131. because AC coefficients are clamped to 255 or 127 */
  1132. qmin = 3;
  1133. if (q < qmin)
  1134. q = qmin;
  1135. else if (q > 31)
  1136. q = 31;
  1137. qscale = (int)(q + 0.5);
  1138. #if defined(DEBUG)
  1139. printf("%d: total=%Ld br=%0.1f diff=%d qest=%0.1f\n",
  1140. s->picture_number,
  1141. total_bits,
  1142. (float)s->frame_rate / FRAME_RATE_BASE *
  1143. total_bits / s->picture_number,
  1144. diff, q);
  1145. #endif
  1146. return qscale;
  1147. }
  1148. AVCodec mpeg1video_encoder = {
  1149. "mpeg1video",
  1150. CODEC_TYPE_VIDEO,
  1151. CODEC_ID_MPEG1VIDEO,
  1152. sizeof(MpegEncContext),
  1153. MPV_encode_init,
  1154. MPV_encode_picture,
  1155. MPV_encode_end,
  1156. };
  1157. AVCodec h263_encoder = {
  1158. "h263",
  1159. CODEC_TYPE_VIDEO,
  1160. CODEC_ID_H263,
  1161. sizeof(MpegEncContext),
  1162. MPV_encode_init,
  1163. MPV_encode_picture,
  1164. MPV_encode_end,
  1165. };
  1166. AVCodec h263p_encoder = {
  1167. "h263p",
  1168. CODEC_TYPE_VIDEO,
  1169. CODEC_ID_H263P,
  1170. sizeof(MpegEncContext),
  1171. MPV_encode_init,
  1172. MPV_encode_picture,
  1173. MPV_encode_end,
  1174. };
  1175. AVCodec rv10_encoder = {
  1176. "rv10",
  1177. CODEC_TYPE_VIDEO,
  1178. CODEC_ID_RV10,
  1179. sizeof(MpegEncContext),
  1180. MPV_encode_init,
  1181. MPV_encode_picture,
  1182. MPV_encode_end,
  1183. };
  1184. AVCodec mjpeg_encoder = {
  1185. "mjpeg",
  1186. CODEC_TYPE_VIDEO,
  1187. CODEC_ID_MJPEG,
  1188. sizeof(MpegEncContext),
  1189. MPV_encode_init,
  1190. MPV_encode_picture,
  1191. MPV_encode_end,
  1192. };
  1193. AVCodec opendivx_encoder = {
  1194. "opendivx",
  1195. CODEC_TYPE_VIDEO,
  1196. CODEC_ID_OPENDIVX,
  1197. sizeof(MpegEncContext),
  1198. MPV_encode_init,
  1199. MPV_encode_picture,
  1200. MPV_encode_end,
  1201. };
  1202. AVCodec msmpeg4_encoder = {
  1203. "msmpeg4",
  1204. CODEC_TYPE_VIDEO,
  1205. CODEC_ID_MSMPEG4,
  1206. sizeof(MpegEncContext),
  1207. MPV_encode_init,
  1208. MPV_encode_picture,
  1209. MPV_encode_end,
  1210. };