100l, I broke H.264 again, forgot one hunk.
[libav.git] / libavcodec / h264_parser.c
CommitLineData
26b4fe82
AJ
1/*
2 * H.26L/H.264/AVC/JVT/14496-10/... parser
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
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
26b4fe82
AJ
20 */
21
22/**
23 * @file h264_parser.c
24 * H.264 / AVC / MPEG4 part10 parser.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28#include "parser.h"
29#include "h264_parser.h"
30
31#include <assert.h>
32
33
34int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
35{
36 int i;
37 uint32_t state;
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;
41 state= pc->state;
42 if(state>13)
43 state= 7;
44
45 for(i=0; i<buf_size; i++){
46 if(state==7){
47 for(; i<buf_size; i++){
48 if(!buf[i]){
49 state=2;
50 break;
51 }
52 }
53 }else if(state<=2){
54 if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5
55 else if(buf[i]) state = 7;
56 else state>>=1; //2->1, 1->0, 0->0
57 }else if(state<=5){
58 int v= buf[i] & 0x1F;
59 if(v==7 || v==8 || v==9){
60 if(pc->frame_start_found){
61 i++;
9aa1cfec 62 goto found;
26b4fe82
AJ
63 }
64 }else if(v==1 || v==2 || v==5){
65 if(pc->frame_start_found){
66 state+=8;
67 continue;
68 }else
69 pc->frame_start_found = 1;
70 }
71 state= 7;
72 }else{
73 if(buf[i] & 0x80)
74 goto found;
75 state= 7;
76 }
77 }
78 pc->state= state;
79 return END_NOT_FOUND;
9aa1cfec
DP
80
81found:
82 pc->state=7;
83 pc->frame_start_found= 0;
84 return i-(state&5);
26b4fe82
AJ
85}
86
87static int h264_parse(AVCodecParserContext *s,
88 AVCodecContext *avctx,
89 const uint8_t **poutbuf, int *poutbuf_size,
90 const uint8_t *buf, int buf_size)
91{
92 H264Context *h = s->priv_data;
93 ParseContext *pc = &h->s.parse_context;
94 int next;
95
96 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
97 next= buf_size;
98 }else{
99 next= ff_h264_find_frame_end(h, buf, buf_size);
100
101 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
102 *poutbuf = NULL;
103 *poutbuf_size = 0;
104 return buf_size;
105 }
106
107 if(next<0 && next != END_NOT_FOUND){
108 assert(pc->last_index + next >= 0 );
109 ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state
110 }
111 }
112
113 *poutbuf = buf;
114 *poutbuf_size = buf_size;
115 return next;
116}
117
118static int h264_split(AVCodecContext *avctx,
119 const uint8_t *buf, int buf_size)
120{
121 int i;
122 uint32_t state = -1;
123 int has_sps= 0;
124
125 for(i=0; i<=buf_size; i++){
126 if((state&0xFFFFFF1F) == 0x107)
127 has_sps=1;
128/* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){
129 }*/
130 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
131 if(has_sps){
132 while(i>4 && buf[i-5]==0) i--;
133 return i-4;
134 }
135 }
136 if (i<buf_size)
137 state= (state<<8) | buf[i];
138 }
139 return 0;
140}
141
dd990075 142static void close(AVCodecParserContext *s)
3ee4f5e4
MN
143{
144 H264Context *h = s->priv_data;
145 ParseContext *pc = &h->s.parse_context;
146
147 av_free(pc->buffer);
148}
149
26b4fe82
AJ
150
151AVCodecParser h264_parser = {
152 { CODEC_ID_H264 },
153 sizeof(H264Context),
154 NULL,
155 h264_parse,
3ee4f5e4 156 close,
26b4fe82
AJ
157 h264_split,
158};