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