FFmpeg
cavs_parser.c
Go to the documentation of this file.
1 /*
2  * Chinese AVS video (AVS1-P2, JiZhun profile) parser.
3  * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Chinese AVS video (AVS1-P2, JiZhun profile) parser
25  * @author Stefan Gehrer <stefan.gehrer@gmx.de>
26  */
27 
28 #include "parser.h"
29 #include "cavs.h"
30 #include "get_bits.h"
31 #include "mpeg12data.h"
32 #include "parser_internal.h"
33 #include "startcode.h"
34 
35 
36 /**
37  * Find the end of the current frame in the bitstream.
38  * @return the position of the first byte of the next frame, or -1
39  */
40 static int cavs_find_frame_end(ParseContext *pc, const uint8_t *buf,
41  int buf_size) {
42  int pic_found, i;
43  uint32_t state;
44 
45  pic_found= pc->frame_start_found;
46  state= pc->state;
47 
48  i=0;
49  if(!pic_found){
50  for(i=0; i<buf_size; i++){
51  state= (state<<8) | buf[i];
53  i++;
54  pic_found=1;
55  break;
56  }
57  }
58  }
59 
60  if(pic_found){
61  /* EOF considered as end of frame */
62  if (buf_size == 0)
63  return 0;
64  for(; i<buf_size; i++){
65  state= (state<<8) | buf[i];
68  pc->frame_start_found=0;
69  pc->state=-1;
70  return i-3;
71  }
72  }
73  }
74  pc->frame_start_found= pic_found;
75  pc->state= state;
76  return END_NOT_FOUND;
77 }
78 
80  GetBitContext *gb)
81 {
82  int frame_rate_code;
83  int width, height;
84  int mb_width, mb_height;
85 
86  skip_bits(gb, 8); // profile
87  skip_bits(gb, 8); // level
88  skip_bits1(gb); // progressive sequence
89 
90  width = get_bits(gb, 14);
91  height = get_bits(gb, 14);
92  if (width <= 0 || height <= 0) {
93  av_log(avctx, AV_LOG_ERROR, "Dimensions invalid\n");
94  return AVERROR_INVALIDDATA;
95  }
96  mb_width = (width + 15) >> 4;
97  mb_height = (height + 15) >> 4;
98 
99  skip_bits(gb, 2); // chroma format
100  skip_bits(gb, 3); // sample_precision
101  skip_bits(gb, 4); // aspect_ratio
102  frame_rate_code = get_bits(gb, 4);
103  if (frame_rate_code == 0 || frame_rate_code > 13) {
104  av_log(avctx, AV_LOG_WARNING,
105  "frame_rate_code %d is invalid\n", frame_rate_code);
106  frame_rate_code = 1;
107  }
108 
109  skip_bits(gb, 18); // bit_rate_lower
110  skip_bits1(gb); // marker_bit
111  skip_bits(gb, 12); // bit_rate_upper
112  skip_bits1(gb); // low_delay
113 
114  s->width = width;
115  s->height = height;
116  s->coded_width = 16 * mb_width;
117  s->coded_height = 16 * mb_height;
118  avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];
119 
120  return 0;
121 }
122 
124  const uint8_t *buf, int buf_size)
125 {
126  GetBitContext gb;
127  const uint8_t *buf_end;
128  const uint8_t *buf_ptr;
129  uint32_t stc = -1;
130 
131  s->key_frame = 0;
132  s->pict_type = AV_PICTURE_TYPE_NONE;
133 
134  if (buf_size == 0)
135  return 0;
136 
137  buf_ptr = buf;
138  buf_end = buf + buf_size;
139  for (;;) {
140  buf_ptr = avpriv_find_start_code(buf_ptr, buf_end, &stc);
141  if ((stc & 0xFFFFFE00) || buf_ptr == buf_end)
142  return 0;
143  switch (stc) {
144  case CAVS_START_CODE:
145  init_get_bits8(&gb, buf_ptr, buf_end - buf_ptr);
146  parse_seq_header(s, avctx, &gb);
147  break;
148  case PIC_I_START_CODE:
149  s->key_frame = 1;
150  s->pict_type = AV_PICTURE_TYPE_I;
151  break;
152  default:
153  break;
154  }
155  }
156 }
157 
159  AVCodecContext *avctx,
160  const uint8_t **poutbuf, int *poutbuf_size,
161  const uint8_t *buf, int buf_size)
162 {
163  ParseContext *pc = s->priv_data;
164  int next;
165 
166  if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
167  next= buf_size;
168  }else{
169  next= cavs_find_frame_end(pc, buf, buf_size);
170 
171  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
172  *poutbuf = NULL;
173  *poutbuf_size = 0;
174  return buf_size;
175  }
176  }
177 
178  cavs_parse_frame(s, avctx, buf, buf_size);
179 
180  *poutbuf = buf;
181  *poutbuf_size = buf_size;
182  return next;
183 }
184 
187  .priv_data_size = sizeof(ParseContext),
190 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
ff_parse_close
av_cold void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:298
parser_internal.h
parse_seq_header
static int parse_seq_header(AVCodecParserContext *s, AVCodecContext *avctx, GetBitContext *gb)
Definition: cavs_parser.c:79
ParseContext::state
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:383
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:551
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:136
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:337
ParseContext
Definition: parser.h:28
GetBitContext
Definition: get_bits.h:109
state
static struct @545 state
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:544
s
#define s(width, name)
Definition: cbs_vp9.c:198
get_bits.h
PIC_PB_START_CODE
#define PIC_PB_START_CODE
Definition: cavs.h:43
NULL
#define NULL
Definition: coverity.c:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
ParseContext::frame_start_found
int frame_start_found
Definition: parser.h:34
PIC_I_START_CODE
#define PIC_I_START_CODE
Definition: cavs.h:42
avpriv_find_start_code
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
CAVS_START_CODE
#define CAVS_START_CODE
Definition: cavs.h:41
parse
static int parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: apv_parser.c:46
startcode.h
height
#define height
Definition: dsp.h:89
cavs_find_frame_end
static int cavs_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
Definition: cavs_parser.c:40
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:277
skip_bits1
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:416
ff_combine_frame
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:211
FFCodecParser
Definition: parser_internal.h:29
ff_cavsvideo_parser
const FFCodecParser ff_cavsvideo_parser
Definition: cavs_parser.c:185
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2609
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
cavsvideo_parse
static int cavsvideo_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: cavs_parser.c:158
AV_CODEC_ID_CAVS
@ AV_CODEC_ID_CAVS
Definition: codec_id.h:139
parser.h
PARSER_CODEC_LIST
#define PARSER_CODEC_LIST(...)
Definition: parser_internal.h:76
AVCodecParserContext
Definition: avcodec.h:2575
ff_mpeg12_frame_rate_tab
const AVRational ff_mpeg12_frame_rate_tab[]
Definition: mpeg12framerate.c:24
mpeg12data.h
AVCodecContext
main external API structure.
Definition: avcodec.h:431
END_NOT_FOUND
#define END_NOT_FOUND
Definition: parser.h:40
cavs_parse_frame
static int cavs_parse_frame(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: cavs_parser.c:123
cavs.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
width
#define width
Definition: dsp.h:89