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.

1353 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. if (s->pict_type == B_TYPE) {
  328. for(i=0;i<3;i++) {
  329. s->current_picture[i] = s->aux_picture[i];
  330. }
  331. } else {
  332. for(i=0;i<3;i++) {
  333. /* swap next and last */
  334. tmp = s->last_picture[i];
  335. s->last_picture[i] = s->next_picture[i];
  336. s->next_picture[i] = tmp;
  337. s->current_picture[i] = tmp;
  338. }
  339. }
  340. }
  341. /* generic function for encode/decode called after a frame has been coded/decoded */
  342. void MPV_frame_end(MpegEncContext *s)
  343. {
  344. /* draw edge for correct motion prediction if outside */
  345. if (s->pict_type != B_TYPE) {
  346. draw_edges(s->current_picture[0], s->linesize, s->width, s->height, EDGE_WIDTH);
  347. draw_edges(s->current_picture[1], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  348. draw_edges(s->current_picture[2], s->linesize/2, s->width/2, s->height/2, EDGE_WIDTH/2);
  349. }
  350. }
  351. int MPV_encode_picture(AVCodecContext *avctx,
  352. unsigned char *buf, int buf_size, void *data)
  353. {
  354. MpegEncContext *s = avctx->priv_data;
  355. AVPicture *pict = data;
  356. int i, j;
  357. if (s->fixed_qscale)
  358. s->qscale = avctx->quality;
  359. init_put_bits(&s->pb, buf, buf_size, NULL, NULL);
  360. if (!s->intra_only) {
  361. /* first picture of GOP is intra */
  362. if ((s->picture_number % s->gop_size) == 0)
  363. s->pict_type = I_TYPE;
  364. else
  365. s->pict_type = P_TYPE;
  366. } else {
  367. s->pict_type = I_TYPE;
  368. }
  369. avctx->key_frame = (s->pict_type == I_TYPE);
  370. MPV_frame_start(s);
  371. for(i=0;i<3;i++) {
  372. UINT8 *src = pict->data[i];
  373. UINT8 *dest = s->current_picture[i];
  374. int src_wrap = pict->linesize[i];
  375. int dest_wrap = s->linesize;
  376. int w = s->width;
  377. int h = s->height;
  378. if (i >= 1) {
  379. dest_wrap >>= 1;
  380. w >>= 1;
  381. h >>= 1;
  382. }
  383. for(j=0;j<h;j++) {
  384. memcpy(dest, src, w);
  385. dest += dest_wrap;
  386. src += src_wrap;
  387. }
  388. s->new_picture[i] = s->current_picture[i];
  389. }
  390. encode_picture(s, s->picture_number);
  391. MPV_frame_end(s);
  392. s->picture_number++;
  393. if (s->out_format == FMT_MJPEG)
  394. mjpeg_picture_trailer(s);
  395. flush_put_bits(&s->pb);
  396. s->total_bits += (s->pb.buf_ptr - s->pb.buf) * 8;
  397. avctx->quality = s->qscale;
  398. return s->pb.buf_ptr - s->pb.buf;
  399. }
  400. static inline int clip(int a, int amin, int amax)
  401. {
  402. if (a < amin)
  403. return amin;
  404. else if (a > amax)
  405. return amax;
  406. else
  407. return a;
  408. }
  409. /* apply one mpeg motion vector to the three components */
  410. static inline void mpeg_motion(MpegEncContext *s,
  411. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  412. int dest_offset,
  413. UINT8 **ref_picture, int src_offset,
  414. int field_based, op_pixels_func *pix_op,
  415. int motion_x, int motion_y, int h)
  416. {
  417. UINT8 *ptr;
  418. int dxy, offset, mx, my, src_x, src_y, height, linesize;
  419. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  420. src_x = s->mb_x * 16 + (motion_x >> 1);
  421. src_y = s->mb_y * (16 >> field_based) + (motion_y >> 1);
  422. /* WARNING: do no forget half pels */
  423. height = s->height >> field_based;
  424. src_x = clip(src_x, -16, s->width);
  425. if (src_x == s->width)
  426. dxy &= ~1;
  427. src_y = clip(src_y, -16, height);
  428. if (src_y == height)
  429. dxy &= ~2;
  430. linesize = s->linesize << field_based;
  431. ptr = ref_picture[0] + (src_y * linesize) + (src_x) + src_offset;
  432. dest_y += dest_offset;
  433. pix_op[dxy](dest_y, ptr, linesize, h);
  434. pix_op[dxy](dest_y + 8, ptr + 8, linesize, h);
  435. if (s->out_format == FMT_H263) {
  436. dxy = 0;
  437. if ((motion_x & 3) != 0)
  438. dxy |= 1;
  439. if ((motion_y & 3) != 0)
  440. dxy |= 2;
  441. mx = motion_x >> 2;
  442. my = motion_y >> 2;
  443. } else {
  444. mx = motion_x / 2;
  445. my = motion_y / 2;
  446. dxy = ((my & 1) << 1) | (mx & 1);
  447. mx >>= 1;
  448. my >>= 1;
  449. }
  450. src_x = s->mb_x * 8 + mx;
  451. src_y = s->mb_y * (8 >> field_based) + my;
  452. src_x = clip(src_x, -8, s->width >> 1);
  453. if (src_x == (s->width >> 1))
  454. dxy &= ~1;
  455. src_y = clip(src_y, -8, height >> 1);
  456. if (src_y == (height >> 1))
  457. dxy &= ~2;
  458. offset = (src_y * (linesize >> 1)) + src_x + (src_offset >> 1);
  459. ptr = ref_picture[1] + offset;
  460. pix_op[dxy](dest_cb + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  461. ptr = ref_picture[2] + offset;
  462. pix_op[dxy](dest_cr + (dest_offset >> 1), ptr, linesize >> 1, h >> 1);
  463. }
  464. static inline void MPV_motion(MpegEncContext *s,
  465. UINT8 *dest_y, UINT8 *dest_cb, UINT8 *dest_cr,
  466. int dir, UINT8 **ref_picture,
  467. op_pixels_func *pix_op)
  468. {
  469. int dxy, offset, mx, my, src_x, src_y, motion_x, motion_y;
  470. int mb_x, mb_y, i;
  471. UINT8 *ptr, *dest;
  472. mb_x = s->mb_x;
  473. mb_y = s->mb_y;
  474. switch(s->mv_type) {
  475. case MV_TYPE_16X16:
  476. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  477. ref_picture, 0,
  478. 0, pix_op,
  479. s->mv[dir][0][0], s->mv[dir][0][1], 16);
  480. break;
  481. case MV_TYPE_8X8:
  482. for(i=0;i<4;i++) {
  483. motion_x = s->mv[dir][i][0];
  484. motion_y = s->mv[dir][i][1];
  485. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  486. src_x = mb_x * 16 + (motion_x >> 1) + (i & 1) * 8;
  487. src_y = mb_y * 16 + (motion_y >> 1) + ((i >> 1) & 1) * 8;
  488. /* WARNING: do no forget half pels */
  489. src_x = clip(src_x, -16, s->width);
  490. if (src_x == s->width)
  491. dxy &= ~1;
  492. src_y = clip(src_y, -16, s->height);
  493. if (src_y == s->height)
  494. dxy &= ~2;
  495. ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
  496. dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
  497. pix_op[dxy](dest, ptr, s->linesize, 8);
  498. }
  499. /* In case of 8X8, we construct a single chroma motion vector
  500. with a special rounding */
  501. mx = 0;
  502. my = 0;
  503. for(i=0;i<4;i++) {
  504. mx += s->mv[dir][i][0];
  505. my += s->mv[dir][i][1];
  506. }
  507. if (mx >= 0)
  508. mx = (h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  509. else {
  510. mx = -mx;
  511. mx = -(h263_chroma_roundtab[mx & 0xf] + ((mx >> 3) & ~1));
  512. }
  513. if (my >= 0)
  514. my = (h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  515. else {
  516. my = -my;
  517. my = -(h263_chroma_roundtab[my & 0xf] + ((my >> 3) & ~1));
  518. }
  519. dxy = ((my & 1) << 1) | (mx & 1);
  520. mx >>= 1;
  521. my >>= 1;
  522. src_x = mb_x * 8 + mx;
  523. src_y = mb_y * 8 + my;
  524. src_x = clip(src_x, -8, s->width/2);
  525. if (src_x == s->width/2)
  526. dxy &= ~1;
  527. src_y = clip(src_y, -8, s->height/2);
  528. if (src_y == s->height/2)
  529. dxy &= ~2;
  530. offset = (src_y * (s->linesize >> 1)) + src_x;
  531. ptr = ref_picture[1] + offset;
  532. pix_op[dxy](dest_cb, ptr, s->linesize >> 1, 8);
  533. ptr = ref_picture[2] + offset;
  534. pix_op[dxy](dest_cr, ptr, s->linesize >> 1, 8);
  535. break;
  536. case MV_TYPE_FIELD:
  537. if (s->picture_structure == PICT_FRAME) {
  538. /* top field */
  539. mpeg_motion(s, dest_y, dest_cb, dest_cr, 0,
  540. ref_picture, s->field_select[dir][0] ? s->linesize : 0,
  541. 1, pix_op,
  542. s->mv[dir][0][0], s->mv[dir][0][1], 8);
  543. /* bottom field */
  544. mpeg_motion(s, dest_y, dest_cb, dest_cr, s->linesize,
  545. ref_picture, s->field_select[dir][1] ? s->linesize : 0,
  546. 1, pix_op,
  547. s->mv[dir][1][0], s->mv[dir][1][1], 8);
  548. } else {
  549. }
  550. break;
  551. }
  552. }
  553. /* put block[] to dest[] */
  554. static inline void put_dct(MpegEncContext *s,
  555. DCTELEM *block, int i, UINT8 *dest, int line_size)
  556. {
  557. if (!s->mpeg2)
  558. s->dct_unquantize(s, block, i, s->qscale);
  559. ff_idct (block);
  560. put_pixels_clamped(block, dest, line_size);
  561. }
  562. /* add block[] to dest[] */
  563. static inline void add_dct(MpegEncContext *s,
  564. DCTELEM *block, int i, UINT8 *dest, int line_size)
  565. {
  566. if (s->block_last_index[i] >= 0) {
  567. if (!s->mpeg2)
  568. s->dct_unquantize(s, block, i, s->qscale);
  569. ff_idct (block);
  570. add_pixels_clamped(block, dest, line_size);
  571. }
  572. }
  573. /* generic function called after a macroblock has been parsed by the
  574. decoder or after it has been encoded by the encoder.
  575. Important variables used:
  576. s->mb_intra : true if intra macroblock
  577. s->mv_dir : motion vector direction
  578. s->mv_type : motion vector type
  579. s->mv : motion vector
  580. s->interlaced_dct : true if interlaced dct used (mpeg2)
  581. */
  582. void MPV_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
  583. {
  584. int mb_x, mb_y, motion_x, motion_y;
  585. int dct_linesize, dct_offset;
  586. op_pixels_func *op_pix;
  587. mb_x = s->mb_x;
  588. mb_y = s->mb_y;
  589. /* update DC predictors for P macroblocks */
  590. if (!s->mb_intra) {
  591. if (s->h263_pred) {
  592. int wrap, x, y, v;
  593. wrap = 2 * s->mb_width + 2;
  594. v = 1024;
  595. x = 2 * mb_x + 1;
  596. y = 2 * mb_y + 1;
  597. s->dc_val[0][(x) + (y) * wrap] = v;
  598. s->dc_val[0][(x + 1) + (y) * wrap] = v;
  599. s->dc_val[0][(x) + (y + 1) * wrap] = v;
  600. s->dc_val[0][(x + 1) + (y + 1) * wrap] = v;
  601. /* ac pred */
  602. memset(s->ac_val[0][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
  603. memset(s->ac_val[0][(x + 1) + (y) * wrap], 0, 16 * sizeof(INT16));
  604. memset(s->ac_val[0][(x) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
  605. memset(s->ac_val[0][(x + 1) + (y + 1) * wrap], 0, 16 * sizeof(INT16));
  606. if (s->h263_msmpeg4) {
  607. s->coded_block[(x) + (y) * wrap] = 0;
  608. s->coded_block[(x + 1) + (y) * wrap] = 0;
  609. s->coded_block[(x) + (y + 1) * wrap] = 0;
  610. s->coded_block[(x + 1) + (y + 1) * wrap] = 0;
  611. }
  612. /* chroma */
  613. wrap = s->mb_width + 2;
  614. x = mb_x + 1;
  615. y = mb_y + 1;
  616. s->dc_val[1][(x) + (y) * wrap] = v;
  617. s->dc_val[2][(x) + (y) * wrap] = v;
  618. /* ac pred */
  619. memset(s->ac_val[1][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
  620. memset(s->ac_val[2][(x) + (y) * wrap], 0, 16 * sizeof(INT16));
  621. } else {
  622. s->last_dc[0] = 128 << s->intra_dc_precision;
  623. s->last_dc[1] = 128 << s->intra_dc_precision;
  624. s->last_dc[2] = 128 << s->intra_dc_precision;
  625. }
  626. }
  627. /* update motion predictor */
  628. if (s->out_format == FMT_H263) {
  629. int x, y, wrap;
  630. x = 2 * mb_x + 1;
  631. y = 2 * mb_y + 1;
  632. wrap = 2 * s->mb_width + 2;
  633. if (s->mb_intra) {
  634. motion_x = 0;
  635. motion_y = 0;
  636. goto motion_init;
  637. } else if (s->mv_type == MV_TYPE_16X16) {
  638. motion_x = s->mv[0][0][0];
  639. motion_y = s->mv[0][0][1];
  640. motion_init:
  641. /* no update if 8X8 because it has been done during parsing */
  642. s->motion_val[(x) + (y) * wrap][0] = motion_x;
  643. s->motion_val[(x) + (y) * wrap][1] = motion_y;
  644. s->motion_val[(x + 1) + (y) * wrap][0] = motion_x;
  645. s->motion_val[(x + 1) + (y) * wrap][1] = motion_y;
  646. s->motion_val[(x) + (y + 1) * wrap][0] = motion_x;
  647. s->motion_val[(x) + (y + 1) * wrap][1] = motion_y;
  648. s->motion_val[(x + 1) + (y + 1) * wrap][0] = motion_x;
  649. s->motion_val[(x + 1) + (y + 1) * wrap][1] = motion_y;
  650. }
  651. }
  652. if (!s->intra_only) {
  653. UINT8 *dest_y, *dest_cb, *dest_cr;
  654. UINT8 *mbskip_ptr;
  655. /* avoid copy if macroblock skipped in last frame too */
  656. if (!s->encoding && s->pict_type != B_TYPE) {
  657. mbskip_ptr = &s->mbskip_table[s->mb_y * s->mb_width + s->mb_x];
  658. if (s->mb_skiped) {
  659. s->mb_skiped = 0;
  660. /* if previous was skipped too, then nothing to do ! */
  661. if (*mbskip_ptr != 0)
  662. goto the_end;
  663. *mbskip_ptr = 1; /* indicate that this time we skiped it */
  664. } else {
  665. *mbskip_ptr = 0; /* not skipped */
  666. }
  667. }
  668. dest_y = s->current_picture[0] + (mb_y * 16 * s->linesize) + mb_x * 16;
  669. dest_cb = s->current_picture[1] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  670. dest_cr = s->current_picture[2] + (mb_y * 8 * (s->linesize >> 1)) + mb_x * 8;
  671. if (s->interlaced_dct) {
  672. dct_linesize = s->linesize * 2;
  673. dct_offset = s->linesize;
  674. } else {
  675. dct_linesize = s->linesize;
  676. dct_offset = s->linesize * 8;
  677. }
  678. if (!s->mb_intra) {
  679. /* motion handling */
  680. if (!s->no_rounding)
  681. op_pix = put_pixels_tab;
  682. else
  683. op_pix = put_no_rnd_pixels_tab;
  684. if (s->mv_dir & MV_DIR_FORWARD) {
  685. MPV_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture, op_pix);
  686. if (!s->no_rounding)
  687. op_pix = avg_pixels_tab;
  688. else
  689. op_pix = avg_no_rnd_pixels_tab;
  690. }
  691. if (s->mv_dir & MV_DIR_BACKWARD) {
  692. MPV_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture, op_pix);
  693. }
  694. /* add dct residue */
  695. add_dct(s, block[0], 0, dest_y, dct_linesize);
  696. add_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  697. add_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  698. add_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  699. add_dct(s, block[4], 4, dest_cb, dct_linesize >> 1);
  700. add_dct(s, block[5], 5, dest_cr, dct_linesize >> 1);
  701. } else {
  702. /* dct only in intra block */
  703. put_dct(s, block[0], 0, dest_y, dct_linesize);
  704. put_dct(s, block[1], 1, dest_y + 8, dct_linesize);
  705. put_dct(s, block[2], 2, dest_y + dct_offset, dct_linesize);
  706. put_dct(s, block[3], 3, dest_y + dct_offset + 8, dct_linesize);
  707. put_dct(s, block[4], 4, dest_cb, dct_linesize >> 1);
  708. put_dct(s, block[5], 5, dest_cr, dct_linesize >> 1);
  709. }
  710. }
  711. the_end:
  712. emms_c();
  713. }
  714. static void encode_picture(MpegEncContext *s, int picture_number)
  715. {
  716. int mb_x, mb_y, wrap;
  717. UINT8 *ptr;
  718. int i, motion_x, motion_y;
  719. s->picture_number = picture_number;
  720. if (!s->fixed_qscale)
  721. s->qscale = rate_estimate_qscale(s);
  722. /* precompute matrix */
  723. if (s->out_format == FMT_MJPEG) {
  724. /* for mjpeg, we do include qscale in the matrix */
  725. s->intra_matrix[0] = default_intra_matrix[0];
  726. for(i=1;i<64;i++)
  727. s->intra_matrix[i] = (default_intra_matrix[i] * s->qscale) >> 3;
  728. convert_matrix(s->q_intra_matrix, s->intra_matrix, 8);
  729. } else {
  730. convert_matrix(s->q_intra_matrix, s->intra_matrix, s->qscale);
  731. convert_matrix(s->q_non_intra_matrix, s->non_intra_matrix, s->qscale);
  732. }
  733. switch(s->out_format) {
  734. case FMT_MJPEG:
  735. mjpeg_picture_header(s);
  736. break;
  737. case FMT_H263:
  738. if (s->h263_msmpeg4)
  739. msmpeg4_encode_picture_header(s, picture_number);
  740. else if (s->h263_pred)
  741. mpeg4_encode_picture_header(s, picture_number);
  742. else if (s->h263_rv10)
  743. rv10_encode_picture_header(s, picture_number);
  744. else
  745. h263_encode_picture_header(s, picture_number);
  746. break;
  747. case FMT_MPEG1:
  748. mpeg1_encode_picture_header(s, picture_number);
  749. break;
  750. }
  751. /* init last dc values */
  752. /* note: quant matrix value (8) is implied here */
  753. s->last_dc[0] = 128;
  754. s->last_dc[1] = 128;
  755. s->last_dc[2] = 128;
  756. s->mb_incr = 1;
  757. s->last_mv[0][0][0] = 0;
  758. s->last_mv[0][0][1] = 0;
  759. s->mv_type = MV_TYPE_16X16;
  760. s->mv_dir = MV_DIR_FORWARD;
  761. for(mb_y=0; mb_y < s->mb_height; mb_y++) {
  762. for(mb_x=0; mb_x < s->mb_width; mb_x++) {
  763. s->mb_x = mb_x;
  764. s->mb_y = mb_y;
  765. /* compute motion vector and macro block type (intra or non intra) */
  766. motion_x = 0;
  767. motion_y = 0;
  768. if (s->pict_type == P_TYPE) {
  769. s->mb_intra = estimate_motion(s, mb_x, mb_y,
  770. &motion_x,
  771. &motion_y);
  772. } else {
  773. s->mb_intra = 1;
  774. }
  775. /* get the pixels */
  776. wrap = s->linesize;
  777. ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
  778. get_pixels(s->block[0], ptr, wrap);
  779. get_pixels(s->block[1], ptr + 8, wrap);
  780. get_pixels(s->block[2], ptr + 8 * wrap, wrap);
  781. get_pixels(s->block[3], ptr + 8 * wrap + 8, wrap);
  782. wrap = s->linesize >> 1;
  783. ptr = s->new_picture[1] + (mb_y * 8 * wrap) + mb_x * 8;
  784. get_pixels(s->block[4], ptr, wrap);
  785. wrap = s->linesize >> 1;
  786. ptr = s->new_picture[2] + (mb_y * 8 * wrap) + mb_x * 8;
  787. get_pixels(s->block[5], ptr, wrap);
  788. /* subtract previous frame if non intra */
  789. if (!s->mb_intra) {
  790. int dxy, offset, mx, my;
  791. dxy = ((motion_y & 1) << 1) | (motion_x & 1);
  792. ptr = s->last_picture[0] +
  793. ((mb_y * 16 + (motion_y >> 1)) * s->linesize) +
  794. (mb_x * 16 + (motion_x >> 1));
  795. sub_pixels_2(s->block[0], ptr, s->linesize, dxy);
  796. sub_pixels_2(s->block[1], ptr + 8, s->linesize, dxy);
  797. sub_pixels_2(s->block[2], ptr + s->linesize * 8, s->linesize, dxy);
  798. sub_pixels_2(s->block[3], ptr + 8 + s->linesize * 8, s->linesize ,dxy);
  799. if (s->out_format == FMT_H263) {
  800. /* special rounding for h263 */
  801. dxy = 0;
  802. if ((motion_x & 3) != 0)
  803. dxy |= 1;
  804. if ((motion_y & 3) != 0)
  805. dxy |= 2;
  806. mx = motion_x >> 2;
  807. my = motion_y >> 2;
  808. } else {
  809. mx = motion_x / 2;
  810. my = motion_y / 2;
  811. dxy = ((my & 1) << 1) | (mx & 1);
  812. mx >>= 1;
  813. my >>= 1;
  814. }
  815. offset = ((mb_y * 8 + my) * (s->linesize >> 1)) + (mb_x * 8 + mx);
  816. ptr = s->last_picture[1] + offset;
  817. sub_pixels_2(s->block[4], ptr, s->linesize >> 1, dxy);
  818. ptr = s->last_picture[2] + offset;
  819. sub_pixels_2(s->block[5], ptr, s->linesize >> 1, dxy);
  820. }
  821. emms_c();
  822. /* DCT & quantize */
  823. if (s->h263_msmpeg4) {
  824. msmpeg4_dc_scale(s);
  825. } else if (s->h263_pred) {
  826. h263_dc_scale(s);
  827. } else {
  828. /* default quantization values */
  829. s->y_dc_scale = 8;
  830. s->c_dc_scale = 8;
  831. }
  832. for(i=0;i<6;i++) {
  833. int last_index;
  834. if (av_fdct == jpeg_fdct_ifast)
  835. last_index = dct_quantize(s, s->block[i], i, s->qscale);
  836. else
  837. last_index = dct_quantize_mmx(s, s->block[i], i, s->qscale);
  838. s->block_last_index[i] = last_index;
  839. }
  840. /* huffman encode */
  841. switch(s->out_format) {
  842. case FMT_MPEG1:
  843. mpeg1_encode_mb(s, s->block, motion_x, motion_y);
  844. break;
  845. case FMT_H263:
  846. if (s->h263_msmpeg4)
  847. msmpeg4_encode_mb(s, s->block, motion_x, motion_y);
  848. else
  849. h263_encode_mb(s, s->block, motion_x, motion_y);
  850. break;
  851. case FMT_MJPEG:
  852. mjpeg_encode_mb(s, s->block);
  853. break;
  854. }
  855. /* decompress blocks so that we keep the state of the decoder */
  856. s->mv[0][0][0] = motion_x;
  857. s->mv[0][0][1] = motion_y;
  858. MPV_decode_mb(s, s->block);
  859. }
  860. }
  861. }
  862. static int dct_quantize(MpegEncContext *s,
  863. DCTELEM *block, int n,
  864. int qscale)
  865. {
  866. int i, j, level, last_non_zero, q;
  867. const int *qmat;
  868. av_fdct (block);
  869. if (s->mb_intra) {
  870. if (n < 4)
  871. q = s->y_dc_scale;
  872. else
  873. q = s->c_dc_scale;
  874. q = q << 3;
  875. /* note: block[0] is assumed to be positive */
  876. block[0] = (block[0] + (q >> 1)) / q;
  877. i = 1;
  878. last_non_zero = 0;
  879. if (s->out_format == FMT_H263) {
  880. qmat = s->q_non_intra_matrix;
  881. } else {
  882. qmat = s->q_intra_matrix;
  883. }
  884. } else {
  885. i = 0;
  886. last_non_zero = -1;
  887. qmat = s->q_non_intra_matrix;
  888. }
  889. for(;i<64;i++) {
  890. j = zigzag_direct[i];
  891. level = block[j];
  892. level = level * qmat[j];
  893. #ifdef PARANOID
  894. {
  895. static int count = 0;
  896. int level1, level2, qmat1;
  897. double val;
  898. if (qmat == s->q_non_intra_matrix) {
  899. qmat1 = default_non_intra_matrix[j] * s->qscale;
  900. } else {
  901. qmat1 = default_intra_matrix[j] * s->qscale;
  902. }
  903. if (av_fdct != jpeg_fdct_ifast)
  904. val = ((double)block[j] * 8.0) / (double)qmat1;
  905. else
  906. val = ((double)block[j] * 8.0 * 2048.0) /
  907. ((double)qmat1 * aanscales[j]);
  908. level1 = (int)val;
  909. level2 = level / (1 << (QMAT_SHIFT - 3));
  910. if (level1 != level2) {
  911. fprintf(stderr, "%d: quant error qlevel=%d wanted=%d level=%d qmat1=%d qmat=%d wantedf=%0.6f\n",
  912. count, level2, level1, block[j], qmat1, qmat[j],
  913. val);
  914. count++;
  915. }
  916. }
  917. #endif
  918. /* XXX: slight error for the low range. Test should be equivalent to
  919. (level <= -(1 << (QMAT_SHIFT - 3)) || level >= (1 <<
  920. (QMAT_SHIFT - 3)))
  921. */
  922. if (((level << (31 - (QMAT_SHIFT - 3))) >> (31 - (QMAT_SHIFT - 3))) !=
  923. level) {
  924. level = level / (1 << (QMAT_SHIFT - 3));
  925. /* XXX: currently, this code is not optimal. the range should be:
  926. mpeg1: -255..255
  927. mpeg2: -2048..2047
  928. h263: -128..127
  929. mpeg4: -2048..2047
  930. */
  931. if (level > 127)
  932. level = 127;
  933. else if (level < -128)
  934. level = -128;
  935. block[j] = level;
  936. last_non_zero = i;
  937. } else {
  938. block[j] = 0;
  939. }
  940. }
  941. return last_non_zero;
  942. }
  943. static int dct_quantize_mmx(MpegEncContext *s,
  944. DCTELEM *block, int n,
  945. int qscale)
  946. {
  947. int i, j, level, last_non_zero, q;
  948. const int *qmat;
  949. av_fdct (block);
  950. if (s->mb_intra) {
  951. if (n < 4)
  952. q = s->y_dc_scale;
  953. else
  954. q = s->c_dc_scale;
  955. /* note: block[0] is assumed to be positive */
  956. block[0] = (block[0] + (q >> 1)) / q;
  957. i = 1;
  958. last_non_zero = 0;
  959. if (s->out_format == FMT_H263) {
  960. qmat = s->q_non_intra_matrix;
  961. } else {
  962. qmat = s->q_intra_matrix;
  963. }
  964. } else {
  965. i = 0;
  966. last_non_zero = -1;
  967. qmat = s->q_non_intra_matrix;
  968. }
  969. for(;i<64;i++) {
  970. j = zigzag_direct[i];
  971. level = block[j];
  972. level = level * qmat[j];
  973. /* XXX: slight error for the low range. Test should be equivalent to
  974. (level <= -(1 << (QMAT_SHIFT_MMX - 3)) || level >= (1 <<
  975. (QMAT_SHIFT_MMX - 3)))
  976. */
  977. if (((level << (31 - (QMAT_SHIFT_MMX - 3))) >> (31 - (QMAT_SHIFT_MMX - 3))) !=
  978. level) {
  979. level = level / (1 << (QMAT_SHIFT_MMX - 3));
  980. /* XXX: currently, this code is not optimal. the range should be:
  981. mpeg1: -255..255
  982. mpeg2: -2048..2047
  983. h263: -128..127
  984. mpeg4: -2048..2047
  985. */
  986. if (level > 127)
  987. level = 127;
  988. else if (level < -128)
  989. level = -128;
  990. block[j] = level;
  991. last_non_zero = i;
  992. } else {
  993. block[j] = 0;
  994. }
  995. }
  996. return last_non_zero;
  997. }
  998. static void dct_unquantize_mpeg1_c(MpegEncContext *s,
  999. DCTELEM *block, int n, int qscale)
  1000. {
  1001. int i, level;
  1002. const UINT16 *quant_matrix;
  1003. if (s->mb_intra) {
  1004. if (n < 4)
  1005. block[0] = block[0] * s->y_dc_scale;
  1006. else
  1007. block[0] = block[0] * s->c_dc_scale;
  1008. /* XXX: only mpeg1 */
  1009. quant_matrix = s->intra_matrix;
  1010. for(i=1;i<64;i++) {
  1011. level = block[i];
  1012. if (level) {
  1013. if (level < 0) {
  1014. level = -level;
  1015. level = (int)(level * qscale * quant_matrix[i]) >> 3;
  1016. level = (level - 1) | 1;
  1017. level = -level;
  1018. } else {
  1019. level = (int)(level * qscale * quant_matrix[i]) >> 3;
  1020. level = (level - 1) | 1;
  1021. }
  1022. #ifdef PARANOID
  1023. if (level < -2048 || level > 2047)
  1024. fprintf(stderr, "unquant error %d %d\n", i, level);
  1025. #endif
  1026. block[i] = level;
  1027. }
  1028. }
  1029. } else {
  1030. i = 0;
  1031. quant_matrix = s->non_intra_matrix;
  1032. for(;i<64;i++) {
  1033. level = block[i];
  1034. if (level) {
  1035. if (level < 0) {
  1036. level = -level;
  1037. level = (((level << 1) + 1) * qscale *
  1038. ((int) (quant_matrix[i]))) >> 4;
  1039. level = (level - 1) | 1;
  1040. level = -level;
  1041. } else {
  1042. level = (((level << 1) + 1) * qscale *
  1043. ((int) (quant_matrix[i]))) >> 4;
  1044. level = (level - 1) | 1;
  1045. }
  1046. #ifdef PARANOID
  1047. if (level < -2048 || level > 2047)
  1048. fprintf(stderr, "unquant error %d %d\n", i, level);
  1049. #endif
  1050. block[i] = level;
  1051. }
  1052. }
  1053. }
  1054. }
  1055. static void dct_unquantize_h263_c(MpegEncContext *s,
  1056. DCTELEM *block, int n, int qscale)
  1057. {
  1058. int i, level, qmul, qadd;
  1059. if (s->mb_intra) {
  1060. if (n < 4)
  1061. block[0] = block[0] * s->y_dc_scale;
  1062. else
  1063. block[0] = block[0] * s->c_dc_scale;
  1064. i = 1;
  1065. } else {
  1066. i = 0;
  1067. }
  1068. qmul = s->qscale << 1;
  1069. qadd = (s->qscale - 1) | 1;
  1070. for(;i<64;i++) {
  1071. level = block[i];
  1072. if (level) {
  1073. if (level < 0) {
  1074. level = level * qmul - qadd;
  1075. } else {
  1076. level = level * qmul + qadd;
  1077. }
  1078. #ifdef PARANOID
  1079. if (level < -2048 || level > 2047)
  1080. fprintf(stderr, "unquant error %d %d\n", i, level);
  1081. #endif
  1082. block[i] = level;
  1083. }
  1084. }
  1085. }
  1086. /* rate control */
  1087. /* an I frame is I_FRAME_SIZE_RATIO bigger than a P frame */
  1088. #define I_FRAME_SIZE_RATIO 3.0
  1089. #define QSCALE_K 20
  1090. static void rate_control_init(MpegEncContext *s)
  1091. {
  1092. s->wanted_bits = 0;
  1093. if (s->intra_only) {
  1094. s->I_frame_bits = ((INT64)s->bit_rate * FRAME_RATE_BASE) / s->frame_rate;
  1095. s->P_frame_bits = s->I_frame_bits;
  1096. } else {
  1097. s->P_frame_bits = (int) ((float)(s->gop_size * s->bit_rate) /
  1098. (float)((float)s->frame_rate / FRAME_RATE_BASE * (I_FRAME_SIZE_RATIO + s->gop_size - 1)));
  1099. s->I_frame_bits = (int)(s->P_frame_bits * I_FRAME_SIZE_RATIO);
  1100. }
  1101. #if defined(DEBUG)
  1102. printf("I_frame_size=%d P_frame_size=%d\n",
  1103. s->I_frame_bits, s->P_frame_bits);
  1104. #endif
  1105. }
  1106. /*
  1107. * This heuristic is rather poor, but at least we do not have to
  1108. * change the qscale at every macroblock.
  1109. */
  1110. static int rate_estimate_qscale(MpegEncContext *s)
  1111. {
  1112. long long total_bits = s->total_bits;
  1113. float q;
  1114. int qscale, diff, qmin;
  1115. if (s->pict_type == I_TYPE) {
  1116. s->wanted_bits += s->I_frame_bits;
  1117. } else {
  1118. s->wanted_bits += s->P_frame_bits;
  1119. }
  1120. diff = s->wanted_bits - total_bits;
  1121. q = 31.0 - (float)diff / (QSCALE_K * s->mb_height * s->mb_width);
  1122. /* adjust for I frame */
  1123. if (s->pict_type == I_TYPE && !s->intra_only) {
  1124. q /= I_FRAME_SIZE_RATIO;
  1125. }
  1126. /* using a too small Q scale leeds to problems in mpeg1 and h263
  1127. because AC coefficients are clamped to 255 or 127 */
  1128. qmin = 3;
  1129. if (q < qmin)
  1130. q = qmin;
  1131. else if (q > 31)
  1132. q = 31;
  1133. qscale = (int)(q + 0.5);
  1134. #if defined(DEBUG)
  1135. printf("%d: total=%Ld br=%0.1f diff=%d qest=%0.1f\n",
  1136. s->picture_number,
  1137. total_bits,
  1138. (float)s->frame_rate / FRAME_RATE_BASE *
  1139. total_bits / s->picture_number,
  1140. diff, q);
  1141. #endif
  1142. return qscale;
  1143. }
  1144. AVCodec mpeg1video_encoder = {
  1145. "mpeg1video",
  1146. CODEC_TYPE_VIDEO,
  1147. CODEC_ID_MPEG1VIDEO,
  1148. sizeof(MpegEncContext),
  1149. MPV_encode_init,
  1150. MPV_encode_picture,
  1151. MPV_encode_end,
  1152. };
  1153. AVCodec h263_encoder = {
  1154. "h263",
  1155. CODEC_TYPE_VIDEO,
  1156. CODEC_ID_H263,
  1157. sizeof(MpegEncContext),
  1158. MPV_encode_init,
  1159. MPV_encode_picture,
  1160. MPV_encode_end,
  1161. };
  1162. AVCodec h263p_encoder = {
  1163. "h263p",
  1164. CODEC_TYPE_VIDEO,
  1165. CODEC_ID_H263P,
  1166. sizeof(MpegEncContext),
  1167. MPV_encode_init,
  1168. MPV_encode_picture,
  1169. MPV_encode_end,
  1170. };
  1171. AVCodec rv10_encoder = {
  1172. "rv10",
  1173. CODEC_TYPE_VIDEO,
  1174. CODEC_ID_RV10,
  1175. sizeof(MpegEncContext),
  1176. MPV_encode_init,
  1177. MPV_encode_picture,
  1178. MPV_encode_end,
  1179. };
  1180. AVCodec mjpeg_encoder = {
  1181. "mjpeg",
  1182. CODEC_TYPE_VIDEO,
  1183. CODEC_ID_MJPEG,
  1184. sizeof(MpegEncContext),
  1185. MPV_encode_init,
  1186. MPV_encode_picture,
  1187. MPV_encode_end,
  1188. };
  1189. AVCodec opendivx_encoder = {
  1190. "opendivx",
  1191. CODEC_TYPE_VIDEO,
  1192. CODEC_ID_OPENDIVX,
  1193. sizeof(MpegEncContext),
  1194. MPV_encode_init,
  1195. MPV_encode_picture,
  1196. MPV_encode_end,
  1197. };
  1198. AVCodec msmpeg4_encoder = {
  1199. "msmpeg4",
  1200. CODEC_TYPE_VIDEO,
  1201. CODEC_ID_MSMPEG4,
  1202. sizeof(MpegEncContext),
  1203. MPV_encode_init,
  1204. MPV_encode_picture,
  1205. MPV_encode_end,
  1206. };