annotate vc1_parser.c @ 9004:aae80543bc6e libavcodec

Check return value of frame_start(), this avoids a crash if AVCodecContext::get_buffer failed. Not sure if returning -1 is the best possible solution but at least avoids the crash.
author reimar
date Sun, 22 Feb 2009 09:51:58 +0000
parents e9d9d946f213
children 9a642761d734
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4900
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
1 /*
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
2 * VC-1 and WMV3 parser
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
3 * Copyright (c) 2006-2007 Konstantin Shishkov
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
5 *
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
6 * This file is part of FFmpeg.
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
7 *
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
12 *
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
16 * Lesser General Public License for more details.
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
17 *
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
21 */
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
22
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
23 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 4931
diff changeset
24 * @file libavcodec/vc1_parser.c
4900
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
25 * VC-1 and WMV3 parser
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
26 */
4919
983533ccc393 Remove superfluous #includes, parser.h now includes its prerequisites.
diego
parents: 4904
diff changeset
27
4900
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
28 #include "parser.h"
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
29 #include "vc1.h"
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
30
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
31 /**
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
32 * finds the end of the current frame in the bitstream.
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
33 * @return the position of the first byte of the next frame, or -1
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
34 */
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
35 static int vc1_find_frame_end(ParseContext *pc, const uint8_t *buf,
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
36 int buf_size) {
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
37 int pic_found, i;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
38 uint32_t state;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
39
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
40 pic_found= pc->frame_start_found;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
41 state= pc->state;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
42
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
43 i=0;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
44 if(!pic_found){
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
45 for(i=0; i<buf_size; i++){
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
46 state= (state<<8) | buf[i];
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
47 if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
48 i++;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
49 pic_found=1;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
50 break;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
51 }
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
52 }
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
53 }
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
54
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
55 if(pic_found){
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
56 /* EOF considered as end of frame */
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
57 if (buf_size == 0)
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
58 return 0;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
59 for(; i<buf_size; i++){
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
60 state= (state<<8) | buf[i];
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
61 if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
62 pc->frame_start_found=0;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
63 pc->state=-1;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
64 return i-3;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
65 }
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
66 }
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
67 }
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
68 pc->frame_start_found= pic_found;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
69 pc->state= state;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
70 return END_NOT_FOUND;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
71 }
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
72
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
73 static int vc1_parse(AVCodecParserContext *s,
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
74 AVCodecContext *avctx,
4931
0d1cc37d9430 make some parser parameters const to avoid casting const to non-const
aurel
parents: 4927
diff changeset
75 const uint8_t **poutbuf, int *poutbuf_size,
4900
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
76 const uint8_t *buf, int buf_size)
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
77 {
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
78 ParseContext *pc = s->priv_data;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
79 int next;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
80
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
81 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
82 next= buf_size;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
83 }else{
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
84 next= vc1_find_frame_end(pc, buf, buf_size);
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
85
4931
0d1cc37d9430 make some parser parameters const to avoid casting const to non-const
aurel
parents: 4927
diff changeset
86 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
4900
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
87 *poutbuf = NULL;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
88 *poutbuf_size = 0;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
89 return buf_size;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
90 }
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
91 }
4931
0d1cc37d9430 make some parser parameters const to avoid casting const to non-const
aurel
parents: 4927
diff changeset
92 *poutbuf = buf;
4900
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
93 *poutbuf_size = buf_size;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
94 return next;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
95 }
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
96
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
97 static int vc1_split(AVCodecContext *avctx,
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
98 const uint8_t *buf, int buf_size)
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
99 {
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
100 int i;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
101 uint32_t state= -1;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
102
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
103 for(i=0; i<buf_size; i++){
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
104 state= (state<<8) | buf[i];
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
105 if(IS_MARKER(state) && state != VC1_CODE_SEQHDR && state != VC1_CODE_ENTRYPOINT)
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
106 return i-3;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
107 }
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
108 return 0;
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
109 }
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
110
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
111 AVCodecParser vc1_parser = {
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
112 { CODEC_ID_VC1 },
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
113 sizeof(ParseContext1),
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
114 NULL,
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
115 vc1_parse,
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
116 ff_parse1_close,
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
117 vc1_split,
36051a4c182a Move VC1 parser to its own file.
diego
parents:
diff changeset
118 };