2 * H.26L/H.264/AVC/JVT/14496-10/... parser
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
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.
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.
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
24 * H.264 / AVC / MPEG4 part10 parser.
25 * @author Michael Niedermayer <michaelni@gmx.at>
29 #include "h264_parser.h"
34 int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
38 ParseContext *pc = &(h->s.parse_context);
39 //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]);
40 // mb_addr= pc->mb_addr - 1;
45 for(i=0; i<buf_size; i++){
47 #ifdef HAVE_FAST_UNALIGNED
48 /* we check i<buf_size instead of i+3/7 because its simpler
49 * and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end
51 # ifdef HAVE_FAST_64BIT
52 while(i<buf_size && !((~*(uint64_t*)(buf+i) & (*(uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
55 while(i<buf_size && !((~*(uint32_t*)(buf+i) & (*(uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
59 for(; i<buf_size; i++){
66 if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
67 else if(buf[i]) state = 7;
68 else state>>=1; //2->1, 1->0, 0->0
71 if(v==7 || v==8 || v==9){
72 if(pc->frame_start_found){
76 }else if(v==1 || v==2 || v==5){
77 if(pc->frame_start_found){
81 pc->frame_start_found = 1;
95 pc->frame_start_found= 0;
99 static int h264_parse(AVCodecParserContext *s,
100 AVCodecContext *avctx,
101 const uint8_t **poutbuf, int *poutbuf_size,
102 const uint8_t *buf, int buf_size)
104 H264Context *h = s->priv_data;
105 ParseContext *pc = &h->s.parse_context;
108 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
111 next= ff_h264_find_frame_end(h, buf, buf_size);
113 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
119 if(next<0 && next != END_NOT_FOUND){
120 assert(pc->last_index + next >= 0 );
121 ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
126 *poutbuf_size = buf_size;
130 static int h264_split(AVCodecContext *avctx,
131 const uint8_t *buf, int buf_size)
137 for(i=0; i<=buf_size; i++){
138 if((state&0xFFFFFF1F) == 0x107)
140 /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
142 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
144 while(i>4 && buf[i-5]==0) i--;
149 state= (state<<8) | buf[i];
154 static void close(AVCodecParserContext *s)
156 H264Context *h = s->priv_data;
157 ParseContext *pc = &h->s.parse_context;
163 AVCodecParser h264_parser = {
165 CODEC_ID_H264_VDPAU },