comparison mpeg12.c @ 1211:126f766bc4c5 libavcodec

cleanup mpeg1/2 bitstream parser
author michaelni
date Tue, 22 Apr 2003 13:55:12 +0000
parents 2e06398e4647
children 358bbc952e27
comparison
equal deleted inserted replaced
1210:ede882dd601f 1211:126f766bc4c5
1541 1541
1542 s->block_last_index[n] = i; 1542 s->block_last_index[n] = i;
1543 return 0; 1543 return 0;
1544 } 1544 }
1545 1545
1546 /* compressed picture size */
1547 #define PICTURE_BUFFER_SIZE 100000
1548
1549 typedef struct Mpeg1Context { 1546 typedef struct Mpeg1Context {
1550 MpegEncContext mpeg_enc_ctx; 1547 MpegEncContext mpeg_enc_ctx;
1551 uint32_t header_state;
1552 int start_code; /* current start code */
1553 uint8_t buffer[PICTURE_BUFFER_SIZE];
1554 uint8_t *buf_ptr;
1555 int buffer_size;
1556 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ 1548 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
1557 int repeat_field; /* true if we must repeat the field */ 1549 int repeat_field; /* true if we must repeat the field */
1558 } Mpeg1Context; 1550 } Mpeg1Context;
1559 1551
1560 static int mpeg_decode_init(AVCodecContext *avctx) 1552 static int mpeg_decode_init(AVCodecContext *avctx)
1563 1555
1564 s->mpeg_enc_ctx.flags= avctx->flags; 1556 s->mpeg_enc_ctx.flags= avctx->flags;
1565 common_init(&s->mpeg_enc_ctx); 1557 common_init(&s->mpeg_enc_ctx);
1566 init_vlcs(&s->mpeg_enc_ctx); 1558 init_vlcs(&s->mpeg_enc_ctx);
1567 1559
1568 s->header_state = 0xff;
1569 s->mpeg_enc_ctx_allocated = 0; 1560 s->mpeg_enc_ctx_allocated = 0;
1570 s->buffer_size = PICTURE_BUFFER_SIZE;
1571 s->start_code = -1;
1572 s->buf_ptr = s->buffer;
1573 s->mpeg_enc_ctx.picture_number = 0; 1561 s->mpeg_enc_ctx.picture_number = 0;
1574 s->repeat_field = 0; 1562 s->repeat_field = 0;
1575 s->mpeg_enc_ctx.codec_id= avctx->codec->id; 1563 s->mpeg_enc_ctx.codec_id= avctx->codec->id;
1576 return 0; 1564 return 0;
1577 } 1565 }
1578 1566
1579 /* return the 8 bit start code value and update the search 1567 /* return the 8 bit start code value and update the search
1580 state. Return -1 if no start code found */ 1568 state. Return -1 if no start code found */
1581 static int find_start_code(uint8_t **pbuf_ptr, uint8_t *buf_end, 1569 static int find_start_code(uint8_t **pbuf_ptr, uint8_t *buf_end)
1582 uint32_t *header_state)
1583 { 1570 {
1584 uint8_t *buf_ptr; 1571 uint8_t *buf_ptr;
1585 unsigned int state, v; 1572 unsigned int state=0xFFFFFFFF, v;
1586 int val; 1573 int val;
1587 1574
1588 state = *header_state;
1589 buf_ptr = *pbuf_ptr; 1575 buf_ptr = *pbuf_ptr;
1590 while (buf_ptr < buf_end) { 1576 while (buf_ptr < buf_end) {
1591 v = *buf_ptr++; 1577 v = *buf_ptr++;
1592 if (state == 0x000001) { 1578 if (state == 0x000001) {
1593 state = ((state << 8) | v) & 0xffffff; 1579 state = ((state << 8) | v) & 0xffffff;
1597 state = ((state << 8) | v) & 0xffffff; 1583 state = ((state << 8) | v) & 0xffffff;
1598 } 1584 }
1599 val = -1; 1585 val = -1;
1600 found: 1586 found:
1601 *pbuf_ptr = buf_ptr; 1587 *pbuf_ptr = buf_ptr;
1602 *header_state = state;
1603 return val; 1588 return val;
1604 } 1589 }
1605 1590
1606 static int mpeg1_decode_picture(AVCodecContext *avctx, 1591 static int mpeg1_decode_picture(AVCodecContext *avctx,
1607 uint8_t *buf, int buf_size) 1592 uint8_t *buf, int buf_size)
1807 * DECODE_SLICE_EOP if the end of the picture is reached 1792 * DECODE_SLICE_EOP if the end of the picture is reached
1808 */ 1793 */
1809 static int mpeg_decode_slice(AVCodecContext *avctx, 1794 static int mpeg_decode_slice(AVCodecContext *avctx,
1810 AVFrame *pict, 1795 AVFrame *pict,
1811 int start_code, 1796 int start_code,
1812 uint8_t *buf, int buf_size) 1797 uint8_t **buf, int buf_size)
1813 { 1798 {
1814 Mpeg1Context *s1 = avctx->priv_data; 1799 Mpeg1Context *s1 = avctx->priv_data;
1815 MpegEncContext *s = &s1->mpeg_enc_ctx; 1800 MpegEncContext *s = &s1->mpeg_enc_ctx;
1816 int ret; 1801 int ret;
1817 const int field_pic= s->picture_structure != PICT_FRAME; 1802 const int field_pic= s->picture_structure != PICT_FRAME;
1872 } 1857 }
1873 } 1858 }
1874 } 1859 }
1875 s->first_slice = 0; 1860 s->first_slice = 0;
1876 1861
1877 init_get_bits(&s->gb, buf, buf_size*8); 1862 init_get_bits(&s->gb, *buf, buf_size*8);
1878 1863
1879 s->qscale = get_qscale(s); 1864 s->qscale = get_qscale(s);
1880 if(s->qscale == 0){ 1865 if(s->qscale == 0){
1881 fprintf(stderr, "qscale == 0\n"); 1866 fprintf(stderr, "qscale == 0\n");
1882 return DECODE_SLICE_MB_ADDR_ERROR; 1867 return DECODE_SLICE_MB_ADDR_ERROR;
1926 1911
1927 if (s->mb_intra || s->mv_type == MV_TYPE_16X16) { 1912 if (s->mb_intra || s->mv_type == MV_TYPE_16X16) {
1928 motion_x = s->mv[0][0][0]; 1913 motion_x = s->mv[0][0][0];
1929 motion_y = s->mv[0][0][1]; 1914 motion_y = s->mv[0][0][1];
1930 } else /*if (s->mv_type == MV_TYPE_FIELD)*/ { 1915 } else /*if (s->mv_type == MV_TYPE_FIELD)*/ {
1931 int i;
1932 motion_x = s->mv[0][0][0] + s->mv[0][1][0]; 1916 motion_x = s->mv[0][0][0] + s->mv[0][1][0];
1933 motion_y = s->mv[0][0][1] + s->mv[0][1][1]; 1917 motion_y = s->mv[0][0][1] + s->mv[0][1][1];
1934 motion_x = (motion_x>>1) | (motion_x&1); 1918 motion_x = (motion_x>>1) | (motion_x&1);
1935 } 1919 }
1936 s->motion_val[xy][0] = motion_x; 1920 s->motion_val[xy][0] = motion_x;
1994 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y); 1978 //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
1995 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END); 1979 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
1996 1980
1997 emms_c(); 1981 emms_c();
1998 1982
1983 *buf += get_bits_count(&s->gb)/8 - 1;
1984
1999 //intf("%d %d %d %d\n", s->mb_y, s->mb_height, s->pict_type, s->picture_number); 1985 //intf("%d %d %d %d\n", s->mb_y, s->mb_height, s->pict_type, s->picture_number);
2000 /* end of slice reached */ 1986 /* end of slice reached */
2001 if (s->mb_y<<field_pic == s->mb_height && !s->first_field) { 1987 if (s->mb_y<<field_pic == s->mb_height && !s->first_field) {
2002 /* end of image */ 1988 /* end of image */
2003 1989
2015 ff_print_debug_info(s, s->current_picture_ptr); 2001 ff_print_debug_info(s, s->current_picture_ptr);
2016 } else { 2002 } else {
2017 s->picture_number++; 2003 s->picture_number++;
2018 /* latency of 1 frame for I and P frames */ 2004 /* latency of 1 frame for I and P frames */
2019 /* XXX: use another variable than picture_number */ 2005 /* XXX: use another variable than picture_number */
2020 if (s->last_picture_ptr == NULL) { 2006 if (s->last_picture_ptr != NULL) {
2021 return DECODE_SLICE_OK;
2022 } else {
2023 *pict= *(AVFrame*)&s->last_picture; 2007 *pict= *(AVFrame*)&s->last_picture;
2024 ff_print_debug_info(s, s->last_picture_ptr); 2008 ff_print_debug_info(s, s->last_picture_ptr);
2025 } 2009 }
2026 } 2010 }
2027 return DECODE_SLICE_EOP; 2011 return DECODE_SLICE_EOP;
2168 avctx->dtg_active_format = p[0] & 0x0f; 2152 avctx->dtg_active_format = p[0] & 0x0f;
2169 } 2153 }
2170 } 2154 }
2171 } 2155 }
2172 2156
2157 /**
2158 * finds the end of the current frame in the bitstream.
2159 * @return the position of the first byte of the next frame, or -1
2160 */
2161 static int mpeg1_find_frame_end(MpegEncContext *s, uint8_t *buf, int buf_size){
2162 ParseContext *pc= &s->parse_context;
2163 int i;
2164 uint32_t state;
2165
2166 state= pc->state;
2167
2168 i=0;
2169 if(!pc->frame_start_found){
2170 for(i=0; i<buf_size; i++){
2171 state= (state<<8) | buf[i];
2172 if(state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE){
2173 i++;
2174 pc->frame_start_found=1;
2175 break;
2176 }
2177 }
2178 }
2179
2180 if(pc->frame_start_found){
2181 for(; i<buf_size; i++){
2182 state= (state<<8) | buf[i];
2183 if((state&0xFFFFFF00) == 0x100){
2184 if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
2185 pc->frame_start_found=0;
2186 pc->state=-1;
2187 return i-3;
2188 }
2189 }
2190 }
2191 }
2192 pc->state= state;
2193 return -1;
2194 }
2195
2173 /* handle buffering and image synchronisation */ 2196 /* handle buffering and image synchronisation */
2174 static int mpeg_decode_frame(AVCodecContext *avctx, 2197 static int mpeg_decode_frame(AVCodecContext *avctx,
2175 void *data, int *data_size, 2198 void *data, int *data_size,
2176 uint8_t *buf, int buf_size) 2199 uint8_t *buf, int buf_size)
2177 { 2200 {
2178 Mpeg1Context *s = avctx->priv_data; 2201 Mpeg1Context *s = avctx->priv_data;
2179 uint8_t *buf_end, *buf_ptr, *buf_start; 2202 uint8_t *buf_end, *buf_ptr;
2180 int len, start_code_found, ret, code, start_code, input_size; 2203 int ret, start_code, input_size;
2181 AVFrame *picture = data; 2204 AVFrame *picture = data;
2182 MpegEncContext *s2 = &s->mpeg_enc_ctx; 2205 MpegEncContext *s2 = &s->mpeg_enc_ctx;
2183
2184 dprintf("fill_buffer\n"); 2206 dprintf("fill_buffer\n");
2185 2207
2186 *data_size = 0; 2208 *data_size = 0;
2187 2209
2188 /* special case for last picture */ 2210 /* special case for last picture */
2193 *data_size = sizeof(AVFrame); 2215 *data_size = sizeof(AVFrame);
2194 } 2216 }
2195 return 0; 2217 return 0;
2196 } 2218 }
2197 2219
2220 if(s2->flags&CODEC_FLAG_TRUNCATED){
2221 int next;
2222
2223 next= mpeg1_find_frame_end(s2, buf, buf_size);
2224
2225 if( ff_combine_frame(s2, next, &buf, &buf_size) < 0 )
2226 return buf_size;
2227 }
2228
2198 buf_ptr = buf; 2229 buf_ptr = buf;
2199 buf_end = buf + buf_size; 2230 buf_end = buf + buf_size;
2200 2231
2201 #if 0 2232 #if 0
2202 if (s->repeat_field % 2 == 1) { 2233 if (s->repeat_field % 2 == 1) {
2207 *data_size = sizeof(AVPicture); 2238 *data_size = sizeof(AVPicture);
2208 goto the_end; 2239 goto the_end;
2209 } 2240 }
2210 } 2241 }
2211 #endif 2242 #endif
2212 while (buf_ptr < buf_end) { 2243 for(;;) {
2213 buf_start = buf_ptr;
2214 /* find start next code */ 2244 /* find start next code */
2215 code = find_start_code(&buf_ptr, buf_end, &s->header_state); 2245 start_code = find_start_code(&buf_ptr, buf_end);
2216 if (code >= 0) { 2246 if (start_code < 0){
2217 start_code_found = 1; 2247 printf("missing end of picture\n");
2218 } else { 2248 return FFMAX(1, buf_ptr - buf - s2->parse_context.last_index);
2219 start_code_found = 0; 2249 }
2220 } 2250
2221 /* copy to buffer */
2222 len = buf_ptr - buf_start;
2223 if (len + (s->buf_ptr - s->buffer) > s->buffer_size) {
2224 /* data too big : flush */
2225 s->buf_ptr = s->buffer;
2226 if (start_code_found)
2227 s->start_code = code;
2228 } else {
2229 memcpy(s->buf_ptr, buf_start, len);
2230 s->buf_ptr += len;
2231 if( (!(s2->flags&CODEC_FLAG_TRUNCATED)) && (!start_code_found)
2232 && s->buf_ptr+4<s->buffer+s->buffer_size){
2233 start_code_found= 1;
2234 code= 0x1FF;
2235 s->header_state=0xFF;
2236 s->buf_ptr[0]=0;
2237 s->buf_ptr[1]=0;
2238 s->buf_ptr[2]=1;
2239 s->buf_ptr[3]=0xFF;
2240 s->buf_ptr+=4;
2241 }
2242 if (start_code_found) {
2243 /* prepare data for next start code */ 2251 /* prepare data for next start code */
2244 input_size = s->buf_ptr - s->buffer; 2252 input_size = buf_end - buf_ptr;
2245 start_code = s->start_code;
2246 s->buf_ptr = s->buffer;
2247 s->start_code = code;
2248 switch(start_code) { 2253 switch(start_code) {
2249 case SEQ_START_CODE: 2254 case SEQ_START_CODE:
2250 mpeg1_decode_sequence(avctx, s->buffer, 2255 mpeg1_decode_sequence(avctx, buf_ptr,
2251 input_size); 2256 input_size);
2252 break; 2257 break;
2253 2258
2254 case PICTURE_START_CODE: 2259 case PICTURE_START_CODE:
2255 /* we have a complete image : we try to decompress it */ 2260 /* we have a complete image : we try to decompress it */
2256 mpeg1_decode_picture(avctx, 2261 mpeg1_decode_picture(avctx,
2257 s->buffer, input_size); 2262 buf_ptr, input_size);
2258 break; 2263 break;
2259 case EXT_START_CODE: 2264 case EXT_START_CODE:
2260 mpeg_decode_extension(avctx, 2265 mpeg_decode_extension(avctx,
2261 s->buffer, input_size); 2266 buf_ptr, input_size);
2262 break; 2267 break;
2263 case USER_START_CODE: 2268 case USER_START_CODE:
2264 mpeg_decode_user_data(avctx, 2269 mpeg_decode_user_data(avctx,
2265 s->buffer, input_size); 2270 buf_ptr, input_size);
2266 break; 2271 break;
2267 default: 2272 default:
2268 if (start_code >= SLICE_MIN_START_CODE && 2273 if (start_code >= SLICE_MIN_START_CODE &&
2269 start_code <= SLICE_MAX_START_CODE) { 2274 start_code <= SLICE_MAX_START_CODE) {
2270 2275
2272 if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break; 2277 if(s2->last_picture_ptr==NULL && s2->pict_type==B_TYPE) break;
2273 /* skip b frames if we are in a hurry */ 2278 /* skip b frames if we are in a hurry */
2274 if(avctx->hurry_up && s2->pict_type==B_TYPE) break; 2279 if(avctx->hurry_up && s2->pict_type==B_TYPE) break;
2275 /* skip everything if we are in a hurry>=5 */ 2280 /* skip everything if we are in a hurry>=5 */
2276 if(avctx->hurry_up>=5) break; 2281 if(avctx->hurry_up>=5) break;
2277 2282
2278 if (!s->mpeg_enc_ctx_allocated) break; 2283 if (!s->mpeg_enc_ctx_allocated) break;
2279 2284
2280 ret = mpeg_decode_slice(avctx, picture, 2285 ret = mpeg_decode_slice(avctx, picture,
2281 start_code, s->buffer, input_size); 2286 start_code, &buf_ptr, input_size);
2282 2287
2283 if (ret == DECODE_SLICE_EOP) { 2288 if (ret == DECODE_SLICE_EOP) {
2284 *data_size = sizeof(AVPicture); 2289 if(s2->last_picture_ptr) //FIXME merge with the stuff in mpeg_decode_slice
2285 goto the_end; 2290 *data_size = sizeof(AVPicture);
2291 return FFMAX(1, buf_ptr - buf - s2->parse_context.last_index);
2286 }else if(ret < 0){ 2292 }else if(ret < 0){
2287 if(ret == DECODE_SLICE_ERROR) 2293 if(ret == DECODE_SLICE_ERROR)
2288 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR); 2294 ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, AC_ERROR|DC_ERROR|MV_ERROR);
2289 2295
2290 fprintf(stderr,"Error while decoding slice\n"); 2296 fprintf(stderr,"Error while decoding slice\n");
2291 if(ret==DECODE_SLICE_FATAL_ERROR) return -1; 2297 if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
2292 } 2298 }
2293 } 2299 }
2294 break; 2300 break;
2295 } 2301 }
2296 } 2302 }
2297 }
2298 }
2299 the_end:
2300 return buf_ptr - buf;
2301 } 2303 }
2302 2304
2303 static int mpeg_decode_end(AVCodecContext *avctx) 2305 static int mpeg_decode_end(AVCodecContext *avctx)
2304 { 2306 {
2305 Mpeg1Context *s = avctx->priv_data; 2307 Mpeg1Context *s = avctx->priv_data;