view libavcodec/scratch.c @ 9:ea1ba68cf0ed

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