comparison mpeg12.c @ 694:20810b0193ef libavcodec

adaptive quantization for mpeg1
author michaelni
date Tue, 24 Sep 2002 12:33:37 +0000
parents 22b22723805e
children e65798d228ea
comparison
equal deleted inserted replaced
693:b6a7ff92df57 694:20810b0193ef
20 #include "avcodec.h" 20 #include "avcodec.h"
21 #include "dsputil.h" 21 #include "dsputil.h"
22 #include "mpegvideo.h" 22 #include "mpegvideo.h"
23 23
24 #include "mpeg12data.h" 24 #include "mpeg12data.h"
25
26 #if 1
27 #define PRINT_QP(a, b) {}
28 #else
29 #define PRINT_QP(a, b) printf(a, b)
30 #endif
25 31
26 /* Start codes. */ 32 /* Start codes. */
27 #define SEQ_END_CODE 0x000001b7 33 #define SEQ_END_CODE 0x000001b7
28 #define SEQ_START_CODE 0x000001b3 34 #define SEQ_START_CODE 0x000001b3
29 #define GOP_START_CODE 0x000001b8 35 #define GOP_START_CODE 0x000001b8
287 /* skip macroblock, except if first or last macroblock of a slice */ 293 /* skip macroblock, except if first or last macroblock of a slice */
288 if ((cbp | motion_x | motion_y) == 0 && 294 if ((cbp | motion_x | motion_y) == 0 &&
289 (!((mb_x | mb_y) == 0 || 295 (!((mb_x | mb_y) == 0 ||
290 (mb_x == s->mb_width - 1 && mb_y == s->mb_height - 1)))) { 296 (mb_x == s->mb_width - 1 && mb_y == s->mb_height - 1)))) {
291 s->mb_incr++; 297 s->mb_incr++;
298 s->qscale -= s->dquant;
292 } else { 299 } else {
293 /* output mb incr */ 300 /* output mb incr */
294 mb_incr = s->mb_incr; 301 mb_incr = s->mb_incr;
295 302
296 while (mb_incr > 33) { 303 while (mb_incr > 33) {
299 } 306 }
300 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1], 307 put_bits(&s->pb, mbAddrIncrTable[mb_incr - 1][1],
301 mbAddrIncrTable[mb_incr - 1][0]); 308 mbAddrIncrTable[mb_incr - 1][0]);
302 309
303 if (s->pict_type == I_TYPE) { 310 if (s->pict_type == I_TYPE) {
304 put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */ 311 if(s->dquant && cbp){
312 put_bits(&s->pb, 2, 1); /* macroblock_type : macroblock_quant = 1 */
313 put_bits(&s->pb, 5, s->qscale);
314 }else{
315 put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */
316 s->qscale -= s->dquant;
317 }
305 } else { 318 } else {
306 if (s->mb_intra) { 319 if (s->mb_intra) {
307 put_bits(&s->pb, 5, 0x03); 320 if(s->dquant && cbp){
321 put_bits(&s->pb, 6, 0x01);
322 put_bits(&s->pb, 5, s->qscale);
323 }else{
324 put_bits(&s->pb, 5, 0x03);
325 s->qscale -= s->dquant;
326 }
308 } else { 327 } else {
309 if (cbp != 0) { 328 if (cbp != 0) {
310 if (motion_x == 0 && motion_y == 0) { 329 if (motion_x == 0 && motion_y == 0) {
311 put_bits(&s->pb, 2, 1); /* macroblock_pattern only */ 330 if(s->dquant){
331 put_bits(&s->pb, 5, 1); /* macroblock_pattern & quant */
332 put_bits(&s->pb, 5, s->qscale);
333 }else{
334 put_bits(&s->pb, 2, 1); /* macroblock_pattern only */
335 }
312 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); 336 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
313 } else { 337 } else {
314 put_bits(&s->pb, 1, 1); /* motion + cbp */ 338 if(s->dquant){
339 put_bits(&s->pb, 5, 2); /* motion + cbp */
340 put_bits(&s->pb, 5, s->qscale);
341 }else{
342 put_bits(&s->pb, 1, 1); /* motion + cbp */
343 }
315 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); 344 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]);
316 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); 345 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]);
317 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]); 346 put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
318 } 347 }
319 } else { 348 } else {
320 put_bits(&s->pb, 3, 1); /* motion only */ 349 put_bits(&s->pb, 3, 1); /* motion only */
321 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); 350 mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]);
322 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); 351 mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]);
352 s->qscale -= s->dquant;
323 } 353 }
324 } 354 }
325 } 355 }
326 for(i=0;i<6;i++) { 356 for(i=0;i<6;i++) {
327 if (cbp & (1 << (5 - i))) { 357 if (cbp & (1 << (5 - i))) {
1500 dprintf("ret=%d\n", ret); 1530 dprintf("ret=%d\n", ret);
1501 if (ret < 0) 1531 if (ret < 0)
1502 return -1; 1532 return -1;
1503 if (ret == 1) 1533 if (ret == 1)
1504 break; 1534 break;
1535
1536 if(s->mb_x==0)
1537 PRINT_QP("%s", "\n");
1538 PRINT_QP("%2d", s->qscale);
1539
1505 MPV_decode_mb(s, s->block); 1540 MPV_decode_mb(s, s->block);
1506 } 1541 }
1507 emms_c(); 1542 emms_c();
1508 1543
1509 /* end of slice reached */ 1544 /* end of slice reached */