| rev |
line source |
|
nengel@2
|
1 static void *entropy_thread(void *arg){
|
|
nengel@2
|
2 H264Context *h = (H264Context *) arg;
|
|
nengel@2
|
3 EDSlice *s;
|
|
nengel@2
|
4
|
|
nengel@2
|
5 H264Cabac hcabac;
|
|
nengel@2
|
6 CABACContext cabac;
|
|
nengel@2
|
7
|
|
nengel@2
|
8 ff_init_cabac_states();
|
|
nengel@2
|
9
|
|
nengel@2
|
10 if (init_cabac(h, &hcabac)<0)
|
|
nengel@2
|
11 return NULL;
|
|
nengel@2
|
12
|
|
nengel@2
|
13 for(;;){
|
|
nengel@2
|
14 {
|
|
nengel@2
|
15 pthread_mutex_lock(&h->lock[ENTROPY]);
|
|
nengel@2
|
16 while (h->ed_cnt<=0)
|
|
nengel@2
|
17 pthread_cond_wait(&h->cond[ENTROPY], &h->lock[ENTROPY]);
|
|
nengel@2
|
18 s= &h->ed_q[h->ed_fo];
|
|
nengel@2
|
19 pthread_mutex_unlock(&h->lock[ENTROPY]);
|
|
nengel@2
|
20 h->ed_fo++; h->ed_fo %= MAX_SLICE_COUNT;
|
|
nengel@2
|
21 }
|
|
nengel@2
|
22 if (s->state<0)
|
|
nengel@2
|
23 break;
|
|
nengel@2
|
24
|
|
nengel@2
|
25 decode_slice_entropy(&hcabac, &cabac, s);
|
|
nengel@2
|
26
|
|
nengel@2
|
27 {
|
|
nengel@2
|
28 pthread_mutex_lock(&h->lock[MBDEC]);
|
|
nengel@2
|
29 while (h->mbdec_cnt >= MAX_SLICE_COUNT)
|
|
nengel@2
|
30 pthread_cond_wait(&h->cond[MBDEC], &h->lock[MBDEC]);
|
|
nengel@2
|
31 h->mbdec_q[h->mbdec_fi] = *((MBSlice *) s);
|
|
nengel@2
|
32 h->mbdec_cnt++;
|
|
nengel@2
|
33 h->mbdec_fi++; h->mbdec_fi %= MAX_SLICE_COUNT;
|
|
nengel@2
|
34 pthread_cond_signal(&h->cond[MBDEC]);
|
|
nengel@2
|
35 pthread_mutex_unlock(&h->lock[MBDEC]);
|
|
nengel@2
|
36 }
|
|
nengel@2
|
37 {
|
|
nengel@2
|
38 pthread_mutex_lock(&h->lock[ENTROPY]);
|
|
nengel@2
|
39 h->ed_cnt--;
|
|
nengel@2
|
40 pthread_cond_signal(&h->cond[ENTROPY]);
|
|
nengel@2
|
41 pthread_mutex_unlock(&h->lock[ENTROPY]);
|
|
nengel@2
|
42 }
|
|
nengel@2
|
43 }
|
|
nengel@2
|
44
|
|
nengel@2
|
45 {
|
|
nengel@2
|
46 pthread_mutex_lock(&h->lock[MBDEC]);
|
|
nengel@2
|
47 while (h->mbdec_cnt >= MAX_SLICE_COUNT)
|
|
nengel@2
|
48 pthread_cond_wait(&h->cond[MBDEC], &h->lock[MBDEC]);
|
|
nengel@2
|
49 h->mbdec_q[h->mbdec_fi] = *((MBSlice *) s);
|
|
nengel@2
|
50 h->mbdec_cnt++;
|
|
nengel@2
|
51 h->mbdec_fi++; h->mbdec_fi %= MAX_SLICE_COUNT;
|
|
nengel@2
|
52 pthread_cond_signal(&h->cond[MBDEC]);
|
|
nengel@2
|
53 pthread_mutex_unlock(&h->lock[MBDEC]);
|
|
nengel@2
|
54
|
|
nengel@2
|
55 }
|
|
nengel@2
|
56
|
|
nengel@2
|
57 free_cabac(&hcabac);
|
|
nengel@2
|
58
|
|
nengel@2
|
59 pthread_exit(NULL);
|
|
nengel@2
|
60 return NULL;
|
|
nengel@2
|
61
|
|
nengel@2
|
62 }
|
|
nengel@2
|
63 /*
|
|
nengel@2
|
64 * The following code is the main loop of the file converter
|
|
nengel@2
|
65 */
|
|
nengel@2
|
66 int av_transcode_1ed(int ifile, int ofile, int frame_width, int frame_height) {
|
|
nengel@2
|
67 H264Context *h;
|
|
nengel@2
|
68 pthread_t read_thr, parsenal_thr, entropy_thr, mbdec_thr, write_thr;
|
|
nengel@2
|
69
|
|
nengel@2
|
70 h = ff_h264_decode_init(ifile, ofile, frame_width, frame_height);
|
|
nengel@2
|
71
|
|
nengel@2
|
72 timer_start = av_gettime();
|
|
nengel@2
|
73
|
|
nengel@2
|
74 // pthread_create(&read_thr, NULL, read_thread, h);
|
|
nengel@2
|
75 // pthread_create(&parsenal_thr, NULL, parsenal_thread, h);
|
|
nengel@2
|
76 pthread_create(&entropy_thr, NULL, entropy_mbd_thread, h);
|
|
nengel@2
|
77
|
|
nengel@2
|
78 // pthread_create(&mbdec_thr, NULL, mbdec_thread, h);
|
|
nengel@2
|
79
|
|
nengel@2
|
80 // pthread_create(&write_thr, NULL, write_thread, h);
|
|
nengel@2
|
81
|
|
nengel@2
|
82 // pthread_join(read_thr, NULL);
|
|
nengel@2
|
83 // pthread_join(parsenal_thr, NULL);
|
|
nengel@2
|
84 pthread_join(entropy_thr, NULL);
|
|
nengel@2
|
85 // pthread_join(mbdec_thr, NULL);
|
|
nengel@2
|
86 // printf("before write_thr\n");
|
|
nengel@2
|
87 // pthread_join(write_thr, NULL);
|
|
nengel@2
|
88
|
|
nengel@2
|
89 /* finished ! */
|
|
nengel@2
|
90 ff_h264_decode_end(h);
|
|
nengel@2
|
91
|
|
nengel@2
|
92 return 0;
|
|
nengel@2
|
93 }
|
|
nengel@2
|
94
|
|
nengel@2
|
95 static void reset_h264mb(EDSlice *s, int mb_width, int mb_height){
|
|
nengel@2
|
96 for (int i=0; i<mb_height; i++){
|
|
nengel@2
|
97 for (int j=0; j<mb_width; j++){
|
|
nengel@2
|
98 H264Mb *m = &s->mbs[i*mb_width + j];
|
|
nengel@2
|
99
|
|
nengel@2
|
100 m->left_mb_xy=0;
|
|
nengel@2
|
101 m->top_mb_xy = 0;
|
|
nengel@2
|
102 }
|
|
nengel@2
|
103 }
|
|
nengel@2
|
104 }
|
|
nengel@2
|
105
|
|
nengel@2
|
106 static void *entropy_mbd_thread(void *arg){
|
|
nengel@2
|
107 H264Context *h = (H264Context *) arg;
|
|
nengel@2
|
108
|
|
nengel@2
|
109 EDSlice slice, *s=&slice;
|
|
nengel@2
|
110 MBSlice mbslice, *s2=&mbslice;
|
|
nengel@2
|
111 H264Cabac hcabac;
|
|
nengel@2
|
112 CABACContext cabac;
|
|
nengel@2
|
113 int frames =0;
|
|
nengel@2
|
114 MBDecContext mbdec, *d=&mbdec;
|
|
nengel@2
|
115 int size=h->width*h->height;
|
|
nengel@2
|
116 WriteContext write, *w=&write;
|
|
nengel@2
|
117 AVCodecParserContext parser, *pc= &parser;
|
|
nengel@2
|
118 NalContext nal, *n=&nal;
|
|
nengel@2
|
119
|
|
nengel@2
|
120
|
|
nengel@2
|
121 memset(pc, 0, sizeof(AVCodecParserContext));
|
|
nengel@2
|
122 pc->buffer_size = 2048;
|
|
nengel@2
|
123 pc->final_frame = 0;
|
|
nengel@2
|
124 pc->cur_len= 0;
|
|
nengel@2
|
125 pc->data = av_mallocz(2048 + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
nengel@2
|
126 pc->size = 2048;
|
|
nengel@2
|
127 pc->eof_reached =0;
|
|
nengel@2
|
128 pc->ifile = h->ifile;
|
|
nengel@2
|
129
|
|
nengel@2
|
130 //init parse
|
|
nengel@2
|
131 memset(n, 0, sizeof(NalContext));
|
|
nengel@2
|
132 n->width = h->width;
|
|
nengel@2
|
133 n->height = h->height;
|
|
nengel@2
|
134 n->mb_height = h->mb_height;
|
|
nengel@2
|
135 n->mb_width = h->mb_width;
|
|
nengel@2
|
136 n->b4_stride = n->mb_width*4 + 1;
|
|
nengel@2
|
137 n->mb_stride = n->mb_width + 1;
|
|
nengel@2
|
138 n->outputed_poc = INT_MIN;
|
|
nengel@2
|
139 // memset(s, 0, sizeof(EDSlice));
|
|
nengel@2
|
140 // ff_init_slice(n, s);
|
|
nengel@2
|
141 //
|
|
nengel@2
|
142
|
|
nengel@2
|
143 memset(w, 0, sizeof(WriteContext));
|
|
nengel@2
|
144 w->bit_buffer_size= FFMAX(1024*256, 6*size + 200);
|
|
nengel@2
|
145 w->bit_buffer= av_mallocz(w->bit_buffer_size);
|
|
nengel@2
|
146
|
|
nengel@2
|
147
|
|
nengel@2
|
148
|
|
nengel@2
|
149 ff_h264dsp_init(&d->hdsp);
|
|
nengel@2
|
150 ff_h264_pred_init(&d->hpc);
|
|
nengel@2
|
151 dsputil_init(&d->dsp);
|
|
nengel@2
|
152 d->hdsp.qpel_put= d->dsp.put_h264_qpel_pixels_tab;
|
|
nengel@2
|
153 d->hdsp.qpel_avg= d->dsp.avg_h264_qpel_pixels_tab;
|
|
nengel@2
|
154 d->mb_height = (h->height + 15) / 16;
|
|
nengel@2
|
155 d->mb_width = (h->width + 15) / 16;
|
|
nengel@2
|
156 d->linesize = h->width + EDGE_WIDTH*2;
|
|
nengel@2
|
157 d->uvlinesize = d->linesize>>1;
|
|
nengel@2
|
158
|
|
nengel@2
|
159 for(int i=0; i<16; i++){
|
|
nengel@2
|
160 d->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*d->linesize*((scan8[i] - scan8[0])>>3);
|
|
nengel@2
|
161 }
|
|
nengel@2
|
162 for(int i=0; i<4; i++){
|
|
nengel@2
|
163 d->block_offset[16+i]=
|
|
nengel@2
|
164 d->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*d->uvlinesize*((scan8[i] - scan8[0])>>3);
|
|
nengel@2
|
165 }
|
|
nengel@2
|
166
|
|
nengel@2
|
167 d->scratchpad= av_mallocz((h->width+64)*4*16*2*sizeof(uint8_t));
|
|
nengel@2
|
168
|
|
nengel@2
|
169 ff_init_cabac_states();
|
|
nengel@2
|
170
|
|
nengel@2
|
171 if (init_cabac(h, &hcabac)<0)
|
|
nengel@2
|
172 return NULL;
|
|
nengel@2
|
173
|
|
nengel@2
|
174 while(!pc->final_frame && frames_max++ < 1000){
|
|
nengel@2
|
175 Picture *out;
|
|
nengel@2
|
176
|
|
nengel@2
|
177 RawFrame *frm;
|
|
nengel@2
|
178 Picture *pic=NULL;
|
|
nengel@2
|
179
|
|
nengel@2
|
180 RawFrame frm_read;
|
|
nengel@2
|
181 frm_read.state =0;
|
|
nengel@2
|
182 av_read_frame_internal(pc, &frm_read);
|
|
nengel@2
|
183 frm = &frm_read;
|
|
nengel@2
|
184
|
|
nengel@2
|
185 if (frm->state < 0)
|
|
nengel@2
|
186 break;
|
|
nengel@2
|
187 /*
|
|
nengel@2
|
188 {
|
|
nengel@2
|
189 pthread_mutex_lock(&h->lock[PARSE2]);
|
|
nengel@2
|
190 while (h->slice_cnt<=0)
|
|
nengel@2
|
191 pthread_cond_wait(&h->cond[PARSE2], &h->lock[PARSE2]);
|
|
nengel@2
|
192 h->slice_cnt--;
|
|
nengel@2
|
193 s= &h->slices[h->slice_next++];
|
|
nengel@2
|
194 h->slice_next %= MAX_SLICE_COUNT;
|
|
nengel@2
|
195 pthread_mutex_unlock(&h->lock[PARSE2]);
|
|
nengel@2
|
196 }*/
|
|
nengel@2
|
197 ff_init_slice(n, s);
|
|
nengel@2
|
198 reset_h264mb(s, n->mb_width, n->mb_height);
|
|
nengel@2
|
199 for(int i=0; i<MAX_PIC_COUNT; i++){
|
|
nengel@2
|
200 if(h->picture[i].reference==0){
|
|
nengel@2
|
201 pic= &h->picture[i];
|
|
nengel@2
|
202 break;
|
|
nengel@2
|
203 }
|
|
nengel@2
|
204 }
|
|
nengel@2
|
205 // {
|
|
nengel@2
|
206 // pthread_mutex_lock(&h->lock[PARSE3]);
|
|
nengel@2
|
207 // while (h->free_pic_cnt<=0)
|
|
nengel@2
|
208 // pthread_cond_wait(&h->cond[PARSE3], &h->lock[PARSE3]);
|
|
nengel@2
|
209 // h->free_pic_cnt--;
|
|
nengel@2
|
210 // /* use first free picture */
|
|
nengel@2
|
211 // for(int i=0; i<MAX_PIC_COUNT; i++){
|
|
nengel@2
|
212 // if(h->picture[i].reference==0){
|
|
nengel@2
|
213 // pic= &h->picture[i];
|
|
nengel@2
|
214 // break;
|
|
nengel@2
|
215 // }
|
|
nengel@2
|
216 // }
|
|
nengel@2
|
217 // pthread_mutex_unlock(&h->lock[PARSE3]);
|
|
nengel@2
|
218 // }
|
|
nengel@2
|
219 ff_alloc_picture(n, s, pic);
|
|
nengel@2
|
220
|
|
nengel@2
|
221 decode_nal_units(n, s, frm, pic);
|
|
nengel@2
|
222
|
|
nengel@2
|
223
|
|
nengel@2
|
224 decode_slice_entropy(&hcabac, &cabac, s);
|
|
nengel@2
|
225 memcpy( s2, s, sizeof(MBSlice)); //this only copys the COMMON_SLICE part
|
|
nengel@2
|
226 av_freep(&s->gb.raw);
|
|
nengel@2
|
227 decode_slice_mb_seq(d, s2);
|
|
nengel@2
|
228
|
|
nengel@2
|
229 // if (s2->release_cnt>0) {
|
|
nengel@2
|
230 // int i;
|
|
nengel@2
|
231 // for (i=0; i<s2->release_cnt; i++){
|
|
nengel@2
|
232 // if ((s2->release_ref[i]->reference & ~2) == 0)
|
|
nengel@2
|
233 // default_release_buffer(h, s2->release_ref[i]);
|
|
nengel@2
|
234 // else
|
|
nengel@2
|
235 // s2->release_ref[i]->reference &= ~2;
|
|
nengel@2
|
236 // }
|
|
nengel@2
|
237 // s->release_cnt=0;
|
|
nengel@2
|
238 // }
|
|
nengel@2
|
239
|
|
nengel@2
|
240 if (s->release_cnt>0) {
|
|
nengel@2
|
241 int i;
|
|
nengel@2
|
242 for (i=0; i<s->release_cnt; i++){
|
|
nengel@2
|
243 s->release_ref[i]->reference &= ~2;
|
|
nengel@2
|
244 }
|
|
nengel@2
|
245 s->release_cnt=0;
|
|
nengel@2
|
246 }
|
|
nengel@2
|
247
|
|
nengel@2
|
248
|
|
nengel@2
|
249 {
|
|
nengel@2
|
250 pthread_mutex_lock(&h->lock[PARSE2]);
|
|
nengel@2
|
251 h->slice_cnt++;
|
|
nengel@2
|
252 pthread_cond_signal(&h->cond[PARSE2]);
|
|
nengel@2
|
253 pthread_mutex_unlock(&h->lock[PARSE2]);
|
|
nengel@2
|
254 }
|
|
nengel@2
|
255
|
|
nengel@2
|
256 out =output_frame(w, s2->current_picture, h->ofile, h->width, h->height);
|
|
nengel@2
|
257 print_report(w->frame_number, w->video_size, 0);
|
|
nengel@2
|
258
|
|
nengel@2
|
259 if (out){
|
|
nengel@2
|
260 // if ((out->reference & ~1) == 0)
|
|
nengel@2
|
261 // default_release_buffer(h, out);
|
|
nengel@2
|
262 // else
|
|
nengel@2
|
263 out->reference &= ~1;
|
|
nengel@2
|
264 }
|
|
nengel@2
|
265
|
|
nengel@2
|
266 {
|
|
nengel@2
|
267 pthread_mutex_lock(&h->lock[ENTROPY]);
|
|
nengel@2
|
268 h->ed_cnt--;
|
|
nengel@2
|
269 pthread_cond_signal(&h->cond[ENTROPY]);
|
|
nengel@2
|
270 pthread_mutex_unlock(&h->lock[ENTROPY]);
|
|
nengel@2
|
271 }
|
|
nengel@2
|
272 }
|
|
nengel@2
|
273 while (output_frame(w, NULL, h->ofile, h->width, h->height));
|
|
nengel@2
|
274 print_report(w->frame_number, w->video_size, 1);
|
|
nengel@2
|
275
|
|
nengel@2
|
276 av_free(w->bit_buffer);
|
|
nengel@2
|
277
|
|
nengel@2
|
278 {//propagate exit
|
|
nengel@2
|
279 pthread_mutex_lock(&h->lock[WRITE]);
|
|
nengel@2
|
280 while (h->write_cnt>= MAX_DELAYED_PIC_COUNT)
|
|
nengel@2
|
281 pthread_cond_wait(&h->cond[WRITE], &h->lock[WRITE]);
|
|
nengel@2
|
282 last_pic.reference = -1;
|
|
nengel@2
|
283 h->write_q[h->write_fi] = &last_pic;
|
|
nengel@2
|
284 h->write_cnt++;
|
|
nengel@2
|
285 h->write_fi++; h->write_fi %= MAX_DELAYED_PIC_COUNT;
|
|
nengel@2
|
286 pthread_cond_signal(&h->cond[WRITE]);
|
|
nengel@2
|
287 pthread_mutex_unlock(&h->lock[WRITE]);
|
|
nengel@2
|
288
|
|
nengel@2
|
289 }
|
|
nengel@2
|
290 free_cabac(&hcabac);
|
|
nengel@2
|
291
|
|
nengel@2
|
292 pthread_exit(NULL);
|
|
nengel@2
|
293 return NULL;
|
|
nengel@2
|
294
|
|
nengel@2
|
295 }
|