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.

1363 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)((UINT64_C(1) << (QMAT_SHIFT + 11)) /
  73. (aanscales[i] * qscale * quant_matrix[i]));
  74. }
  75. } else {
  76. for(i=0;i<64;i++) {
  77. /* We can safely suppose that 16 <= quant_matrix[i] <= 255
  78. So 16 <= qscale * quant_matrix[i] <= 7905
  79. so (1 << QMAT_SHIFT) / 16 >= qmat[i] >= (1 << QMAT_SHIFT) / 7905
  80. */
  81. qmat[i] = (1 << QMAT_SHIFT_MMX) / (qscale * quant_matrix[i]);
  82. }
  83. }
  84. }
  85. /* init common structure for both encoder and decoder */
  86. int MPV_common_init(MpegEncContext *s)
  87. {
  88. int c_size, i;
  89. UINT8 *pict;
  90. if (s->out_format == FMT_H263)
  91. s->dct_unquantize = dct_unquantize_h263_c;
  92. else
  93. s->dct_unquantize = dct_unquantize_mpeg1_c;
  94. #ifdef HAVE_MMX
  95. MPV_common_init_mmx(s);
  96. #endif
  97. s->mb_width = (s->width + 15) / 16;
  98. s->mb_height = (s->height + 15) / 16;
  99. s->linesize = s->mb_width * 16 + 2 * EDGE_WIDTH;
  100. for(i=0;i<3;i++) {
  101. int w, h, shift, pict_start;
  102. w = s->linesize;
  103. h = s->mb_height * 16 + 2 * EDGE_WIDTH;
  104. shift = (i == 0) ? 0 : 1;
  105. c_size = (w >> shift) * (h >> shift);
  106. pict_start = (w >> shift) * (EDGE_WIDTH >> shift) + (EDGE_WIDTH >> shift);
  107. pict = av_mallocz(c_size);
  108. if (pict == NULL)
  109. goto fail;
  110. s->last_picture_base[i] = pict;
  111. s->last_picture[i] = pict + pict_start;
  112. pict = av_mallocz(c_size);
  113. if (pict == NULL)
  114. goto fail;
  115. s->next_picture_base[i] = pict;
  116. s->next_picture[i] = pict + pict_start;
  117. if (s->has_b_frames) {
  118. pict = av_mallocz(c_size);
  119. if (pict == NULL)
  120. goto fail;
  121. s->aux_picture_base[i] = pict;
  122. s->aux_picture[i] = pict + pict_start;
  123. }
  124. }
  125. if (s->out_format == FMT_H263) {
  126. int size;
  127. /* MV prediction */
  128. size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  129. s->motion_val = malloc(size * 2 * sizeof(INT16));
  130. if (s->motion_val == NULL)
  131. goto fail;
  132. memset(s->motion_val, 0, size * 2 * sizeof(INT16));
  133. }
  134. if (s->h263_pred) {
  135. int y_size, c_size, i, size;
  136. /* dc values */
  137. y_size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
  138. c_size = (s->mb_width + 2) * (s->mb_height + 2);
  139. size = y_size + 2 * c_size;
  140. s->dc_val[0] = malloc(size * sizeof(INT16));
  141. if (s->dc_val[0] == NULL)
  142. goto fail;
  143. s->dc_val[1] = s->dc_val[0] + y_size;
  144. s->dc_val[2] = s->dc_val[1] + c_size;
  145. for(i=0;i<size;i++)
  146. s->dc_val[0][i] = 1024;
  147. /* ac values */
  148. s->ac_val[0] = av_mallocz(size * sizeof(INT16) * 16);
  149. if (s->ac_val[0] == NULL)
  150. goto fail;
  151. s->ac_val[1] = s->ac_val[0] + y_size;
  152. s->ac_val[2] = s->ac_val[1] + c_size;
  153. /* cbp values */
  154. s->coded_block = av_mallocz(y_size);
  155. if (!s->coded_block)
  156. goto fail;
  157. }
  158. /* default structure is frame */
  159. s->picture_structure = PICT_FRAME;
  160. /* init macroblock skip table */
  161. if (!s->encoding) {
  162. s->mbskip_table = av_mallocz(s->mb_width * s->mb_height);
  163. if (!s->mbskip_table)
  164. goto fail;
  165. }
  166. s->context_initialized = 1;
  167. return 0;
  168. fail:
  169. if (s->motion_val)
  170. free(s->motion_val);
  171. if (s->dc_val[0])
  172. free(s->dc_val[0]);
  173. if (s->ac_val[0])
  174. free(s->ac_val[0]);
  175. if (s->coded_block)
  176. free(s->coded_block);
  177. if (s->mbskip_table)
  178. free(s->mbskip_table);
  179. for(i=0;i<3;i++) {
  180. if (s->last_picture_base[i])
  181. free(s->last_picture_base[i]);
  182. if (s->next_picture_base[i])
  183. free(s->next_picture_base[i]);
  184. if (s->aux_picture_base[i])
  185. free(s->aux_picture_base[i]);
  186. }
  187. return -1;
  188. }
  189. /* init common structure for both encoder and decoder */
  190. void MPV_common_end(MpegEncContext *s)
  191. {
  192. int i;
  193. if (s->motion_val)
  194. free(s->motion_val);
  195. if (s->h263_pred) {
  196. free(s->dc_val[0]);
  197. free(s->ac_val[0]);
  198. free(s->coded_block);
  199. }
  200. if (s->mbskip_table)
  201. free(s->mbskip_table);
  202. for(i=0;i<3;i++) {
  203. free(s->last_picture_base[i]);
  204. free(s->next_picture_base[i]);
  205. if (s->has_b_frames)
  206. free(s->aux_picture_base[i]);
  207. }
  208. s->context_initialized = 0;
  209. }
  210. /* init video encoder */
  211. int MPV_encode_init(AVCodecContext *avctx)
  212. {
  213. MpegEncContext *s = avctx->priv_data;
  214. int i;
  215. s->bit_rate = avctx->bit_rate;
  216. s->frame_rate = avctx->frame_rate;
  217. s->width = avctx->width;
  218. s->height = avctx->height;
  219. s->gop_size = avctx->gop_size;
  220. if (s->gop_size <= 1) {
  221. s->intra_only = 1;
  222. s->gop_size = 12;
  223. } else {
  224. s->intra_only = 0;
  225. }
  226. s->full_search = motion_estimation_method;
  227. s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
  228. switch(avctx->codec->id) {
  229. case CODEC_ID_MPEG1VIDEO:
  230. s->out_format = FMT_MPEG1;
  231. break;
  232. case CODEC_ID_MJPEG:
  233. s->out_format = FMT_MJPEG;
  234. s->intra_only = 1; /* force intra only for jpeg */
  235. if (mjpeg_init(s) < 0)
  236. return -1;
  237. break;
  238. case CODEC_ID_H263:
  239. if (h263_get_picture_format(s->width, s->height) == 7)
  240. return -1;
  241. s->out_format = FMT_H263;
  242. break;
  243. case CODEC_ID_H263P:
  244. s->out_format = FMT_H263;
  245. s->h263_plus = 1;
  246. /* XXX: not unrectricted mv yet */
  247. break;
  248. case CODEC_ID_RV10:
  249. s->out_format = FMT_H263;
  250. s->h263_rv10 = 1;
  251. break;
  252. case CODEC_ID_MPEG4:
  253. s->out_format = FMT_H263;
  254. s->h263_pred = 1;
  255. s->unrestricted_mv = 1;
  256. break;
  257. case CODEC_ID_MSMPEG4:
  258. s->out_format = FMT_H263;
  259. s->h263_msmpeg4 = 1;
  260. s->h263_pred = 1;
  261. s->unrestricted_mv = 1;
  262. break;
  263. default:
  264. return -1;
  265. }
  266. if (s->out_format == FMT_H263)
  267. h263_encode_init_vlc(s);
  268. s->encoding = 1;
  269. /* init */
  270. if (MPV_common_init(s) < 0)
  271. return -1;
  272. /* init default q matrix */
  273. for(i=0;i<64;i++) {
  274. s->intra_matrix[i] = default_intra_matrix[i];
  275. s->non_intra_matrix[i] = default_non_intra_matrix[i];
  276. }
  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. /* we need this permutation so that we correct the IDCT
  871. permutation. will be moved into DCT code */
  872. block_permute(block);
  873. if (s->mb_intra) {
  874. if (n < 4)
  875. q = s->y_dc_scale;
  876. else
  877. q = s->c_dc_scale;
  878. q = q << 3;
  879. /* note: block[0] is assumed to be positive */
  880. block[0] = (block[0] + (q >> 1)) / q;
  881. i = 1;
  882. last_non_zero = 0;
  883. if (s->out_format == FMT_H263) {
  884. qmat = s->q_non_intra_matrix;
  885. } else {
  886. qmat = s->q_intra_matrix;
  887. }
  888. } else {
  889. i = 0;
  890. last_non_zero = -1;
  891. qmat = s->q_non_intra_matrix;
  892. }
  893. for(;i<64;i++) {
  894. j = zigzag_direct[i];
  895. level = block[j];
  896. level = level * qmat[j];
  897. #ifdef PARANOID
  898. {
  899. static int count = 0;
  900. int level1, level2, qmat1;
  901. double val;
  902. if (qmat == s->q_non_intra_matrix) {
  903. qmat1 = default_non_intra_matrix[j] * s->qscale;
  904. } else {
  905. qmat1 = default_intra_matrix[j] * s->qscale;
  906. }
  907. if (av_fdct != jpeg_fdct_ifast)
  908. val = ((double)block[j] * 8.0) / (double)qmat1;
  909. else
  910. val = ((double)block[j] * 8.0 * 2048.0) /
  911. ((double)qmat1 * aanscales[j]);
  912. level1 = (int)val;
  913. level2 = level / (1 << (QMAT_SHIFT - 3));
  914. if (level1 != level2) {
  915. fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
  916. count, level2, level1, block[j], qmat1, qmat[j],
  917. val);
  918. count++;
  919. }
  920. }
  921. #endif
  922. /* XXX: slight error for the low range. Test should be equivalent to
  923. (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
  924. (QMAT_SHIFT - 3)))
  925. */
  926. if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
  927. level) {
  928. level = level / (1 << (QMAT_SHIFT - 3));
  929. /* XXX: currently, this code is not optimal. the range should be:
  930. mpeg1: -255..255
  931. mpeg2: -2048..2047
  932. h263: -128..127
  933. mpeg4: -2048..2047
  934. */
  935. if (level > 127)
  936. level = 127;
  937. else if (level < -128)
  938. level = -128;
  939. block[j] = level;
  940. last_non_zero = i;
  941. } else {
  942. block[j] = 0;
  943. }
  944. }
  945. return last_non_zero;
  946. }
  947. static int dct_quantize_mmx(MpegEncContext *s,
  948. DCTELEM *block, int n,
  949. int qscale)
  950. {
  951. int i, j, level, last_non_zero, q;
  952. const int *qmat;
  953. av_fdct (block);
  954. /* we need this permutation so that we correct the IDCT
  955. permutation. will be moved into DCT code */
  956. block_permute(block);
  957. if (s->mb_intra) {
  958. if (n < 4)
  959. q = s->y_dc_scale;
  960. else
  961. q = s->c_dc_scale;
  962. /* note: block[0] is assumed to be positive */
  963. block[0] = (block[0] + (q >> 1)) / q;
  964. i = 1;
  965. last_non_zero = 0;
  966. if (s->out_format == FMT_H263) {
  967. qmat = s->q_non_intra_matrix;
  968. } else {
  969. qmat = s->q_intra_matrix;
  970. }
  971. } else {
  972. i = 0;
  973. last_non_zero = -1;
  974. qmat = s->q_non_intra_matrix;
  975. }
  976. for(;i<64;i++) {
  977. j = zigzag_direct[i];
  978. level = block[j];
  979. level = level * qmat[j];
  980. /* XXX: slight error for the low range. Test should be equivalent to
  981. (level <= -(1 << (QMAT_SHIFT_MMX - 3)) || level >= (1 <<
  982. (QMAT_SHIFT_MMX - 3)))
  983. */
  984. if (((level << (31 - (QMAT_SHIFT_MMX - 3))) >> (31 - (QMAT_SHIFT_MMX - 3))) !=
  985. level) {
  986. level = level / (1 << (QMAT_SHIFT_MMX - 3));
  987. /* XXX: currently, this code is not optimal. the range should be:
  988. mpeg1: -255..255
  989. mpeg2: -2048..2047
  990. h263: -128..127
  991. mpeg4: -2048..2047
  992. */
  993. if (level > 127)
  994. level = 127;
  995. else if (level < -128)
  996. level = -128;
  997. block[j] = level;
  998. last_non_zero = i;
  999. } else {
  1000. block[j] = 0;
  1001. }
  1002. }
  1003. return last_non_zero;
  1004. }
  1005. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  1006. DCTELEM *block, int n, int qscale)
  1007. {
  1008. int i, level;
  1009. const UINT16 *quant_matrix;
  1010. if (s->mb_intra) {
  1011. if (n < 4)
  1012. block[0] = block[0] * s->y_dc_scale;
  1013. else
  1014. block[0] = block[0] * s->c_dc_scale;
  1015. /* XXX: only mpeg1 */
  1016. quant_matrix = s->intra_matrix;
  1017. for(i=1;i<64;i++) {
  1018. level = block[i];
  1019. if (level) {
  1020. if (level < 0) {
  1021. level = -level;
  1022. level = (int)(level * qscale * quant_matrix[i]) >> 3;
  1023. level = (level - 1) | 1;
  1024. level = -level;
  1025. } else {
  1026. level = (int)(level * qscale * quant_matrix[i]) >> 3;
  1027. level = (level - 1) | 1;
  1028. }
  1029. #ifdef PARANOID
  1030. if (level < -2048 || level > 2047)
  1031. fprintf(stderr, "unquant error %d %d\n", i, level);
  1032. #endif
  1033. block[i] = level;
  1034. }
  1035. }
  1036. } else {
  1037. i = 0;
  1038. quant_matrix = s->non_intra_matrix;
  1039. for(;i<64;i++) {
  1040. level = block[i];
  1041. if (level) {
  1042. if (level < 0) {
  1043. level = -level;
  1044. level = (((level << 1) + 1) * qscale *
  1045. ((int) (quant_matrix[i]))) >> 4;
  1046. level = (level - 1) | 1;
  1047. level = -level;
  1048. } else {
  1049. level = (((level << 1) + 1) * qscale *
  1050. ((int) (quant_matrix[i]))) >> 4;
  1051. level = (level - 1) | 1;
  1052. }
  1053. #ifdef PARANOID
  1054. if (level < -2048 || level > 2047)
  1055. fprintf(stderr, "unquant error %d %d\n", i, level);
  1056. #endif
  1057. block[i] = level;
  1058. }
  1059. }
  1060. }
  1061. }
  1062. static void dct_unquantize_h263_c(MpegEncContext *s,
  1063. DCTELEM *block, int n, int qscale)
  1064. {
  1065. int i, level, qmul, qadd;
  1066. if (s->mb_intra) {
  1067. if (n < 4)
  1068. block[0] = block[0] * s->y_dc_scale;
  1069. else
  1070. block[0] = block[0] * s->c_dc_scale;
  1071. i = 1;
  1072. } else {
  1073. i = 0;
  1074. }
  1075. qmul = s->qscale << 1;
  1076. qadd = (s->qscale - 1) | 1;
  1077. for(;i<64;i++) {
  1078. level = block[i];
  1079. if (level) {
  1080. if (level < 0) {
  1081. level = level * qmul - qadd;
  1082. } else {
  1083. level = level * qmul + qadd;
  1084. }
  1085. #ifdef PARANOID
  1086. if (level < -2048 || level > 2047)
  1087. fprintf(stderr, "unquant error %d %d\n", i, level);
  1088. #endif
  1089. block[i] = level;
  1090. }
  1091. }
  1092. }
  1093. /* rate control */
  1094. /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
  1095. #define I_FRAME_SIZE_RATIO 3.0
  1096. #define QSCALE_K 20
  1097. static void rate_control_init(MpegEncContext *s)
  1098. {
  1099. s->wanted_bits = 0;
  1100. if (s->intra_only) {
  1101. s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
  1102. s->P_frame_bits = s->I_frame_bits;
  1103. } else {
  1104. s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
  1105. (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
  1106. s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
  1107. }
  1108. #if defined(DEBUG)
  1109. printf("I_frame_size=%d P_frame_size=%d\n",
  1110. s->I_frame_bits, s->P_frame_bits);
  1111. #endif
  1112. }
  1113. /*
  1114. * This heuristic is rather poor, but at least we do not have to
  1115. * change the qscale at every macroblock.
  1116. */
  1117. static int rate_estimate_qscale(MpegEncContext *s)
  1118. {
  1119. INT64 total_bits = s->total_bits;
  1120. float q;
  1121. int qscale, diff, qmin;
  1122. if (s->pict_type == I_TYPE) {
  1123. s->wanted_bits += s->I_frame_bits;
  1124. } else {
  1125. s->wanted_bits += s->P_frame_bits;
  1126. }
  1127. diff = s->wanted_bits - total_bits;
  1128. q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
  1129. /* adjust for I frame */
  1130. if (s->pict_type == I_TYPE && !s->intra_only) {
  1131. q /= I_FRAME_SIZE_RATIO;
  1132. }
  1133. /* using a too small Q scale leeds to problems in mpeg1 and h263
  1134. because AC coefficients are clamped to 255 or 127 */
  1135. qmin = 3;
  1136. if (q < qmin)
  1137. q = qmin;
  1138. else if (q > 31)
  1139. q = 31;
  1140. qscale = (int)(q + 0.5);
  1141. #if defined(DEBUG)
  1142. printf("%d: total=%0.0f br=%0.1f diff=%d qest=%0.1f\n",
  1143. s->picture_number,
  1144. (double)total_bits,
  1145. (float)s->frame_rate / FRAME_RATE_BASE *
  1146. total_bits / s->picture_number,
  1147. diff, q);
  1148. #endif
  1149. return qscale;
  1150. }
  1151. AVCodec mpeg1video_encoder = {
  1152. "mpeg1video",
  1153. CODEC_TYPE_VIDEO,
  1154. CODEC_ID_MPEG1VIDEO,
  1155. sizeof(MpegEncContext),
  1156. MPV_encode_init,
  1157. MPV_encode_picture,
  1158. MPV_encode_end,
  1159. };
  1160. AVCodec h263_encoder = {
  1161. "h263",
  1162. CODEC_TYPE_VIDEO,
  1163. CODEC_ID_H263,
  1164. sizeof(MpegEncContext),
  1165. MPV_encode_init,
  1166. MPV_encode_picture,
  1167. MPV_encode_end,
  1168. };
  1169. AVCodec h263p_encoder = {
  1170. "h263p",
  1171. CODEC_TYPE_VIDEO,
  1172. CODEC_ID_H263P,
  1173. sizeof(MpegEncContext),
  1174. MPV_encode_init,
  1175. MPV_encode_picture,
  1176. MPV_encode_end,
  1177. };
  1178. AVCodec rv10_encoder = {
  1179. "rv10",
  1180. CODEC_TYPE_VIDEO,
  1181. CODEC_ID_RV10,
  1182. sizeof(MpegEncContext),
  1183. MPV_encode_init,
  1184. MPV_encode_picture,
  1185. MPV_encode_end,
  1186. };
  1187. AVCodec mjpeg_encoder = {
  1188. "mjpeg",
  1189. CODEC_TYPE_VIDEO,
  1190. CODEC_ID_MJPEG,
  1191. sizeof(MpegEncContext),
  1192. MPV_encode_init,
  1193. MPV_encode_picture,
  1194. MPV_encode_end,
  1195. };
  1196. AVCodec mpeg4_encoder = {
  1197. "mpeg4",
  1198. CODEC_TYPE_VIDEO,
  1199. CODEC_ID_MPEG4,
  1200. sizeof(MpegEncContext),
  1201. MPV_encode_init,
  1202. MPV_encode_picture,
  1203. MPV_encode_end,
  1204. };
  1205. AVCodec msmpeg4_encoder = {
  1206. "msmpeg4",
  1207. CODEC_TYPE_VIDEO,
  1208. CODEC_ID_MSMPEG4,
  1209. sizeof(MpegEncContext),
  1210. MPV_encode_init,
  1211. MPV_encode_picture,
  1212. MPV_encode_end,
  1213. };