Mercurial > libavcodec.hg
comparison mpeg12.c @ 1421:340c90faa1dc libavcodec
mpeg2 encoding
author | michaelni |
---|---|
date | Mon, 25 Aug 2003 22:47:32 +0000 |
parents | c2e63cb94d06 |
children | 2e590fee59bd |
comparison
equal
deleted
inserted
replaced
1420:ff0220970711 | 1421:340c90faa1dc |
---|---|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 */ | 18 */ |
19 | 19 |
20 /** | 20 /** |
21 * @file mpeg12.c | 21 * @file mpeg12.c |
22 * MPEG1 codec / MPEG2 decoder. | 22 * MPEG1/2 codec |
23 */ | 23 */ |
24 | 24 |
25 //#define DEBUG | 25 //#define DEBUG |
26 #include "avcodec.h" | 26 #include "avcodec.h" |
27 #include "dsputil.h" | 27 #include "dsputil.h" |
226 } | 226 } |
227 | 227 |
228 put_bits(&s->pb, 4, s->aspect_ratio_info); | 228 put_bits(&s->pb, 4, s->aspect_ratio_info); |
229 put_bits(&s->pb, 4, s->frame_rate_index); | 229 put_bits(&s->pb, 4, s->frame_rate_index); |
230 v = (s->bit_rate + 399) / 400; | 230 v = (s->bit_rate + 399) / 400; |
231 if (v > 0x3ffff) | 231 if (v > 0x3ffff && s->codec_id == CODEC_ID_MPEG1VIDEO) |
232 v = 0x3ffff; | 232 v = 0x3ffff; |
233 put_bits(&s->pb, 18, v); | 233 put_bits(&s->pb, 18, v & 0x3FFFF); |
234 put_bits(&s->pb, 1, 1); /* marker */ | 234 put_bits(&s->pb, 1, 1); /* marker */ |
235 | 235 |
236 if(s->avctx->rc_buffer_size) | 236 if(s->avctx->rc_buffer_size) |
237 vbv_buffer_size = s->avctx->rc_buffer_size; | 237 vbv_buffer_size = s->avctx->rc_buffer_size; |
238 else | 238 else |
239 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */ | 239 /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */ |
240 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024; | 240 vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024; |
241 put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384); | 241 put_bits(&s->pb, 10, ((vbv_buffer_size + 16383) / 16384) & 0x3FF); |
242 put_bits(&s->pb, 1, 1); /* constrained parameter flag */ | 242 put_bits(&s->pb, 1, 1); /* constrained parameter flag */ |
243 | 243 |
244 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix); | 244 ff_write_quant_matrix(&s->pb, s->avctx->intra_matrix); |
245 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix); | 245 ff_write_quant_matrix(&s->pb, s->avctx->inter_matrix); |
246 | 246 |
247 if(s->codec_id == CODEC_ID_MPEG2VIDEO){ | |
248 put_header(s, EXT_START_CODE); | |
249 put_bits(&s->pb, 4, 1); //seq ext | |
250 put_bits(&s->pb, 1, 0); //esc | |
251 put_bits(&s->pb, 3, 4); //profile | |
252 put_bits(&s->pb, 4, 8); //level | |
253 put_bits(&s->pb, 1, s->progressive_sequence=1); | |
254 put_bits(&s->pb, 2, 1); //chroma format 4:2:0 | |
255 put_bits(&s->pb, 2, 0); //horizontal size ext | |
256 put_bits(&s->pb, 2, 0); //vertical size ext | |
257 put_bits(&s->pb, 12, v>>18); //bitrate ext | |
258 put_bits(&s->pb, 1, 1); //marker | |
259 put_bits(&s->pb, 8, vbv_buffer_size >>10); //vbv buffer ext | |
260 put_bits(&s->pb, 1, s->low_delay); | |
261 put_bits(&s->pb, 2, 0); // frame_rate_ext_n | |
262 put_bits(&s->pb, 5, 0); // frame_rate_ext_d | |
263 } | |
264 | |
247 put_header(s, GOP_START_CODE); | 265 put_header(s, GOP_START_CODE); |
248 put_bits(&s->pb, 1, 0); /* do drop frame */ | 266 put_bits(&s->pb, 1, 0); /* do drop frame */ |
249 /* time code : we must convert from the real frame rate to a | 267 /* time code : we must convert from the real frame rate to a |
250 fake mpeg frame rate in case of low frame rate */ | 268 fake mpeg frame rate in case of low frame rate */ |
251 fps = frame_rate_tab[s->frame_rate_index]; | 269 fps = frame_rate_tab[s->frame_rate_index]; |
284 } | 302 } |
285 | 303 |
286 /* insert a fake P picture */ | 304 /* insert a fake P picture */ |
287 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num) | 305 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num) |
288 { | 306 { |
307 assert(s->codec_id == CODEC_ID_MPEG1VIDEO); // mpeg2 can do these repeat things | |
308 | |
289 /* mpeg1 picture header */ | 309 /* mpeg1 picture header */ |
290 put_header(s, PICTURE_START_CODE); | 310 put_header(s, PICTURE_START_CODE); |
291 /* temporal reference */ | 311 /* temporal reference */ |
292 put_bits(&s->pb, 10, pict_num & 0x3ff); | 312 put_bits(&s->pb, 10, pict_num & 0x3ff); |
293 | 313 |
370 | 390 |
371 // RAL: Backward f_code necessary for B frames | 391 // RAL: Backward f_code necessary for B frames |
372 if (s->pict_type == B_TYPE) { | 392 if (s->pict_type == B_TYPE) { |
373 put_bits(&s->pb, 1, 0); /* half pel coordinates */ | 393 put_bits(&s->pb, 1, 0); /* half pel coordinates */ |
374 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */ | 394 put_bits(&s->pb, 3, s->b_code); /* backward_f_code */ |
375 } | 395 } |
376 | 396 |
377 put_bits(&s->pb, 1, 0); /* extra bit picture */ | 397 put_bits(&s->pb, 1, 0); /* extra bit picture */ |
398 | |
399 if(s->codec_id == CODEC_ID_MPEG2VIDEO){ | |
400 put_header(s, EXT_START_CODE); | |
401 put_bits(&s->pb, 4, 8); //pic ext | |
402 put_bits(&s->pb, 4, s->f_code); | |
403 put_bits(&s->pb, 4, s->f_code); | |
404 put_bits(&s->pb, 4, s->b_code); | |
405 put_bits(&s->pb, 4, s->b_code); | |
406 put_bits(&s->pb, 2, s->intra_dc_precision); | |
407 put_bits(&s->pb, 2, s->picture_structure= PICT_FRAME); | |
408 put_bits(&s->pb, 1, s->top_field_first); | |
409 put_bits(&s->pb, 1, s->frame_pred_frame_dct= 1); | |
410 put_bits(&s->pb, 1, s->concealment_motion_vectors); | |
411 put_bits(&s->pb, 1, s->q_scale_type); | |
412 put_bits(&s->pb, 1, s->intra_vlc_format); | |
413 put_bits(&s->pb, 1, s->alternate_scan); | |
414 put_bits(&s->pb, 1, s->repeat_first_field); | |
415 put_bits(&s->pb, 1, s->chroma_420_type=1); | |
416 put_bits(&s->pb, 1, s->progressive_frame=1); | |
417 put_bits(&s->pb, 1, 0); //composite_display_flag | |
418 } | |
378 | 419 |
379 s->mb_y=0; | 420 s->mb_y=0; |
380 ff_mpeg1_encode_slice_header(s); | 421 ff_mpeg1_encode_slice_header(s); |
381 } | 422 } |
382 | 423 |
394 for(i=0;i<6;i++) { | 435 for(i=0;i<6;i++) { |
395 if (s->block_last_index[i] >= 0) | 436 if (s->block_last_index[i] >= 0) |
396 cbp |= 1 << (5 - i); | 437 cbp |= 1 << (5 - i); |
397 } | 438 } |
398 | 439 |
399 if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || mb_y != s->mb_height - 1) && | 440 if (cbp == 0 && !first_mb && (mb_x != s->mb_width - 1 || (mb_y != s->mb_height - 1 && s->codec_id == CODEC_ID_MPEG1VIDEO)) && |
400 ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) || | 441 ((s->pict_type == P_TYPE && (motion_x | motion_y) == 0) || |
401 (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) | | 442 (s->pict_type == B_TYPE && s->mv_dir == s->last_mv_dir && (((s->mv_dir & MV_DIR_FORWARD) ? ((s->mv[0][0][0] - s->last_mv[0][0][0])|(s->mv[0][0][1] - s->last_mv[0][0][1])) : 0) | |
402 ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) { | 443 ((s->mv_dir & MV_DIR_BACKWARD) ? ((s->mv[1][0][0] - s->last_mv[1][0][0])|(s->mv[1][0][1] - s->last_mv[1][0][1])) : 0)) == 0))) { |
403 s->mb_skip_run++; | 444 s->mb_skip_run++; |
404 s->qscale -= s->dquant; | 445 s->qscale -= s->dquant; |
706 } | 747 } |
707 } | 748 } |
708 } | 749 } |
709 s->me.mv_penalty= mv_penalty; | 750 s->me.mv_penalty= mv_penalty; |
710 s->fcode_tab= fcode_tab; | 751 s->fcode_tab= fcode_tab; |
711 s->min_qcoeff=-255; | 752 if(s->codec_id == CODEC_ID_MPEG1VIDEO){ |
712 s->max_qcoeff= 255; | 753 s->min_qcoeff=-255; |
754 s->max_qcoeff= 255; | |
755 }else{ | |
756 s->min_qcoeff=-2047; | |
757 s->max_qcoeff= 2047; | |
758 } | |
713 s->intra_ac_vlc_length= | 759 s->intra_ac_vlc_length= |
714 s->inter_ac_vlc_length= uni_mpeg1_ac_vlc_len; | 760 s->inter_ac_vlc_length= uni_mpeg1_ac_vlc_len; |
715 } | 761 } |
716 | 762 |
717 static inline void encode_dc(MpegEncContext *s, int diff, int component) | 763 static inline void encode_dc(MpegEncContext *s, int diff, int component) |
745 dc = block[0]; /* overflow is impossible */ | 791 dc = block[0]; /* overflow is impossible */ |
746 diff = dc - s->last_dc[component]; | 792 diff = dc - s->last_dc[component]; |
747 encode_dc(s, diff, component); | 793 encode_dc(s, diff, component); |
748 s->last_dc[component] = dc; | 794 s->last_dc[component] = dc; |
749 i = 1; | 795 i = 1; |
796 /* | |
797 if (s->intra_vlc_format) | |
798 rl = &rl_mpeg2; | |
799 else | |
800 rl = &rl_mpeg1; | |
801 */ | |
750 } else { | 802 } else { |
751 /* encode the first coefficient : needs to be done here because | 803 /* encode the first coefficient : needs to be done here because |
752 it is handled slightly differently */ | 804 it is handled slightly differently */ |
753 level = block[0]; | 805 level = block[0]; |
754 if (abs(level) == 1) { | 806 if (abs(level) == 1) { |
789 } else { | 841 } else { |
790 /* escape seems to be pretty rare <5% so i dont optimize it */ | 842 /* escape seems to be pretty rare <5% so i dont optimize it */ |
791 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]); | 843 put_bits(&s->pb, mpeg1_vlc[111/*rl->n*/][1], mpeg1_vlc[111/*rl->n*/][0]); |
792 /* escape: only clip in this case */ | 844 /* escape: only clip in this case */ |
793 put_bits(&s->pb, 6, run); | 845 put_bits(&s->pb, 6, run); |
794 if (alevel < 128) { | 846 if(s->codec_id == CODEC_ID_MPEG1VIDEO){ |
795 put_bits(&s->pb, 8, level & 0xff); | 847 if (alevel < 128) { |
796 } else { | 848 put_bits(&s->pb, 8, level & 0xff); |
797 if (level < 0) { | |
798 put_bits(&s->pb, 16, 0x8001 + level + 255); | |
799 } else { | 849 } else { |
800 put_bits(&s->pb, 16, level & 0xffff); | 850 if (level < 0) { |
851 put_bits(&s->pb, 16, 0x8001 + level + 255); | |
852 } else { | |
853 put_bits(&s->pb, 16, level & 0xffff); | |
854 } | |
801 } | 855 } |
856 }else{ | |
857 put_bits(&s->pb, 12, level & 0xfff); | |
802 } | 858 } |
803 } | 859 } |
804 last_non_zero = i; | 860 last_non_zero = i; |
805 } | 861 } |
806 } | 862 } |
866 } | 922 } |
867 | 923 |
868 static inline int get_qscale(MpegEncContext *s) | 924 static inline int get_qscale(MpegEncContext *s) |
869 { | 925 { |
870 int qscale = get_bits(&s->gb, 5); | 926 int qscale = get_bits(&s->gb, 5); |
871 if (s->mpeg2) { | 927 if (s->codec_id == CODEC_ID_MPEG2VIDEO) { |
872 if (s->q_scale_type) { | 928 if (s->q_scale_type) { |
873 return non_linear_qscale[qscale]; | 929 return non_linear_qscale[qscale]; |
874 } else { | 930 } else { |
875 return qscale << 1; | 931 return qscale << 1; |
876 } | 932 } |
985 skip_bits1(&s->gb); /* marker */ | 1041 skip_bits1(&s->gb); /* marker */ |
986 }else | 1042 }else |
987 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */ | 1043 memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */ |
988 s->mb_intra = 1; | 1044 s->mb_intra = 1; |
989 | 1045 |
990 if (s->mpeg2) { | 1046 if (s->codec_id == CODEC_ID_MPEG2VIDEO) { |
991 for(i=0;i<6;i++) { | 1047 for(i=0;i<6;i++) { |
992 if (mpeg2_decode_block_intra(s, block[i], i) < 0) | 1048 if (mpeg2_decode_block_intra(s, block[i], i) < 0) |
993 return -1; | 1049 return -1; |
994 } | 1050 } |
995 } else { | 1051 } else { |
1163 fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y); | 1219 fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y); |
1164 return -1; | 1220 return -1; |
1165 } | 1221 } |
1166 cbp++; | 1222 cbp++; |
1167 | 1223 |
1168 if (s->mpeg2) { | 1224 if (s->codec_id == CODEC_ID_MPEG2VIDEO) { |
1169 for(i=0;i<6;i++) { | 1225 for(i=0;i<6;i++) { |
1170 if (cbp & 32) { | 1226 if (cbp & 32) { |
1171 if (mpeg2_decode_block_non_intra(s, block[i], i) < 0) | 1227 if (mpeg2_decode_block_non_intra(s, block[i], i) < 0) |
1172 return -1; | 1228 return -1; |
1173 } else { | 1229 } else { |
1646 static void mpeg_decode_sequence_extension(MpegEncContext *s) | 1702 static void mpeg_decode_sequence_extension(MpegEncContext *s) |
1647 { | 1703 { |
1648 int horiz_size_ext, vert_size_ext; | 1704 int horiz_size_ext, vert_size_ext; |
1649 int bit_rate_ext, vbv_buf_ext; | 1705 int bit_rate_ext, vbv_buf_ext; |
1650 int frame_rate_ext_n, frame_rate_ext_d; | 1706 int frame_rate_ext_n, frame_rate_ext_d; |
1707 int level, profile; | |
1651 float aspect; | 1708 float aspect; |
1652 | 1709 |
1653 skip_bits(&s->gb, 8); /* profil and level */ | 1710 skip_bits(&s->gb, 1); /* profil and level esc*/ |
1711 profile= get_bits(&s->gb, 3); | |
1712 level= get_bits(&s->gb, 4); | |
1654 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */ | 1713 s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */ |
1655 skip_bits(&s->gb, 2); /* chroma_format */ | 1714 skip_bits(&s->gb, 2); /* chroma_format */ |
1656 horiz_size_ext = get_bits(&s->gb, 2); | 1715 horiz_size_ext = get_bits(&s->gb, 2); |
1657 vert_size_ext = get_bits(&s->gb, 2); | 1716 vert_size_ext = get_bits(&s->gb, 2); |
1658 s->width |= (horiz_size_ext << 12); | 1717 s->width |= (horiz_size_ext << 12); |
1673 frame_rate_tab[s->frame_rate_index] * (frame_rate_ext_n+1), | 1732 frame_rate_tab[s->frame_rate_index] * (frame_rate_ext_n+1), |
1674 MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d+1), | 1733 MPEG1_FRAME_RATE_BASE * (frame_rate_ext_d+1), |
1675 1<<30); | 1734 1<<30); |
1676 | 1735 |
1677 dprintf("sequence extension\n"); | 1736 dprintf("sequence extension\n"); |
1678 s->mpeg2 = 1; | 1737 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO; |
1679 s->avctx->sub_id = 2; /* indicates mpeg2 found */ | 1738 s->avctx->sub_id = 2; /* indicates mpeg2 found */ |
1680 | 1739 |
1681 aspect= mpeg2_aspect[s->aspect_ratio_info]; | 1740 aspect= mpeg2_aspect[s->aspect_ratio_info]; |
1682 if(aspect>0.0) s->avctx->aspect_ratio= s->width/(aspect*s->height); | 1741 if(aspect>0.0) s->avctx->aspect_ratio= s->width/(aspect*s->height); |
1683 else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect; | 1742 else if(aspect<0.0) s->avctx->aspect_ratio= -1.0/aspect; |
1743 | |
1744 if(s->avctx->debug & FF_DEBUG_PICT_INFO) | |
1745 printf("profile: %d, level: %d \n", profile, level); | |
1684 } | 1746 } |
1685 | 1747 |
1686 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s) | 1748 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s) |
1687 { | 1749 { |
1688 int i, v, j; | 1750 int i, v, j; |
2044 #endif | 2106 #endif |
2045 /* end of slice reached */ | 2107 /* end of slice reached */ |
2046 if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) { | 2108 if (/*s->mb_y<<field_pic == s->mb_height &&*/ !s->first_field) { |
2047 /* end of image */ | 2109 /* end of image */ |
2048 | 2110 |
2049 if(s->mpeg2){ | 2111 if(s->codec_id == CODEC_ID_MPEG2VIDEO){ |
2050 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2; | 2112 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG2; |
2051 }else | 2113 }else |
2052 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG1; | 2114 s->current_picture_ptr->qscale_type= FF_QSCALE_TYPE_MPEG1; |
2053 | 2115 |
2054 ff_er_frame_end(s); | 2116 ff_er_frame_end(s); |
2087 init_get_bits(&s->gb, buf, buf_size*8); | 2149 init_get_bits(&s->gb, buf, buf_size*8); |
2088 | 2150 |
2089 width = get_bits(&s->gb, 12); | 2151 width = get_bits(&s->gb, 12); |
2090 height = get_bits(&s->gb, 12); | 2152 height = get_bits(&s->gb, 12); |
2091 s->aspect_ratio_info= get_bits(&s->gb, 4); | 2153 s->aspect_ratio_info= get_bits(&s->gb, 4); |
2092 if(!s->mpeg2){ | 2154 if(s->codec_id == CODEC_ID_MPEG1VIDEO){ |
2093 aspect= mpeg1_aspect[s->aspect_ratio_info]; | 2155 aspect= mpeg1_aspect[s->aspect_ratio_info]; |
2094 if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height); | 2156 if(aspect!=0.0) avctx->aspect_ratio= width/(aspect*height); |
2095 } | 2157 } |
2096 | 2158 |
2097 s->frame_rate_index = get_bits(&s->gb, 4); | 2159 s->frame_rate_index = get_bits(&s->gb, 4); |
2185 /* we set mpeg2 parameters so that it emulates mpeg1 */ | 2247 /* we set mpeg2 parameters so that it emulates mpeg1 */ |
2186 s->progressive_sequence = 1; | 2248 s->progressive_sequence = 1; |
2187 s->progressive_frame = 1; | 2249 s->progressive_frame = 1; |
2188 s->picture_structure = PICT_FRAME; | 2250 s->picture_structure = PICT_FRAME; |
2189 s->frame_pred_frame_dct = 1; | 2251 s->frame_pred_frame_dct = 1; |
2190 s->mpeg2 = 0; | 2252 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG1VIDEO; |
2191 avctx->sub_id = 1; /* indicates mpeg1 */ | 2253 avctx->sub_id = 1; /* indicates mpeg1 */ |
2192 return 0; | 2254 return 0; |
2193 } | 2255 } |
2194 | 2256 |
2195 static int vcr2_init_sequence(AVCodecContext *avctx) | 2257 static int vcr2_init_sequence(AVCodecContext *avctx) |
2231 | 2293 |
2232 s->progressive_sequence = 1; | 2294 s->progressive_sequence = 1; |
2233 s->progressive_frame = 1; | 2295 s->progressive_frame = 1; |
2234 s->picture_structure = PICT_FRAME; | 2296 s->picture_structure = PICT_FRAME; |
2235 s->frame_pred_frame_dct = 1; | 2297 s->frame_pred_frame_dct = 1; |
2236 s->mpeg2 = 1; | 2298 s->codec_id= s->avctx->codec_id= CODEC_ID_MPEG2VIDEO; |
2237 avctx->sub_id = 2; /* indicates mpeg2 */ | 2299 avctx->sub_id = 2; /* indicates mpeg2 */ |
2238 return 0; | 2300 return 0; |
2239 } | 2301 } |
2240 | 2302 |
2241 | 2303 |