diff libavcodec/scratch.c @ 2:897f711a7157

rearrange to work with autoconf
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Tue, 25 Sep 2012 15:55:33 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libavcodec/scratch.c	Tue Sep 25 15:55:33 2012 +0200
     1.3 @@ -0,0 +1,295 @@
     1.4 +static void *entropy_thread(void *arg){
     1.5 +	H264Context *h = (H264Context *) arg;
     1.6 +	EDSlice *s;
     1.7 +	
     1.8 +	H264Cabac hcabac;
     1.9 +	CABACContext cabac;
    1.10 +	
    1.11 +	ff_init_cabac_states();
    1.12 +	
    1.13 +	if (init_cabac(h, &hcabac)<0)
    1.14 +		return NULL;
    1.15 +	
    1.16 +	for(;;){
    1.17 +		{
    1.18 +			pthread_mutex_lock(&h->lock[ENTROPY]);
    1.19 +			while (h->ed_cnt<=0)
    1.20 +				pthread_cond_wait(&h->cond[ENTROPY], &h->lock[ENTROPY]);
    1.21 +			s= &h->ed_q[h->ed_fo];
    1.22 +			pthread_mutex_unlock(&h->lock[ENTROPY]);
    1.23 +			h->ed_fo++; h->ed_fo %= MAX_SLICE_COUNT;
    1.24 +		}
    1.25 +		if (s->state<0)
    1.26 +			break;
    1.27 +		
    1.28 +		decode_slice_entropy(&hcabac, &cabac, s);
    1.29 +		
    1.30 +		{
    1.31 +			pthread_mutex_lock(&h->lock[MBDEC]);
    1.32 +			while (h->mbdec_cnt >= MAX_SLICE_COUNT)
    1.33 +				pthread_cond_wait(&h->cond[MBDEC], &h->lock[MBDEC]);
    1.34 +			h->mbdec_q[h->mbdec_fi] = *((MBSlice *) s);
    1.35 +			h->mbdec_cnt++;
    1.36 +			h->mbdec_fi++; h->mbdec_fi %= MAX_SLICE_COUNT;
    1.37 +			pthread_cond_signal(&h->cond[MBDEC]);
    1.38 +			pthread_mutex_unlock(&h->lock[MBDEC]);
    1.39 +		}
    1.40 +		{
    1.41 +			pthread_mutex_lock(&h->lock[ENTROPY]);
    1.42 +			h->ed_cnt--;
    1.43 +			pthread_cond_signal(&h->cond[ENTROPY]);
    1.44 +			pthread_mutex_unlock(&h->lock[ENTROPY]);
    1.45 +		}
    1.46 +	}
    1.47 +	
    1.48 +	{
    1.49 +		pthread_mutex_lock(&h->lock[MBDEC]);
    1.50 +		while (h->mbdec_cnt >= MAX_SLICE_COUNT)
    1.51 +			pthread_cond_wait(&h->cond[MBDEC], &h->lock[MBDEC]);
    1.52 +		h->mbdec_q[h->mbdec_fi] = *((MBSlice *) s);
    1.53 +		h->mbdec_cnt++;
    1.54 +		h->mbdec_fi++; h->mbdec_fi %= MAX_SLICE_COUNT;
    1.55 +		pthread_cond_signal(&h->cond[MBDEC]);
    1.56 +		pthread_mutex_unlock(&h->lock[MBDEC]);
    1.57 +		
    1.58 +	}
    1.59 +	
    1.60 +	free_cabac(&hcabac);
    1.61 +	
    1.62 +	pthread_exit(NULL);
    1.63 +	return NULL;
    1.64 +	
    1.65 +}
    1.66 +/*
    1.67 +* The following code is the main loop of the file converter
    1.68 +*/
    1.69 +int av_transcode_1ed(int ifile, int ofile, int frame_width, int frame_height) {
    1.70 +	H264Context *h;
    1.71 +	pthread_t read_thr, parsenal_thr, entropy_thr, mbdec_thr, write_thr;
    1.72 +	
    1.73 +	h = ff_h264_decode_init(ifile, ofile, frame_width, frame_height);
    1.74 +	
    1.75 +	timer_start = av_gettime();
    1.76 +	
    1.77 +	//    pthread_create(&read_thr, NULL, read_thread, h);
    1.78 +	//    pthread_create(&parsenal_thr, NULL, parsenal_thread, h);
    1.79 +	pthread_create(&entropy_thr, NULL, entropy_mbd_thread, h);
    1.80 +	
    1.81 +	// pthread_create(&mbdec_thr, NULL, mbdec_thread, h);
    1.82 +	
    1.83 +	//   pthread_create(&write_thr, NULL, write_thread, h);
    1.84 +	
    1.85 +	//   pthread_join(read_thr, NULL);
    1.86 +	//    pthread_join(parsenal_thr, NULL);
    1.87 +	pthread_join(entropy_thr, NULL);
    1.88 +	//    pthread_join(mbdec_thr, NULL);
    1.89 +	//	printf("before write_thr\n");
    1.90 +	//    pthread_join(write_thr, NULL);
    1.91 +	
    1.92 +	/* finished ! */
    1.93 +	ff_h264_decode_end(h);
    1.94 +	
    1.95 +	return 0;
    1.96 +}
    1.97 +
    1.98 +static void reset_h264mb(EDSlice *s, int mb_width, int mb_height){
    1.99 +	for (int i=0; i<mb_height; i++){
   1.100 +		for (int j=0; j<mb_width; j++){
   1.101 +			H264Mb *m = &s->mbs[i*mb_width + j];
   1.102 +
   1.103 +			m->left_mb_xy=0;
   1.104 +			m->top_mb_xy = 0;
   1.105 +		}
   1.106 +	}
   1.107 +}
   1.108 +
   1.109 +static void *entropy_mbd_thread(void *arg){
   1.110 +	H264Context *h = (H264Context *) arg;
   1.111 +
   1.112 +	EDSlice slice, *s=&slice;
   1.113 +	MBSlice mbslice, *s2=&mbslice;
   1.114 +	H264Cabac hcabac;
   1.115 +	CABACContext cabac;
   1.116 +	int frames =0;
   1.117 +	MBDecContext mbdec, *d=&mbdec;
   1.118 +	int size=h->width*h->height;
   1.119 +	WriteContext write, *w=&write;
   1.120 +	AVCodecParserContext parser, *pc= &parser;
   1.121 +	NalContext nal, *n=&nal;
   1.122 +
   1.123 +
   1.124 +	memset(pc, 0, sizeof(AVCodecParserContext));
   1.125 +	pc->buffer_size = 2048;
   1.126 +	pc->final_frame = 0;
   1.127 +	pc->cur_len= 0;
   1.128 +	pc->data = av_mallocz(2048 + FF_INPUT_BUFFER_PADDING_SIZE);
   1.129 +	pc->size = 2048;
   1.130 +	pc->eof_reached =0;
   1.131 +	pc->ifile = h->ifile;
   1.132 +
   1.133 +	//init parse
   1.134 +	memset(n, 0, sizeof(NalContext));
   1.135 +	n->width = h->width;
   1.136 +	n->height = h->height;
   1.137 +	n->mb_height = h->mb_height;
   1.138 +	n->mb_width  = h->mb_width;
   1.139 +	n->b4_stride = n->mb_width*4 + 1;
   1.140 +	n->mb_stride = n->mb_width + 1;
   1.141 +	n->outputed_poc = INT_MIN;
   1.142 +// 	memset(s, 0, sizeof(EDSlice));
   1.143 +// 	ff_init_slice(n, s);
   1.144 +//
   1.145 +
   1.146 +	memset(w, 0, sizeof(WriteContext));
   1.147 +	w->bit_buffer_size= FFMAX(1024*256, 6*size + 200);
   1.148 +	w->bit_buffer=  av_mallocz(w->bit_buffer_size);
   1.149 +
   1.150 +
   1.151 +
   1.152 +	ff_h264dsp_init(&d->hdsp);
   1.153 +	ff_h264_pred_init(&d->hpc);
   1.154 +	dsputil_init(&d->dsp);
   1.155 +	d->hdsp.qpel_put= d->dsp.put_h264_qpel_pixels_tab;
   1.156 +	d->hdsp.qpel_avg= d->dsp.avg_h264_qpel_pixels_tab;
   1.157 +	d->mb_height = (h->height + 15) / 16;
   1.158 +	d->mb_width  = (h->width  + 15) / 16;
   1.159 +	d->linesize = h->width + EDGE_WIDTH*2;
   1.160 +	d->uvlinesize = d->linesize>>1;
   1.161 +
   1.162 +	for(int i=0; i<16; i++){
   1.163 +		d->block_offset[i]= 4*((scan8[i] - scan8[0])&7) + 4*d->linesize*((scan8[i] - scan8[0])>>3);
   1.164 +	}
   1.165 +	for(int i=0; i<4; i++){
   1.166 +		d->block_offset[16+i]=
   1.167 +		d->block_offset[20+i]= 4*((scan8[i] - scan8[0])&7) + 4*d->uvlinesize*((scan8[i] - scan8[0])>>3);
   1.168 +	}
   1.169 +
   1.170 +	d->scratchpad= av_mallocz((h->width+64)*4*16*2*sizeof(uint8_t));
   1.171 +
   1.172 +	ff_init_cabac_states();
   1.173 +
   1.174 +	if (init_cabac(h, &hcabac)<0)
   1.175 +		return NULL;
   1.176 +
   1.177 +	while(!pc->final_frame && frames_max++ < 1000){
   1.178 +		Picture *out;
   1.179 +
   1.180 +		RawFrame *frm;
   1.181 +		Picture *pic=NULL;
   1.182 +
   1.183 +		RawFrame frm_read;
   1.184 +		frm_read.state =0;
   1.185 +		av_read_frame_internal(pc, &frm_read);
   1.186 +		frm = &frm_read;
   1.187 +
   1.188 +		if (frm->state < 0)
   1.189 +			break;
   1.190 +/*
   1.191 +		{
   1.192 +			pthread_mutex_lock(&h->lock[PARSE2]);
   1.193 +			while (h->slice_cnt<=0)
   1.194 +				pthread_cond_wait(&h->cond[PARSE2], &h->lock[PARSE2]);
   1.195 +			h->slice_cnt--;
   1.196 +			s= &h->slices[h->slice_next++];
   1.197 +			h->slice_next %= MAX_SLICE_COUNT;
   1.198 +			pthread_mutex_unlock(&h->lock[PARSE2]);
   1.199 +		}*/
   1.200 +		ff_init_slice(n, s);
   1.201 +		reset_h264mb(s, n->mb_width, n->mb_height);
   1.202 +		for(int i=0; i<MAX_PIC_COUNT; i++){
   1.203 +			if(h->picture[i].reference==0){
   1.204 +				pic= &h->picture[i];
   1.205 +				break;
   1.206 +			}
   1.207 +		}
   1.208 +// 		{
   1.209 +// 			pthread_mutex_lock(&h->lock[PARSE3]);
   1.210 +// 			while (h->free_pic_cnt<=0)
   1.211 +// 				pthread_cond_wait(&h->cond[PARSE3], &h->lock[PARSE3]);
   1.212 +// 			h->free_pic_cnt--;
   1.213 +// 			/* use first free picture */
   1.214 +// 			for(int i=0; i<MAX_PIC_COUNT; i++){
   1.215 +// 				if(h->picture[i].reference==0){
   1.216 +// 					pic= &h->picture[i];
   1.217 +// 					break;
   1.218 +// 				}
   1.219 +// 			}
   1.220 +// 			pthread_mutex_unlock(&h->lock[PARSE3]);
   1.221 +// 		}
   1.222 +		ff_alloc_picture(n, s, pic);
   1.223 +
   1.224 +		decode_nal_units(n, s, frm, pic);
   1.225 +
   1.226 +
   1.227 +		decode_slice_entropy(&hcabac, &cabac, s);
   1.228 +		memcpy( s2, s, sizeof(MBSlice)); //this only copys the COMMON_SLICE part
   1.229 +		av_freep(&s->gb.raw);
   1.230 +		decode_slice_mb_seq(d, s2);
   1.231 +
   1.232 +//         if (s2->release_cnt>0) {
   1.233 +//             int i;
   1.234 +//             for (i=0; i<s2->release_cnt; i++){
   1.235 +//                 if ((s2->release_ref[i]->reference & ~2) == 0)
   1.236 +//                     default_release_buffer(h, s2->release_ref[i]);
   1.237 +//                 else
   1.238 +//                     s2->release_ref[i]->reference &= ~2;
   1.239 +//             }
   1.240 +//             s->release_cnt=0;
   1.241 +//         }
   1.242 +
   1.243 +if (s->release_cnt>0) {
   1.244 +	int i;
   1.245 +	for (i=0; i<s->release_cnt; i++){
   1.246 +		s->release_ref[i]->reference &= ~2;
   1.247 +	}
   1.248 +	s->release_cnt=0;
   1.249 +}
   1.250 +
   1.251 +
   1.252 +        {
   1.253 +			pthread_mutex_lock(&h->lock[PARSE2]);
   1.254 +			h->slice_cnt++;
   1.255 +			pthread_cond_signal(&h->cond[PARSE2]);
   1.256 +			pthread_mutex_unlock(&h->lock[PARSE2]);
   1.257 +		}
   1.258 +
   1.259 +		out =output_frame(w, s2->current_picture, h->ofile, h->width, h->height);
   1.260 +		print_report(w->frame_number, w->video_size, 0);
   1.261 +
   1.262 +		if (out){
   1.263 +// 			if ((out->reference & ~1) == 0)
   1.264 +// 				default_release_buffer(h, out);
   1.265 +// 			else
   1.266 +				out->reference &= ~1;
   1.267 +		}
   1.268 +
   1.269 +		{
   1.270 +			pthread_mutex_lock(&h->lock[ENTROPY]);
   1.271 +			h->ed_cnt--;
   1.272 +			pthread_cond_signal(&h->cond[ENTROPY]);
   1.273 +			pthread_mutex_unlock(&h->lock[ENTROPY]);
   1.274 +		}
   1.275 +	}
   1.276 +	while (output_frame(w, NULL, h->ofile, h->width, h->height));
   1.277 +	print_report(w->frame_number, w->video_size, 1);
   1.278 +
   1.279 +	av_free(w->bit_buffer);
   1.280 +
   1.281 +	{//propagate exit
   1.282 +		pthread_mutex_lock(&h->lock[WRITE]);
   1.283 +		while (h->write_cnt>= MAX_DELAYED_PIC_COUNT)
   1.284 +			pthread_cond_wait(&h->cond[WRITE], &h->lock[WRITE]);
   1.285 +		last_pic.reference = -1;
   1.286 +		h->write_q[h->write_fi] = &last_pic;
   1.287 +		h->write_cnt++;
   1.288 +		h->write_fi++; h->write_fi %= MAX_DELAYED_PIC_COUNT;
   1.289 +		pthread_cond_signal(&h->cond[WRITE]);
   1.290 +		pthread_mutex_unlock(&h->lock[WRITE]);
   1.291 +
   1.292 +	}
   1.293 +	free_cabac(&hcabac);
   1.294 +
   1.295 +	pthread_exit(NULL);
   1.296 +	return NULL;
   1.297 +
   1.298 +}