comparison mpegvideo.c @ 0:986e461dc072 libavcodec

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