FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vc1_parser.c
Go to the documentation of this file.
1 /*
2  * VC-1 and WMV3 parser
3  * Copyright (c) 2006-2007 Konstantin Shishkov
4  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, 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 /**
24  * @file
25  * VC-1 and WMV3 parser
26  */
27 
28 #include "libavutil/attributes.h"
29 #include "parser.h"
30 #include "vc1.h"
31 #include "get_bits.h"
32 
33 typedef struct {
37 
39  const uint8_t *buf, int buf_size)
40 {
41  VC1ParseContext *vpc = s->priv_data;
42  GetBitContext gb;
43  const uint8_t *start, *end, *next;
45 
46  vpc->v.s.avctx = avctx;
47  vpc->v.parse_only = 1;
48  vpc->v.first_pic_header_flag = 1;
49  next = buf;
50  s->repeat_pict = 0;
51 
52  for(start = buf, end = buf + buf_size; next < end; start = next){
53  int buf2_size, size;
54 
55  next = find_next_marker(start + 4, end);
56  size = next - start - 4;
57  buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
58  init_get_bits(&gb, buf2, buf2_size * 8);
59  if(size <= 0) continue;
60  switch(AV_RB32(start)){
61  case VC1_CODE_SEQHDR:
62  ff_vc1_decode_sequence_header(avctx, &vpc->v, &gb);
63  break;
65  ff_vc1_decode_entry_point(avctx, &vpc->v, &gb);
66  break;
67  case VC1_CODE_FRAME:
68  if(vpc->v.profile < PROFILE_ADVANCED)
69  ff_vc1_parse_frame_header (&vpc->v, &gb);
70  else
71  ff_vc1_parse_frame_header_adv(&vpc->v, &gb);
72 
73  /* keep AV_PICTURE_TYPE_BI internal to VC1 */
74  if (vpc->v.s.pict_type == AV_PICTURE_TYPE_BI)
76  else
77  s->pict_type = vpc->v.s.pict_type;
78 
79  if (avctx->ticks_per_frame > 1){
80  // process pulldown flags
81  s->repeat_pict = 1;
82  // Pulldown flags are only valid when 'broadcast' has been set.
83  // So ticks_per_frame will be 2
84  if (vpc->v.rff){
85  // repeat field
86  s->repeat_pict = 2;
87  }else if (vpc->v.rptfrm){
88  // repeat frames
89  s->repeat_pict = vpc->v.rptfrm * 2 + 1;
90  }
91  }
92 
93  if (vpc->v.broadcast && vpc->v.interlace && !vpc->v.psf)
94  s->field_order = vpc->v.tff ? AV_FIELD_TT : AV_FIELD_BB;
95  else
97 
98  break;
99  }
100  }
101 
102  av_free(buf2);
103 }
104 
105 /**
106  * Find the end of the current frame in the bitstream.
107  * @return the position of the first byte of the next frame, or -1
108  */
110  int buf_size) {
111  int pic_found, i;
112  uint32_t state;
113 
114  pic_found= pc->frame_start_found;
115  state= pc->state;
116 
117  i=0;
118  if(!pic_found){
119  for(i=0; i<buf_size; i++){
120  state= (state<<8) | buf[i];
121  if(state == VC1_CODE_FRAME || state == VC1_CODE_FIELD){
122  i++;
123  pic_found=1;
124  break;
125  }
126  }
127  }
128 
129  if(pic_found){
130  /* EOF considered as end of frame */
131  if (buf_size == 0)
132  return 0;
133  for(; i<buf_size; i++){
134  state= (state<<8) | buf[i];
135  if(IS_MARKER(state) && state != VC1_CODE_FIELD && state != VC1_CODE_SLICE){
136  pc->frame_start_found=0;
137  pc->state=-1;
138  return i-3;
139  }
140  }
141  }
142  pc->frame_start_found= pic_found;
143  pc->state= state;
144  return END_NOT_FOUND;
145 }
146 
148  AVCodecContext *avctx,
149  const uint8_t **poutbuf, int *poutbuf_size,
150  const uint8_t *buf, int buf_size)
151 {
152  VC1ParseContext *vpc = s->priv_data;
153  int next;
154 
156  next= buf_size;
157  }else{
158  next= vc1_find_frame_end(&vpc->pc, buf, buf_size);
159 
160  if (ff_combine_frame(&vpc->pc, next, &buf, &buf_size) < 0) {
161  *poutbuf = NULL;
162  *poutbuf_size = 0;
163  return buf_size;
164  }
165  }
166 
167  vc1_extract_headers(s, avctx, buf, buf_size);
168 
169  *poutbuf = buf;
170  *poutbuf_size = buf_size;
171  return next;
172 }
173 
174 static int vc1_split(AVCodecContext *avctx,
175  const uint8_t *buf, int buf_size)
176 {
177  int i;
178  uint32_t state= -1;
179  int charged=0;
180 
181  for(i=0; i<buf_size; i++){
182  state= (state<<8) | buf[i];
183  if(IS_MARKER(state)){
184  if(state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT){
185  charged=1;
186  }else if(charged){
187  return i-3;
188  }
189  }
190  }
191  return 0;
192 }
193 
195 {
196  VC1ParseContext *vpc = s->priv_data;
197  vpc->v.s.slice_context_count = 1;
198  return ff_vc1_init_common(&vpc->v);
199 }
200 
202  .codec_ids = { AV_CODEC_ID_VC1 },
203  .priv_data_size = sizeof(VC1ParseContext),
204  .parser_init = vc1_parse_init,
205  .parser_parse = vc1_parse,
206  .parser_close = ff_parse_close,
207  .split = vc1_split,
208 };