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.

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