]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegvideo_parser.c
Make timestamp interpolation work with mpeg2 field pictures.
[ffmpeg] / libavcodec / mpegvideo_parser.c
1 /*
2  * MPEG1 / MPEG2 video parser
3  * Copyright (c) 2000,2001 Fabrice Bellard.
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "parser.h"
24 #include "mpegvideo.h"
25
26 static void mpegvideo_extract_headers(AVCodecParserContext *s,
27                                       AVCodecContext *avctx,
28                                       const uint8_t *buf, int buf_size)
29 {
30     ParseContext1 *pc = s->priv_data;
31     const uint8_t *buf_end;
32     uint32_t start_code;
33     int frame_rate_index, ext_type, bytes_left;
34     int frame_rate_ext_n, frame_rate_ext_d;
35     int picture_structure, top_field_first, repeat_first_field, progressive_frame;
36     int horiz_size_ext, vert_size_ext, bit_rate_ext;
37 //FIXME replace the crap with get_bits()
38     s->repeat_pict = 0;
39     s->parity = 0;
40     buf_end = buf + buf_size;
41     while (buf < buf_end) {
42         start_code= -1;
43         buf= ff_find_start_code(buf, buf_end, &start_code);
44         bytes_left = buf_end - buf;
45         switch(start_code) {
46         case PICTURE_START_CODE:
47             if (bytes_left >= 2) {
48                 s->pict_type = (buf[1] >> 3) & 7;
49             }
50             break;
51         case SEQ_START_CODE:
52             if (bytes_left >= 7) {
53                 pc->width  = (buf[0] << 4) | (buf[1] >> 4);
54                 pc->height = ((buf[1] & 0x0f) << 8) | buf[2];
55                 avcodec_set_dimensions(avctx, pc->width, pc->height);
56                 frame_rate_index = buf[3] & 0xf;
57                 pc->frame_rate.den = avctx->time_base.den = ff_frame_rate_tab[frame_rate_index].num;
58                 pc->frame_rate.num = avctx->time_base.num = ff_frame_rate_tab[frame_rate_index].den;
59                 avctx->bit_rate = ((buf[4]<<10) | (buf[5]<<2) | (buf[6]>>6))*400;
60                 avctx->codec_id = CODEC_ID_MPEG1VIDEO;
61                 avctx->sub_id = 1;
62             }
63             break;
64         case EXT_START_CODE:
65             if (bytes_left >= 1) {
66                 ext_type = (buf[0] >> 4);
67                 switch(ext_type) {
68                 case 0x1: /* sequence extension */
69                     if (bytes_left >= 6) {
70                         horiz_size_ext = ((buf[1] & 1) << 1) | (buf[2] >> 7);
71                         vert_size_ext = (buf[2] >> 5) & 3;
72                         bit_rate_ext = ((buf[2] & 0x1F)<<7) | (buf[3]>>1);
73                         frame_rate_ext_n = (buf[5] >> 5) & 3;
74                         frame_rate_ext_d = (buf[5] & 0x1f);
75                         pc->progressive_sequence = buf[1] & (1 << 3);
76                         avctx->has_b_frames= !(buf[5] >> 7);
77
78                         pc->width  |=(horiz_size_ext << 12);
79                         pc->height |=( vert_size_ext << 12);
80                         avctx->bit_rate += (bit_rate_ext << 18) * 400;
81                         avcodec_set_dimensions(avctx, pc->width, pc->height);
82                         avctx->time_base.den = pc->frame_rate.den * (frame_rate_ext_n + 1);
83                         avctx->time_base.num = pc->frame_rate.num * (frame_rate_ext_d + 1);
84                         avctx->codec_id = CODEC_ID_MPEG2VIDEO;
85                         avctx->sub_id = 2; /* forces MPEG2 */
86                     }
87                     break;
88                 case 0x8: /* picture coding extension */
89                     if (bytes_left >= 5) {
90                         picture_structure = buf[2]&3;
91                         top_field_first = buf[3] & (1 << 7);
92                         repeat_first_field = buf[3] & (1 << 1);
93                         progressive_frame = buf[4] & (1 << 7);
94
95                         /* check if we must repeat the frame */
96                         if (repeat_first_field) {
97                             if (pc->progressive_sequence) {
98                                 if (top_field_first)
99                                     s->repeat_pict = 4;
100                                 else
101                                     s->repeat_pict = 2;
102                             } else if (progressive_frame) {
103                                 s->repeat_pict = 1;
104                             }
105                         }
106
107                         /* the packet only represents half a frame
108                            XXX,FIXME maybe find a different solution */
109                         if(picture_structure != 3){
110                             s->repeat_pict = -1;
111                             s->parity = picture_structure-1;
112                         }else
113                             s->parity = !top_field_first;
114                     }
115                     break;
116                 }
117             }
118             break;
119         case -1:
120             goto the_end;
121         default:
122             /* we stop parsing when we encounter a slice. It ensures
123                that this function takes a negligible amount of time */
124             if (start_code >= SLICE_MIN_START_CODE &&
125                 start_code <= SLICE_MAX_START_CODE)
126                 goto the_end;
127             break;
128         }
129     }
130  the_end: ;
131 }
132
133 static int mpegvideo_parse(AVCodecParserContext *s,
134                            AVCodecContext *avctx,
135                            const uint8_t **poutbuf, int *poutbuf_size,
136                            const uint8_t *buf, int buf_size)
137 {
138     ParseContext1 *pc1 = s->priv_data;
139     ParseContext *pc= &pc1->pc;
140     int next;
141
142     if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
143         next= buf_size;
144     }else{
145         next= ff_mpeg1_find_frame_end(pc, buf, buf_size);
146
147         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
148             *poutbuf = NULL;
149             *poutbuf_size = 0;
150             return buf_size;
151         }
152
153     }
154     /* we have a full frame : we just parse the first few MPEG headers
155        to have the full timing information. The time take by this
156        function should be negligible for uncorrupted streams */
157     mpegvideo_extract_headers(s, avctx, buf, buf_size);
158 #if 0
159     printf("pict_type=%d frame_rate=%0.3f repeat_pict=%d\n",
160            s->pict_type, (double)avctx->time_base.den / avctx->time_base.num, s->repeat_pict);
161 #endif
162
163     *poutbuf = buf;
164     *poutbuf_size = buf_size;
165     return next;
166 }
167
168 static int mpegvideo_split(AVCodecContext *avctx,
169                            const uint8_t *buf, int buf_size)
170 {
171     int i;
172     uint32_t state= -1;
173
174     for(i=0; i<buf_size; i++){
175         state= (state<<8) | buf[i];
176         if(state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100)
177             return i-3;
178     }
179     return 0;
180 }
181
182 AVCodecParser mpegvideo_parser = {
183     { CODEC_ID_MPEG1VIDEO, CODEC_ID_MPEG2VIDEO },
184     sizeof(ParseContext1),
185     NULL,
186     mpegvideo_parse,
187     ff_parse1_close,
188     mpegvideo_split,
189 };