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