1 #include "../libzmpeg3.h"
5 zig_zag_scan_nommx[64] = {
6 0 , 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
7 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
8 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
9 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
14 alternate_scan_nommx[64] = {
15 0, 8, 16, 24, 1, 9, 2, 10, 17, 25, 32, 40, 48, 56, 57, 49,
16 41, 33, 26, 18, 3, 11, 4, 12, 19, 27, 34, 42, 50, 58, 35, 43,
17 51, 59, 20, 28, 5, 13, 6, 14, 21, 29, 36, 44, 52, 60, 37, 45,
18 53, 61, 22, 30, 7, 15, 23, 31, 38, 46, 54, 62, 39, 47, 55, 63,
21 /* default intra quantization matrix */
23 default_intra_quantizer_matrix[64] = {
24 8, 16, 19, 22, 26, 27, 29, 34,
25 16, 16, 22, 24, 27, 29, 34, 37,
26 19, 22, 26, 27, 29, 34, 34, 38,
27 22, 22, 26, 27, 29, 34, 37, 40,
28 22, 26, 27, 29, 32, 35, 40, 48,
29 26, 27, 29, 32, 35, 40, 48, 58,
30 26, 27, 29, 34, 38, 46, 56, 69,
31 27, 29, 35, 38, 46, 56, 69, 83,
35 frame_rate_table[16] = {
37 (double)24000.0/1001.0, /* Official frame rates */
40 (double)30000.0/1001.0,
43 (double)60000.0/1001.0,
46 1.0, 5.0, 10.0, 12.0, 15.0, /* Unofficial economy rates */
50 static int blk_cnt_tab[3] = { 6, 8, 12 };
56 long size[4], padding[2]; /* Size of Y, U, and V buffers */
58 if( !mpeg2 ) { /* force MPEG-1 parameters */
61 pict_struct = pics_FRAME_PICTURE;
63 chroma_format = cfmt_420;
64 matrix_coefficients = 5;
67 /* Get dimensions rounded to nearest multiple of coded macroblocks */
68 mb_width = (horizontal_size + 15) / 16;
69 mb_height = (mpeg2 && !prog_seq) ?
70 (2 * ((vertical_size + 31) / 32)) : ((vertical_size + 15) / 16);
71 coded_picture_width = 16 * mb_width;
72 coded_picture_height = 16 * mb_height;
73 chrom_width = (chroma_format == cfmt_444) ?
74 coded_picture_width : (coded_picture_width >> 1);
75 chrom_height = (chroma_format != cfmt_420) ?
76 coded_picture_height : (coded_picture_height >> 1);
77 blk_cnt = blk_cnt_tab[chroma_format - 1];
79 /* Get sizes of YUV buffers */
80 padding[0] = 16*coded_picture_width + 16;
81 size[0] = coded_picture_width*coded_picture_height + padding[0];
83 padding[1] = 16*chrom_width + 16;
84 size[1] = chrom_width*chrom_height + padding[1];
85 size[3] = (size[2] = (llw * llh)) / 4;
87 /* Allocate contiguous fragments for YUV buffers for hardware YUV decoding */
88 sz = size[0] + 2*size[1];
89 yuv_buffer[0] = new uint8_t[sz]; memset(yuv_buffer[0],0,sz);
90 yuv_buffer[1] = new uint8_t[sz]; memset(yuv_buffer[1],0,sz);
91 yuv_buffer[2] = new uint8_t[sz]; memset(yuv_buffer[2],0,sz);
93 if( scalable_mode == slice_decoder_t::sc_SPAT ) {
94 sz = size[2] + 2 * size[3];
95 yuv_buffer[3] = new uint8_t[sz]; memset(yuv_buffer[3],0,sz);
96 yuv_buffer[4] = new uint8_t[sz]; memset(yuv_buffer[4],0,sz);
99 tdat = new uint8_t[4 * mb_width * mb_height];
100 memset(tdat, 0, 4 * mb_width * mb_height);
102 slice_decoder_t::init_tables();
104 /* Direct pointers to areas of contiguous fragments in YVU order per Microsoft */
105 for( cc=0; cc<3; ++cc ) {
111 refframe[0] = yuv_buffer[0];
112 oldrefframe[0] = yuv_buffer[1];
113 auxframe[0] = yuv_buffer[2];
115 refframe[2] = yuv_buffer[0] + sz;
116 oldrefframe[2] = yuv_buffer[1] + sz;
117 auxframe[2] = yuv_buffer[2] + sz;
119 refframe[1] = yuv_buffer[0] + sz;
120 oldrefframe[1] = yuv_buffer[1] + sz;
121 auxframe[1] = yuv_buffer[2] + sz;
123 if( scalable_mode == slice_decoder_t::sc_SPAT ) {
124 /* this assumes lower layer is 4:2:0 */
126 llframe0[0] = yuv_buffer[3] + sz;
127 llframe1[0] = yuv_buffer[4] + sz;
128 sz = padding[1] + size[2];
129 llframe0[2] = yuv_buffer[3] + sz;
130 llframe1[2] = yuv_buffer[4] + sz;
132 llframe0[1] = yuv_buffer[3] + sz;
133 llframe1[1] = yuv_buffer[4] + sz;
136 /* Initialize the YUV tables for software YUV decoding */
137 cr_to_r = new int[256];
138 crb_to_g = new int[256*256];
139 cb_to_b = new int[256];
140 int *cr_to_r_ptr = cr_to_r + 128;
141 int *crb_to_g_ptr = crb_to_g + 128*256 + 128;
142 int *cb_to_b_ptr = cb_to_b + 128;
144 for( i=-128; i<128; ++i ) {
145 cr_to_r_ptr[i] = (int)(1.371*i * 65536.);
146 for( j=-128; j<128; ++j )
147 crb_to_g_ptr[i*256 + j] = (int)((-0.698*i + -0.336*j) * 65536.);
148 cb_to_b_ptr[i] = (int)((1.732*i) * 65536.);
158 if( yuv_buffer[0] ) delete [] yuv_buffer[0];
159 if( yuv_buffer[1] ) delete [] yuv_buffer[1];
160 if( yuv_buffer[2] ) delete [] yuv_buffer[2];
162 if( subtitle_frame[0] ) delete [] subtitle_frame[0];
163 if( subtitle_frame[1] ) delete [] subtitle_frame[1];
164 if( subtitle_frame[2] ) delete [] subtitle_frame[2];
167 delete [] yuv_buffer[3];
168 delete [] yuv_buffer[4];
181 zigzag_scan_table = zig_zag_scan_nommx;
182 alternate_scan_table = alternate_scan_nommx;
190 track->width = horizontal_size;
191 track->height = vertical_size;
192 track->frame_rate = frame_rate;
193 demuxer_t *demux = vstream->demuxer;
195 /* Try to get the length of the file from GOP's */
196 if( !track->frame_offsets ) {
197 if( src->is_video_stream() ) {
198 /* Load the first GOP */
200 result = vstream->next_code(GOP_START_CODE);
201 if( !result ) vstream->get_bits(32);
202 if( !result ) result = get_gop_header();
203 first_frame = gop_to_frame(&gop_timecode);
204 /* GOPs are supposed to have 16 frames */
206 /* Read the last GOP in the file by seeking backward. */
207 demux->seek_byte(demux->movie_size());
208 demux->start_reverse();
209 result = demux->prev_code(GOP_START_CODE);
210 demux->start_forward();
212 vstream->get_bits(8);
213 if(!result) result = get_gop_header();
214 last_frame = gop_to_frame(&gop_timecode);
215 //zmsgs("3 %p\n", this);
216 /* Count number of frames to end */
218 result = vstream->next_code(PICTURE_START_CODE);
220 vstream->get_byte_noptr();
225 track->total_frames = last_frame-first_frame+1;
226 //zmsgs("mpeg3video_new 3 %ld\n", track->total_frames);
229 /* Try to get the length of the file from the multiplexing. */
230 /* Need a table of contents */
232 * track->total_frames = last_frame =
233 * (long)(demux->length() * frame_rate);
239 /* Get length from table of contents */
240 track->total_frames = track->total_frame_offsets-1;
243 maxframe = track->total_frames;
251 if( track->video_time >= 0 && src->pts_padding > 0 ) {
252 int pts_framenum = track->video_time*track->frame_rate + 0.5;
253 int padding = pts_framenum - track->pts_position;
255 if( track->vskip <= 0 ) {
257 if( padding > limit ) {
258 zmsgs("video start padding pid %02x @ %12d (%12.6f) %d\n",
259 track->pid, framenum, track->get_video_time(), padding);
264 else if( !(++track->vskip & 1) || padding > track->frame_rate )
266 if( result ) ++track->pts_position;
268 else if( track->vskip > 0 ) {
269 zmsgs("video end padding pid %02x @ %12d (%12.6f) %d\n",
270 track->pid, framenum, track->get_video_time(), (1+track->vskip)/2);
281 if( track->video_time >= 0 && src->pts_padding > 0 ) {
282 int pts_framenum = track->video_time*track->frame_rate + 0.5;
283 int skipping = track->pts_position - pts_framenum;
285 if( track->vskip >= 0 ) {
287 if( skipping > limit ) {
288 zmsgs("video start skipping pid %02x @ %12d (%12.6f) %d frames\n",
289 track->pid, framenum, track->get_video_time(), skipping);
294 else if( !(--track->vskip & 1) || skipping > track->frame_rate )
296 if( result ) --track->pts_position;
298 else if( track->vskip < 0 ) {
299 zmsgs("video end skipping pid %02x @ %12d (%12.6f) %d frames\n",
300 track->pid, framenum, track->get_video_time(), (1-track->vskip)/2);
310 while( vstream->eof() ) {
311 demuxer_t *demux = vstream->demuxer;
312 if( demux->seek_phys() ) return 1;
318 /* The way they do field based encoding, */
319 /* the I frames have the top field but both the I frame and */
320 /* subsequent P frame are interlaced to make the keyframe. */
327 got_top = got_bottom = 0;
331 if( (result=eof()) != 0 ) break;
332 if( (result=get_header()) != 0 ) break;
333 if( pict_struct != pics_FRAME_PICTURE ) secondfield = field;
334 /* if dropping frames then skip B frames, */
335 /* Don't skip the B frames which generate original */
336 /* repeated frames at the end of the skip sequence */
337 if( !skip_bframes || pict_type != pic_type_B ||
338 repeat_fields > 2*skip_bframes ) {
339 if( (result=get_picture()) != 0 ) break;
342 if( pict_struct == pics_FRAME_PICTURE ) { got_top = got_bottom = field; break; }
343 if( pict_struct == pics_TOP_FIELD ) got_top = field;
344 else if( pict_struct == pics_BOTTOM_FIELD ) got_bottom = field;
345 } while( !secondfield );
347 if( pict_type != pic_type_B )
354 read_frame_backend(int zskip_bframes)
357 //if( src->seekable ) {
358 // int app_pos = track->apparent_position();
359 // double vtime = track->get_video_time();
360 // int pts_frm = vtime * track->frame_rate + 0.5;
361 // zmsgs(" apr_pos %f: %d/%d + %d: pts_frame %d + %d\n", vtime,
362 // app_pos, framenum, app_pos - framenum, pts_frm, pts_frm-framenum);
364 if( !mpeg2 ) current_field = repeat_fields = 0;
366 /* Repeat if current_field is more than 1 field from repeat_fields */
367 if( !repeat_fields || current_field+2 >= repeat_fields ) {
368 if( (repeat_fields -= current_field) < 0 ) repeat_fields = 0;
369 track->update_video_time();
371 // if pts lags framenum, skip to next picture
372 // only skip once (double speed) to catch up
373 while( !(result=find_header()) ) {
374 if( !video_pts_skipping() ) break;
378 // if framenum lags pts, repeat picture
379 if( !result && !video_pts_padding() ) {
380 track->update_frame_pts();
381 skip_bframes = zskip_bframes;
382 //static const char *pstruct[] = { "nil", "top", "bot", "fld" };
383 //zmsgs("video %d PID %02x frame %d vtime %12.5f %c/%s %d 0x%010lx/0x%010lx %d/%d\n",
384 // result, track->pid, framenum, track->get_video_time(), "XIPBD"[pict_type],
385 // pstruct[pict_struct], skip_bframes, vstream->demuxer->last_packet_start,
386 // vstream->demuxer->absolute_position()-vstream->demuxer->zdata.length(),
387 // repeat_fields, current_field);
388 result = read_picture();
391 snprintf(&fn[0],sizeof(fn),"/tmp/dat/f%05d.pnm",framenum);
392 int fd = open(&fn[0],O_CREAT+O_TRUNC+O_WRONLY,0666);
393 write(fd,&fn, snprintf(&fn[0],sizeof(fn),
394 "P5\n%d %d\n255\n", coded_picture_width, coded_picture_height));
395 write(fd,output_src[0],coded_picture_width*coded_picture_height);
403 if( mpeg2 ) current_field = !repeat_fields ? 0 : current_field+2;
405 last_number = framenum++;
412 get_scaletable(int in_l, int out_l)
414 int *result = new int[out_l];
415 double scale = (double)in_l / out_l;
416 for( int i=0; i<out_l; ++i ) {
417 result[i] = (int)(scale * i);
423 gop_to_frame(timecode_t *gop_timecode)
425 /* Mirror of what mpeg2enc does */
426 int fps = (int)(frame_rate + 0.5);
427 int hour = gop_timecode->hour;
428 int minute = gop_timecode->minute;
429 int second = gop_timecode->second;
430 int frame = gop_timecode->frame;
431 long result = ((long)hour*3600 + minute*60 + second)*fps + frame;
436 /* ======================================================================= */
438 /* ======================================================================= */
443 video_t(zmpeg3_t *zsrc, zvtrack_t *ztrack)
447 vstream = new bits_t(zsrc, track->demuxer);
448 //zmsgs("%d\n", vstream->eof());
450 /* First frame is all green */
458 allocate_slice_buffers();
463 new_video_t(zvtrack_t *ztrack)
466 video_t *new_video = new video_t(this,ztrack);
468 /* Get encoding parameters from stream */
470 result = new_video->get_header();
472 new_video->init_video();
474 /* No header found */
476 zerr("no header found.\n");
489 delete_slice_buffers();
490 if( decoder_initted )
513 /* Read all the way up to and including the next picture start code */
515 read_raw(uint8_t *output, long *size, long max_size)
519 while( zcode!=PICTURE_START_CODE && zcode!=SEQUENCE_END_CODE &&
520 sz < max_size && !eof() ) {
521 uint8_t byte = vstream->get_byte_noptr();
523 zcode = (zcode << 8) | byte;
530 int zvideo_t::read_frame( uint8_t **output_rows,
531 int in_x, int in_y, int in_w, int in_h,
532 int out_w, int out_h, int color_model)
535 int frame_number, result;
538 this->output_rows = output_rows;
539 this->color_model = color_model;
541 /* Get scaling tables */
542 if( this->out_w != out_w || this->out_h != out_h ||
543 this->in_w != in_w || this->in_h != in_h ||
544 this->in_x != in_x || this->in_y != in_y) {
546 delete [] x_table; x_table = 0;
547 delete [] y_table; y_table = 0;
551 this->out_w = out_w; this->out_h = out_h;
552 this->in_w = in_w; this->in_h = in_h;
553 this->in_x = in_x; this->in_y = in_y;
556 x_table = get_scaletable(in_w, out_w);
557 y_table = get_scaletable(in_h, out_h);
559 //zmsgs("mpeg3video_read_frame 1 %d\n", framenum);
561 /* Recover from cache */
562 frame_number = frame_seek >= 0 ? frame_seek : framenum;
563 cache_t *cache = track->frame_cache;
564 if( frame_seek != last_number &&
565 cache->get_frame(frame_number, &y, &u, &v) ) {
566 //zmsgs("1 %d\n", frame_number);
567 /* Transfer with cropping */
568 if( y ) present_frame(y, u, v);
569 /* Advance either framenum or frame_seek */
570 if( frame_number == framenum )
571 framenum = ++frame_number;
572 else if( frame_number == frame_seek )
573 frame_seek = ++frame_number;
576 /* Only decode if it's a different frame */
577 if( frame_seek < 0 || last_number < 0 ||
578 frame_seek != last_number) {
579 if( !result ) result = seek();
580 if( !result ) result = read_frame_backend(0);
583 framenum = frame_seek + 1;
584 last_number = frame_seek;
588 present_frame(output_src[0], output_src[1], output_src[2]);
594 read_yuvframe(char *y_output, char *u_output, char *v_output,
595 int in_x, int in_y, int in_w, int in_h)
597 uint8_t *y = 0, *u = 0, *v = 0;
599 //zmsgs("1 %d\n", framenum);
601 this->y_output = y_output;
602 this->u_output = u_output;
603 this->v_output = v_output;
609 /* Recover from cache if framenum exists */
610 int frame_number = frame_seek >= 0 ? frame_seek : framenum;
611 cache_t *cache = track->frame_cache;
612 if( cache->get_frame(frame_number, &y, &u, &v) ) {
613 //zmsgs("1 %d\n", frame_number);
614 /* Transfer with cropping */
615 if( y ) present_frame(y, u, v);
616 /* Advance either framenum or frame_seek */
617 if( frame_number == framenum )
618 framenum = ++frame_number;
619 else if( frame_number == frame_seek )
620 frame_seek = ++frame_number;
623 if( !result ) result = seek();
624 if( !result ) result = read_frame_backend(0);
625 if( !result && output_src[0] )
626 present_frame(output_src[0], output_src[1], output_src[2]);
635 read_yuvframe_ptr(char **y_output, char **u_output, char **v_output)
638 int frame_number, result;
642 *y_output = *u_output = *v_output = 0;
644 frame_number = frame_seek >= 0 ? frame_seek : framenum;
645 cache_t *cache = track->frame_cache;
646 if( debug ) zmsgs("%d\n", __LINE__);
647 if( cache->get_frame(frame_number, &y, &u, &v) ) {
648 if( debug ) zmsgs("%d\n", __LINE__);
649 *y_output = (char*)y;
650 *u_output = (char*)u;
651 *v_output = (char*)v;
652 /* Advance either framenum or frame_seek */
653 if( frame_number == framenum )
654 framenum = ++frame_number;
655 else if( frame_number == frame_seek )
656 frame_seek = ++frame_number;
657 if( debug ) zmsgs("%d\n", __LINE__);
659 /* Only decode if it's a different frame */
660 else if( frame_seek < 0 || last_number < 0 ||
661 frame_seek != last_number) {
662 if( debug ) zmsgs("%d\n", __LINE__);
663 if( !result ) result = seek();
664 if( debug ) zmsgs("%d\n", __LINE__);
665 if( !result ) result = read_frame_backend(0);
666 if( debug ) zmsgs("%d\n", __LINE__);
668 if( output_src[0] ) {
669 *y_output = (char*)output_src[0];
670 *u_output = (char*)output_src[1];
671 *v_output = (char*)output_src[2];
673 if( debug ) zmsgs("%d\n", __LINE__);
676 if( debug ) zmsgs("%d\n", __LINE__);
677 framenum = frame_seek + 1;
678 last_number = frame_seek;
681 if( output_src[0 ]) {
682 *y_output = (char*)output_src[0];
683 *u_output = (char*)output_src[1];
684 *v_output = (char*)output_src[2];
686 if( debug ) zmsgs("%d\n", __LINE__);
689 if( debug ) zmsgs("%d\n", __LINE__);
691 /* Caching not used if byte seek */
699 switch( chroma_format ) {
700 case cfmt_422: return cmdl_YUV422P; break;
701 case cfmt_420: return cmdl_YUV420P; break;
717 for( int i=0; i<src->total_stracks; ++i ) {
718 strack_t *strack = src->strack[i];
719 if( strack->video != this ) continue;
720 strack->del_all_subtitles();
725 show_subtitle(int strk)
727 int ret = subtitle_track;
728 if( subtitle_track != strk ) {
730 subtitle_track = strk;
732 if( strk >= 0 ) cc->reset();
733 else { delete cc; cc = 0; }
743 zmsg(" *** sequence extension 1\n");
744 zmsgs("prog_seq=%d\n", prog_seq);
745 zmsg(" *** picture header 1\n");
746 zmsgs("pict_type=%d field_sequence=%d\n", pict_type, field_sequence);
747 zmsg(" *** picture coding extension 1\n");
748 zmsgs("field_sequence=%d repeatfirst=%d prog_frame=%d pict_struct=%d\n",
749 field_sequence, repeatfirst, prog_frame, pict_struct);