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