dfba84cfbb121ce5a1ab1b27864cddc8edb84065
[libav.git] / 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 Libav.
7 *
8 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "internal.h"
24 #include "parser.h"
25 #include "mpegvideo.h"
26 #include "mpeg4video.h"
27 #include "mpeg4video_parser.h"
28
29 struct Mp4vParseContext {
30 ParseContext pc;
31 struct MpegEncContext enc;
32 int first_picture;
33 };
34
35 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
36 int vop_found, i;
37 uint32_t state;
38
39 vop_found= pc->frame_start_found;
40 state= pc->state;
41
42 i=0;
43 if(!vop_found){
44 for(i=0; i<buf_size; i++){
45 state= (state<<8) | buf[i];
46 if(state == 0x1B6){
47 i++;
48 vop_found=1;
49 break;
50 }
51 }
52 }
53
54 if(vop_found){
55 /* EOF considered as end of frame */
56 if (buf_size == 0)
57 return 0;
58 for(; i<buf_size; i++){
59 state= (state<<8) | buf[i];
60 if((state&0xFFFFFF00) == 0x100){
61 pc->frame_start_found=0;
62 pc->state=-1;
63 return i-3;
64 }
65 }
66 }
67 pc->frame_start_found= vop_found;
68 pc->state= state;
69 return END_NOT_FOUND;
70 }
71
72 /* XXX: make it use less memory */
73 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
74 AVCodecContext *avctx,
75 const uint8_t *buf, int buf_size)
76 {
77 struct Mp4vParseContext *pc = s1->priv_data;
78 MpegEncContext *s = &pc->enc;
79 GetBitContext gb1, *gb = &gb1;
80 int ret;
81
82 s->avctx = avctx;
83 s->current_picture_ptr = &s->current_picture;
84
85 if (avctx->extradata_size && pc->first_picture){
86 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
87 ret = ff_mpeg4_decode_picture_header(s, gb);
88 }
89
90 init_get_bits(gb, buf, 8 * buf_size);
91 ret = ff_mpeg4_decode_picture_header(s, gb);
92 if (s->width && (!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height)) {
93 ret = ff_set_dimensions(avctx, s->width, s->height);
94 if (ret < 0)
95 return ret;
96 }
97 s1->pict_type= s->pict_type;
98 pc->first_picture = 0;
99 return ret;
100 }
101
102 static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
103 {
104 struct Mp4vParseContext *pc = s->priv_data;
105
106 pc->first_picture = 1;
107 pc->enc.slice_context_count = 1;
108 return 0;
109 }
110
111 static int mpeg4video_parse(AVCodecParserContext *s,
112 AVCodecContext *avctx,
113 const uint8_t **poutbuf, int *poutbuf_size,
114 const uint8_t *buf, int buf_size)
115 {
116 ParseContext *pc = s->priv_data;
117 int next;
118
119 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
120 next= buf_size;
121 }else{
122 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
123
124 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
125 *poutbuf = NULL;
126 *poutbuf_size = 0;
127 return buf_size;
128 }
129 }
130 av_mpeg4_decode_header(s, avctx, buf, buf_size);
131
132 *poutbuf = buf;
133 *poutbuf_size = buf_size;
134 return next;
135 }
136
137
138 AVCodecParser ff_mpeg4video_parser = {
139 .codec_ids = { AV_CODEC_ID_MPEG4 },
140 .priv_data_size = sizeof(struct Mp4vParseContext),
141 .parser_init = mpeg4video_parse_init,
142 .parser_parse = mpeg4video_parse,
143 .parser_close = ff_parse_close,
144 .split = ff_mpeg4video_split,
145 };