]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpeg4video_parser.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / mpeg4video_parser.c
1 /*
2  * MPEG4 Video frame extraction
3  * Copyright (c) 2003 Fabrice Bellard
4  * Copyright (c) 2003 Michael Niedermayer
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 #define UNCHECKED_BITSTREAM_READER 1
24
25 #include "parser.h"
26 #include "mpegvideo.h"
27 #include "mpeg4video.h"
28 #include "mpeg4video_parser.h"
29
30
31 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
32     int vop_found, i;
33     uint32_t state;
34
35     vop_found= pc->frame_start_found;
36     state= pc->state;
37
38     i=0;
39     if(!vop_found){
40         for(i=0; i<buf_size; i++){
41             state= (state<<8) | buf[i];
42             if(state == 0x1B6){
43                 i++;
44                 vop_found=1;
45                 break;
46             }
47         }
48     }
49
50     if(vop_found){
51         /* EOF considered as end of frame */
52         if (buf_size == 0)
53             return 0;
54         for(; i<buf_size; i++){
55             state= (state<<8) | buf[i];
56             if((state&0xFFFFFF00) == 0x100){
57                 pc->frame_start_found=0;
58                 pc->state=-1;
59                 return i-3;
60             }
61         }
62     }
63     pc->frame_start_found= vop_found;
64     pc->state= state;
65     return END_NOT_FOUND;
66 }
67
68 /* XXX: make it use less memory */
69 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
70                                   AVCodecContext *avctx,
71                                   const uint8_t *buf, int buf_size)
72 {
73     ParseContext1 *pc = s1->priv_data;
74     MpegEncContext *s = pc->enc;
75     GetBitContext gb1, *gb = &gb1;
76     int ret;
77
78     s->avctx = avctx;
79     s->current_picture_ptr = &s->current_picture;
80
81     if (avctx->extradata_size && pc->first_picture){
82         init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
83         ret = ff_mpeg4_decode_picture_header(s, gb);
84     }
85
86     init_get_bits(gb, buf, 8 * buf_size);
87     ret = ff_mpeg4_decode_picture_header(s, gb);
88     if (s->width && (!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height)) {
89         avcodec_set_dimensions(avctx, s->width, s->height);
90     }
91     s1->pict_type= s->pict_type;
92     pc->first_picture = 0;
93     return ret;
94 }
95
96 static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
97 {
98     ParseContext1 *pc = s->priv_data;
99
100     pc->enc = av_mallocz(sizeof(MpegEncContext));
101     if (!pc->enc)
102         return -1;
103     pc->first_picture = 1;
104     pc->enc->quant_precision=5;
105     return 0;
106 }
107
108 static int mpeg4video_parse(AVCodecParserContext *s,
109                            AVCodecContext *avctx,
110                            const uint8_t **poutbuf, int *poutbuf_size,
111                            const uint8_t *buf, int buf_size)
112 {
113     ParseContext *pc = s->priv_data;
114     int next;
115
116     if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
117         next= buf_size;
118     }else{
119         next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
120
121         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
122             *poutbuf = NULL;
123             *poutbuf_size = 0;
124             return buf_size;
125         }
126     }
127     av_mpeg4_decode_header(s, avctx, buf, buf_size);
128
129     *poutbuf = buf;
130     *poutbuf_size = buf_size;
131     return next;
132 }
133
134
135 AVCodecParser ff_mpeg4video_parser = {
136     .codec_ids      = { CODEC_ID_MPEG4 },
137     .priv_data_size = sizeof(ParseContext1),
138     .parser_init    = mpeg4video_parse_init,
139     .parser_parse   = mpeg4video_parse,
140     .parser_close   = ff_parse1_close,
141     .split          = ff_mpeg4video_split,
142 };