comparison mpegvideo.c @ 232:b640ec5948b0 libavcodec

- Now the ME is done for the entire picture when enconding, the DCT/Quantization is done after we have calculated all the MV of the picture. - This is the preamble for a better bit rate control.
author pulento
date Sun, 10 Feb 2002 01:56:50 +0000
parents 840cd25bf259
children 3f5b72726118
comparison
equal deleted inserted replaced
231:840cd25bf259 232:b640ec5948b0
144 goto fail; 144 goto fail;
145 s->aux_picture_base[i] = pict; 145 s->aux_picture_base[i] = pict;
146 s->aux_picture[i] = pict + pict_start; 146 s->aux_picture[i] = pict + pict_start;
147 } 147 }
148 } 148 }
149 149
150 if (s->encoding) {
151 /* Allocate MB type table */
152 s->mb_type = malloc(s->mb_width * s->mb_height * sizeof(char));
153 if (s->mb_type == NULL) {
154 perror("malloc");
155 goto fail;
156 }
157
158 /* Allocate MV table */
159 /* By now we just have one MV per MB */
160 s->mv_table[0] = malloc(s->mb_width * s->mb_height * sizeof(INT16));
161 s->mv_table[1] = malloc(s->mb_width * s->mb_height * sizeof(INT16));
162 if (s->mv_table[1] == NULL || s->mv_table[0] == NULL) {
163 perror("malloc");
164 goto fail;
165 }
166 }
167
150 if (s->out_format == FMT_H263) { 168 if (s->out_format == FMT_H263) {
151 int size; 169 int size;
152 /* MV prediction */ 170 /* MV prediction */
153 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2); 171 size = (2 * s->mb_width + 2) * (2 * s->mb_height + 2);
154 s->motion_val = malloc(size * 2 * sizeof(INT16)); 172 s->motion_val = malloc(size * 2 * sizeof(INT16));
202 } 220 }
203 221
204 s->context_initialized = 1; 222 s->context_initialized = 1;
205 return 0; 223 return 0;
206 fail: 224 fail:
225 if (s->mb_type)
226 free(s->mb_type);
227 if (s->mv_table[0])
228 free(s->mv_table[0]);
229 if (s->mv_table[1])
230 free(s->mv_table[1]);
207 if (s->motion_val) 231 if (s->motion_val)
208 free(s->motion_val); 232 free(s->motion_val);
209 if (s->dc_val[0]) 233 if (s->dc_val[0])
210 free(s->dc_val[0]); 234 free(s->dc_val[0]);
211 if (s->ac_val[0]) 235 if (s->ac_val[0])
230 /* init common structure for both encoder and decoder */ 254 /* init common structure for both encoder and decoder */
231 void MPV_common_end(MpegEncContext *s) 255 void MPV_common_end(MpegEncContext *s)
232 { 256 {
233 int i; 257 int i;
234 258
259 if (s->mb_type)
260 free(s->mb_type);
261 if (s->mv_table[0])
262 free(s->mv_table[0]);
263 if (s->mv_table[1])
264 free(s->mv_table[1]);
235 if (s->motion_val) 265 if (s->motion_val)
236 free(s->motion_val); 266 free(s->motion_val);
237 if (s->h263_pred) { 267 if (s->h263_pred) {
238 free(s->dc_val[0]); 268 free(s->dc_val[0]);
239 free(s->ac_val[0]); 269 free(s->ac_val[0]);
275 s->intra_only = 0; 305 s->intra_only = 0;
276 } 306 }
277 s->full_search = motion_estimation_method; 307 s->full_search = motion_estimation_method;
278 308
279 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE); 309 s->fixed_qscale = (avctx->flags & CODEC_FLAG_QSCALE);
280 310
281 switch(avctx->codec->id) { 311 switch(avctx->codec->id) {
282 case CODEC_ID_MPEG1VIDEO: 312 case CODEC_ID_MPEG1VIDEO:
283 s->out_format = FMT_MPEG1; 313 s->out_format = FMT_MPEG1;
284 break; 314 break;
285 case CODEC_ID_MJPEG: 315 case CODEC_ID_MJPEG:
286 s->out_format = FMT_MJPEG; 316 s->out_format = FMT_MJPEG;
287 s->intra_only = 1; /* force intra only for jpeg */ 317 s->intra_only = 1; /* force intra only for jpeg */
288 s->mjpeg_write_tables = 1; /* write all tables */ 318 s->mjpeg_write_tables = 1; /* write all tables */
289 s->mjpeg_vsample[0] = 2; /* set up default sampling factors */ 319 s->mjpeg_vsample[0] = 2; /* set up default sampling factors */
290 s->mjpeg_vsample[1] = 1; /* the only currently supported values */ 320 s->mjpeg_vsample[1] = 1; /* the only currently supported values */
291 s->mjpeg_vsample[2] = 1; 321 s->mjpeg_vsample[2] = 1;
292 s->mjpeg_hsample[0] = 2; 322 s->mjpeg_hsample[0] = 2;
293 s->mjpeg_hsample[1] = 1; 323 s->mjpeg_hsample[1] = 1;
294 s->mjpeg_hsample[2] = 1; 324 s->mjpeg_hsample[2] = 1;
295 if (mjpeg_init(s) < 0) 325 if (mjpeg_init(s) < 0)
296 return -1; 326 return -1;
297 break; 327 break;
298 case CODEC_ID_H263: 328 case CODEC_ID_H263:
299 if (h263_get_picture_format(s->width, s->height) == 7){ 329 if (h263_get_picture_format(s->width, s->height) == 7) {
300 printf("Input picture size isn't suitable for h263 codec! try h263+\n"); 330 printf("Input picture size isn't suitable for h263 codec! try h263+\n");
301 return -1; 331 return -1;
302 } 332 }
303 s->out_format = FMT_H263; 333 s->out_format = FMT_H263;
304 break; 334 break;
305 case CODEC_ID_H263P: 335 case CODEC_ID_H263P:
306 s->out_format = FMT_H263; 336 s->out_format = FMT_H263;
307 s->rtp_mode = 1; 337 s->rtp_mode = 1;
366 print_stats(); 396 print_stats();
367 #endif 397 #endif
368 MPV_common_end(s); 398 MPV_common_end(s);
369 if (s->out_format == FMT_MJPEG) 399 if (s->out_format == FMT_MJPEG)
370 mjpeg_close(s); 400 mjpeg_close(s);
401
371 return 0; 402 return 0;
372 } 403 }
373 404
374 /* draw the edges of width 'w' of an image of size width, height */ 405 /* draw the edges of width 'w' of an image of size width, height */
375 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w) 406 static void draw_edges_c(UINT8 *buf, int wrap, int width, int height, int w)
477 dest_wrap >>= 1; 508 dest_wrap >>= 1;
478 w >>= 1; 509 w >>= 1;
479 h >>= 1; 510 h >>= 1;
480 } 511 }
481 512
482 if(dest_wrap==src_wrap){ 513 if(dest_wrap==src_wrap){
483 s->new_picture[i] = pict->data[i]; 514 s->new_picture[i] = pict->data[i];
484 }else { 515 } else {
485 for(j=0;j<h;j++) { 516 for(j=0;j<h;j++) {
486 memcpy(dest, src, w); 517 memcpy(dest, src, w);
487 dest += dest_wrap; 518 dest += dest_wrap;
488 src += src_wrap; 519 src += src_wrap;
489 } 520 }
490 s->new_picture[i] = s->current_picture[i]; 521 s->new_picture[i] = s->current_picture[i];
491 } 522 }
492 } 523 }
493 524
494 encode_picture(s, s->picture_number); 525 encode_picture(s, s->picture_number);
495 526
496 MPV_frame_end(s); 527 MPV_frame_end(s);
942 if (last_gob) { 973 if (last_gob) {
943 s->first_gob_line = 1; 974 s->first_gob_line = 1;
944 } 975 }
945 } 976 }
946 } 977 }
978
947 for(mb_x=0; mb_x < s->mb_width; mb_x++) { 979 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
948
949 s->mb_x = mb_x; 980 s->mb_x = mb_x;
950 s->mb_y = mb_y; 981 s->mb_y = mb_y;
951 982
952 /* compute motion vector and macro block type (intra or non intra) */ 983 /* compute motion vector and macro block type (intra or non intra) */
953 motion_x = 0; 984 motion_x = 0;
957 &motion_x, 988 &motion_x,
958 &motion_y); 989 &motion_y);
959 } else { 990 } else {
960 s->mb_intra = 1; 991 s->mb_intra = 1;
961 } 992 }
962 993 /* Store MB type and MV */
994 s->mb_type[mb_y * s->mb_width + mb_x] = s->mb_intra;
995 s->mv_table[0][mb_y * s->mb_width + mb_x] = motion_x;
996 s->mv_table[1][mb_y * s->mb_width + mb_x] = motion_y;
997 }
998
999 for(mb_x=0; mb_x < s->mb_width; mb_x++) {
1000
1001 s->mb_x = mb_x;
1002 s->mb_y = mb_y;
1003 #if 0
1004 /* compute motion vector and macro block type (intra or non intra) */
1005 motion_x = 0;
1006 motion_y = 0;
1007 if (s->pict_type == P_TYPE) {
1008 s->mb_intra = estimate_motion(s, mb_x, mb_y,
1009 &motion_x,
1010 &motion_y);
1011 } else {
1012 s->mb_intra = 1;
1013 }
1014 #endif
1015
1016 s->mb_intra = s->mb_type[mb_y * s->mb_width + mb_x];
1017 motion_x = s->mv_table[0][mb_y * s->mb_width + mb_x];
1018 motion_y = s->mv_table[1][mb_y * s->mb_width + mb_x];
1019
963 /* get the pixels */ 1020 /* get the pixels */
964 wrap = s->linesize; 1021 wrap = s->linesize;
965 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16; 1022 ptr = s->new_picture[0] + (mb_y * 16 * wrap) + mb_x * 16;
966 get_pixels(s->block[0], ptr, wrap); 1023 get_pixels(s->block[0], ptr, wrap);
967 get_pixels(s->block[1], ptr + 8, wrap); 1024 get_pixels(s->block[1], ptr + 8, wrap);