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