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