2 Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "libavcodec/avcodec.h"
21 #include "mpeg2dec/mpeg2.h"
23 #define SUPPRESS_AV_LOG
25 #define YADIF_MODE_DEFAULT -1
26 #define YADIF_PARITY_DEFAULT -1
28 #define MCDEINT_MODE_DEFAULT -1
29 #define MCDEINT_QP_DEFAULT 1
31 #define ABS(a) ((a) > 0 ? (a) : (-(a)))
32 #define MIN3(a,b,c) MIN(MIN(a,b),c)
33 #define MAX3(a,b,c) MAX(MAX(a,b),c)
35 typedef struct yadif_arguments_s {
42 struct hb_filter_private_s
52 uint8_t * yadif_ref[4][3];
53 int yadif_ref_stride[3];
57 hb_thread_t ** yadif_threads; // Threads for Yadif - one per CPU
58 hb_lock_t ** yadif_begin_lock; // Thread has work
59 hb_lock_t ** yadif_complete_lock; // Thread has completed work
60 yadif_arguments_t *yadif_arguments; // Arguments to thread for work
65 int mcdeint_outbuf_size;
66 uint8_t * mcdeint_outbuf;
67 AVCodecContext * mcdeint_avctx_enc;
68 AVFrame * mcdeint_frame;
69 AVFrame * mcdeint_frame_dec;
73 hb_buffer_t * buf_out[2];
74 hb_buffer_t * buf_settings;
77 hb_filter_private_t * hb_deinterlace_init( int pix_fmt,
82 int hb_deinterlace_work( hb_buffer_t * buf_in,
83 hb_buffer_t ** buf_out,
87 hb_filter_private_t * pv );
89 void hb_deinterlace_close( hb_filter_private_t * pv );
91 hb_filter_object_t hb_filter_deinterlace =
94 "Deinterlace (ffmpeg or yadif/mcdeint)",
102 static void yadif_store_ref( const uint8_t ** pic,
103 hb_filter_private_t * pv )
105 memcpy( pv->yadif_ref[3],
107 sizeof(uint8_t *)*3 );
109 memmove( pv->yadif_ref[0],
111 sizeof(uint8_t *)*3*3 );
114 for( i = 0; i < 3; i++ )
116 const uint8_t * src = pic[i];
117 uint8_t * ref = pv->yadif_ref[2][i];
119 int w = pv->width[i];
120 int ref_stride = pv->yadif_ref_stride[i];
123 for( y = 0; y < pv->height[i]; y++ )
126 src = (uint8_t*)src + w;
127 ref = (uint8_t*)ref + ref_stride;
132 static void yadif_filter_line( uint8_t *dst,
138 hb_filter_private_t * pv )
140 uint8_t *prev2 = parity ? prev : cur ;
141 uint8_t *next2 = parity ? cur : next;
143 int w = pv->width[plane];
144 int refs = pv->yadif_ref_stride[plane];
147 for( x = 0; x < w; x++)
150 int d = (prev2[0] + next2[0])>>1;
152 int temporal_diff0 = ABS(prev2[0] - next2[0]);
153 int temporal_diff1 = ( ABS(prev[-refs] - c) + ABS(prev[+refs] - e) ) >> 1;
154 int temporal_diff2 = ( ABS(next[-refs] - c) + ABS(next[+refs] - e) ) >> 1;
155 int diff = MAX3(temporal_diff0>>1, temporal_diff1, temporal_diff2);
156 int spatial_pred = (c+e)>>1;
157 int spatial_score = ABS(cur[-refs-1] - cur[+refs-1]) + ABS(c-e) +
158 ABS(cur[-refs+1] - cur[+refs+1]) - 1;
160 #define YADIF_CHECK(j)\
161 { int score = ABS(cur[-refs-1+j] - cur[+refs-1-j])\
162 + ABS(cur[-refs +j] - cur[+refs -j])\
163 + ABS(cur[-refs+1+j] - cur[+refs+1-j]);\
164 if( score < spatial_score ){\
165 spatial_score = score;\
166 spatial_pred = (cur[-refs +j] + cur[+refs -j])>>1;\
168 YADIF_CHECK(-1) YADIF_CHECK(-2) }} }}
169 YADIF_CHECK( 1) YADIF_CHECK( 2) }} }}
171 if( pv->yadif_mode < 2 )
173 int b = (prev2[-2*refs] + next2[-2*refs])>>1;
174 int f = (prev2[+2*refs] + next2[+2*refs])>>1;
176 int max = MAX3(d-e, d-c, MIN(b-c, f-e));
177 int min = MIN3(d-e, d-c, MAX(b-c, f-e));
179 diff = MAX3( diff, min, -max );
182 if( spatial_pred > d + diff )
184 spatial_pred = d + diff;
186 else if( spatial_pred < d - diff )
188 spatial_pred = d - diff;
191 dst[0] = spatial_pred;
202 typedef struct yadif_thread_arg_s {
203 hb_filter_private_t *pv;
205 } yadif_thread_arg_t;
208 * deinterlace this segment of all three planes in a single thread.
210 void yadif_filter_thread( void *thread_args_v )
212 yadif_arguments_t *yadif_work = NULL;
213 hb_filter_private_t * pv;
216 int segment, segment_start, segment_stop;
217 yadif_thread_arg_t *thread_args = thread_args_v;
219 int parity, tff, y, w, h, ref_stride;
222 pv = thread_args->pv;
223 segment = thread_args->segment;
225 hb_log("Yadif Deinterlace thread started for segment %d", segment);
230 * Wait here until there is work to do. hb_lock() blocks until
231 * render releases it to say that there is more work to do.
233 hb_lock( pv->yadif_begin_lock[segment] );
235 yadif_work = &pv->yadif_arguments[segment];
237 if( yadif_work->stop )
240 * No more work to do, exit this thread.
246 if( yadif_work->dst == NULL )
248 hb_error( "Thread started when no work available" );
254 * Process all three planes, but only this segment of it.
256 for( plane = 0; plane < 3; plane++)
259 dst = yadif_work->dst;
260 parity = yadif_work->parity;
261 tff = yadif_work->tff;
262 w = pv->width[plane];
263 h = pv->height[plane];
264 ref_stride = pv->yadif_ref_stride[plane];
265 segment_start = ( h / pv->cpu_count ) * segment;
266 if( segment == pv->cpu_count - 1 )
273 segment_stop = ( h / pv->cpu_count ) * ( segment + 1 );
276 for( y = segment_start; y < segment_stop; y++ )
278 if( (y ^ parity) & 1 )
280 uint8_t *prev = &pv->yadif_ref[0][plane][y*ref_stride];
281 uint8_t *cur = &pv->yadif_ref[1][plane][y*ref_stride];
282 uint8_t *next = &pv->yadif_ref[2][plane][y*ref_stride];
283 uint8_t *dst2 = &dst[plane][y*w];
285 yadif_filter_line( dst2,
296 memcpy( &dst[plane][y*w],
297 &pv->yadif_ref[1][plane][y*ref_stride],
298 w * sizeof(uint8_t) );
303 * Finished this segment, let everyone know.
305 hb_unlock( pv->yadif_complete_lock[segment] );
307 free( thread_args_v );
312 * threaded yadif - each thread deinterlaces a single segment of all
313 * three planes. Where a segment is defined as the frame divided by
314 * the number of CPUs.
316 * This function blocks until the frame is deinterlaced.
318 static void yadif_filter( uint8_t ** dst,
321 hb_filter_private_t * pv )
326 for( segment = 0; segment < pv->cpu_count; segment++ )
329 * Setup the work for this plane.
331 pv->yadif_arguments[segment].parity = parity;
332 pv->yadif_arguments[segment].tff = tff;
333 pv->yadif_arguments[segment].dst = dst;
336 * Let the thread for this plane know that we've setup work
337 * for it by releasing the begin lock (ensuring that the
338 * complete lock is already locked so that we block when
339 * we try to lock it again below).
341 hb_lock( pv->yadif_complete_lock[segment] );
342 hb_unlock( pv->yadif_begin_lock[segment] );
346 * Wait until all three threads have completed by trying to get
347 * the complete lock that we locked earlier for each thread, which
348 * will block until that thread has completed the work on that
351 for( segment = 0; segment < pv->cpu_count; segment++ )
353 hb_lock( pv->yadif_complete_lock[segment] );
354 hb_unlock( pv->yadif_complete_lock[segment] );
358 * Entire frame is now deinterlaced.
362 static void mcdeint_filter( uint8_t ** dst,
365 hb_filter_private_t * pv )
370 #ifdef SUPPRESS_AV_LOG
371 /* TODO: temporarily change log level to suppress obnoxious debug output */
372 int loglevel = av_log_get_level();
373 av_log_set_level( AV_LOG_QUIET );
378 pv->mcdeint_frame->data[i] = src[i];
379 pv->mcdeint_frame->linesize[i] = pv->width[i];
381 pv->mcdeint_avctx_enc->me_cmp = FF_CMP_SAD;
382 pv->mcdeint_avctx_enc->me_sub_cmp = FF_CMP_SAD;
383 pv->mcdeint_frame->quality = pv->mcdeint_qp * FF_QP2LAMBDA;
385 out_size = avcodec_encode_video( pv->mcdeint_avctx_enc,
387 pv->mcdeint_outbuf_size,
390 pv->mcdeint_frame_dec = pv->mcdeint_avctx_enc->coded_frame;
392 for( i = 0; i < 3; i++ )
394 int w = pv->width[i];
395 int h = pv->height[i];
396 int fils = pv->mcdeint_frame_dec->linesize[i];
397 int srcs = pv->width[i];
399 for( y = 0; y < h; y++ )
401 if( (y ^ parity) & 1 )
403 for( x = 0; x < w; x++ )
405 if( (x-2)+(y-1)*w >= 0 && (x+2)+(y+1)*w < w*h )
408 &pv->mcdeint_frame_dec->data[i][x + y*fils];
409 uint8_t * srcp = &src[i][x + y*srcs];
411 int diff0 = filp[-fils] - srcp[-srcs];
412 int diff1 = filp[+fils] - srcp[+srcs];
415 ABS(srcp[-srcs-1] - srcp[+srcs-1])
416 + ABS(srcp[-srcs ] - srcp[+srcs ])
417 + ABS(srcp[-srcs+1] - srcp[+srcs+1]) - 1;
421 #define MCDEINT_CHECK(j)\
422 { int score = ABS(srcp[-srcs-1+j] - srcp[+srcs-1-j])\
423 + ABS(srcp[-srcs +j] - srcp[+srcs -j])\
424 + ABS(srcp[-srcs+1+j] - srcp[+srcs+1-j]);\
425 if( score < spatial_score ) {\
426 spatial_score = score;\
427 diff0 = filp[-fils+j] - srcp[-srcs+j];\
428 diff1 = filp[+fils-j] - srcp[+srcs-j];
430 MCDEINT_CHECK(-1) MCDEINT_CHECK(-2) }} }}
431 MCDEINT_CHECK( 1) MCDEINT_CHECK( 2) }} }}
433 if(diff0 + diff1 > 0)
435 temp -= (diff0 + diff1 -
436 ABS( ABS(diff0) - ABS(diff1) ) / 2) / 2;
440 temp -= (diff0 + diff1 +
441 ABS( ABS(diff0) - ABS(diff1) ) / 2) / 2;
444 filp[0] = dst[i][x + y*w] =
445 temp > 255U ? ~(temp>>31) : temp;
450 pv->mcdeint_frame_dec->data[i][x + y*fils];
456 for( y = 0; y < h; y++ )
458 if( !((y ^ parity) & 1) )
460 for( x = 0; x < w; x++ )
462 pv->mcdeint_frame_dec->data[i][x + y*fils] =
463 dst[i][x + y*w]= src[i][x + y*srcs];
469 #ifdef SUPPRESS_AV_LOG
470 /* TODO: restore previous log level */
471 av_log_set_level(loglevel);
475 hb_filter_private_t * hb_deinterlace_init( int pix_fmt,
480 if( pix_fmt != PIX_FMT_YUV420P )
485 hb_filter_private_t * pv = calloc( 1, sizeof(struct hb_filter_private_s) );
487 pv->pix_fmt = pix_fmt;
489 pv->width[0] = width;
490 pv->height[0] = height;
491 pv->width[1] = pv->width[2] = width >> 1;
492 pv->height[1] = pv->height[2] = height >> 1;
494 int buf_size = 3 * width * height / 2;
495 pv->buf_out[0] = hb_buffer_init( buf_size );
496 pv->buf_out[1] = hb_buffer_init( buf_size );
497 pv->buf_settings = hb_buffer_init( 0 );
500 pv->yadif_mode = YADIF_MODE_DEFAULT;
501 pv->yadif_parity = YADIF_PARITY_DEFAULT;
503 pv->mcdeint_mode = MCDEINT_MODE_DEFAULT;
504 pv->mcdeint_qp = MCDEINT_QP_DEFAULT;
508 sscanf( settings, "%d:%d:%d:%d",
515 pv->cpu_count = hb_get_cpu_count();
517 /* Allocate yadif specific buffers */
518 if( pv->yadif_mode >= 0 )
521 for( i = 0; i < 3; i++ )
524 int w = ((width + 31) & (~31))>>is_chroma;
525 int h = ((height+6+ 31) & (~31))>>is_chroma;
527 pv->yadif_ref_stride[i] = w;
529 for( j = 0; j < 3; j++ )
531 pv->yadif_ref[j][i] = malloc( w*h*sizeof(uint8_t) ) + 3*w;
536 * Create yadif threads and locks.
538 pv->yadif_threads = malloc( sizeof( hb_thread_t* ) * pv->cpu_count );
539 pv->yadif_begin_lock = malloc( sizeof( hb_lock_t * ) * pv->cpu_count );
540 pv->yadif_complete_lock = malloc( sizeof( hb_lock_t * ) * pv->cpu_count );
541 pv->yadif_arguments = malloc( sizeof( yadif_arguments_t ) * pv->cpu_count );
543 for( i = 0; i < pv->cpu_count; i++ )
545 yadif_thread_arg_t *thread_args;
547 thread_args = malloc( sizeof( yadif_thread_arg_t ) );
550 thread_args->pv = pv;
551 thread_args->segment = i;
553 pv->yadif_begin_lock[i] = hb_lock_init();
554 pv->yadif_complete_lock[i] = hb_lock_init();
557 * Important to start off with the threads locked waiting
560 hb_lock( pv->yadif_begin_lock[i] );
562 pv->yadif_arguments[i].stop = 0;
563 pv->yadif_arguments[i].dst = NULL;
565 pv->yadif_threads[i] = hb_thread_init( "yadif_filter_segment",
568 HB_NORMAL_PRIORITY );
570 hb_error( "Yadif could not create threads" );
575 /* Allocate mcdeint specific buffers */
576 if( pv->mcdeint_mode >= 0 )
579 avcodec_register_all();
581 AVCodec * enc = avcodec_find_encoder( CODEC_ID_SNOW );
584 for (i = 0; i < 3; i++ )
586 AVCodecContext * avctx_enc;
588 avctx_enc = pv->mcdeint_avctx_enc = avcodec_alloc_context();
590 avctx_enc->width = width;
591 avctx_enc->height = height;
592 avctx_enc->time_base = (AVRational){1,25}; // meaningless
593 avctx_enc->gop_size = 300;
594 avctx_enc->max_b_frames = 0;
595 avctx_enc->pix_fmt = PIX_FMT_YUV420P;
596 avctx_enc->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_LOW_DELAY;
597 avctx_enc->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
598 avctx_enc->global_quality = 1;
599 avctx_enc->flags2 = CODEC_FLAG2_MEMC_ONLY;
600 avctx_enc->me_cmp = FF_CMP_SAD; //SSE;
601 avctx_enc->me_sub_cmp = FF_CMP_SAD; //SSE;
602 avctx_enc->mb_cmp = FF_CMP_SSE;
604 switch( pv->mcdeint_mode )
609 avctx_enc->me_method = ME_UMH;
611 avctx_enc->flags |= CODEC_FLAG_4MV;
612 avctx_enc->dia_size =2;
614 avctx_enc->flags |= CODEC_FLAG_QPEL;
617 avcodec_open(avctx_enc, enc);
620 pv->mcdeint_frame = avcodec_alloc_frame();
621 pv->mcdeint_outbuf_size = width * height * 10;
622 pv->mcdeint_outbuf = malloc( pv->mcdeint_outbuf_size );
628 void hb_deinterlace_close( hb_filter_private_t * pv )
635 /* Cleanup frame buffers */
638 hb_buffer_close( &pv->buf_out[0] );
642 hb_buffer_close( &pv->buf_out[1] );
644 if (pv->buf_settings )
646 hb_buffer_close( &pv->buf_settings );
649 /* Cleanup yadif specific buffers */
650 if( pv->yadif_mode >= 0 )
653 for( i = 0; i<3*3; i++ )
655 uint8_t **p = &pv->yadif_ref[i%3][i/3];
658 free( *p - 3*pv->yadif_ref_stride[i/3] );
663 for( i = 0; i < pv->cpu_count; i++)
666 * Tell each yadif thread to stop, and then cleanup.
668 pv->yadif_arguments[i].stop = 1;
669 hb_unlock( pv->yadif_begin_lock[i] );
671 hb_thread_close( &pv->yadif_threads[i] );
672 hb_lock_close( &pv->yadif_begin_lock[i] );
673 hb_lock_close( &pv->yadif_complete_lock[i] );
677 * free memory for yadif structs
679 free( pv->yadif_threads );
680 free( pv->yadif_begin_lock );
681 free( pv->yadif_complete_lock );
682 free( pv->yadif_arguments );
685 /* Cleanup mcdeint specific buffers */
686 if( pv->mcdeint_mode >= 0 )
688 if( pv->mcdeint_avctx_enc )
690 avcodec_close( pv->mcdeint_avctx_enc );
691 av_freep( &pv->mcdeint_avctx_enc );
693 if( pv->mcdeint_outbuf )
695 free( pv->mcdeint_outbuf );
702 int hb_deinterlace_work( hb_buffer_t * buf_in,
703 hb_buffer_t ** buf_out,
707 hb_filter_private_t * pv )
710 pix_fmt != pv->pix_fmt ||
711 width != pv->width[0] ||
712 height != pv->height[0] )
714 return FILTER_FAILED;
717 avpicture_fill( &pv->pic_in, buf_in->data,
718 pix_fmt, width, height );
720 /* Use libavcodec deinterlace if yadif_mode < 0 */
721 if( pv->yadif_mode < 0 )
723 avpicture_fill( &pv->pic_out, pv->buf_out[0]->data,
724 pix_fmt, width, height );
726 avpicture_deinterlace( &pv->pic_out, &pv->pic_in,
727 pix_fmt, width, height );
729 hb_buffer_copy_settings( pv->buf_out[0], buf_in );
731 *buf_out = pv->buf_out[0];
736 /* Determine if top-field first layout */
738 if( pv->yadif_parity < 0 )
740 tff = !!(buf_in->flags & PIC_FLAG_TOP_FIELD_FIRST);
744 tff = (pv->yadif_parity & 1) ^ 1;
747 /* Store current frame in yadif cache */
748 yadif_store_ref( (const uint8_t**)pv->pic_in.data, pv );
750 /* If yadif is not ready, store another ref and return FILTER_DELAY */
751 if( pv->yadif_ready == 0 )
753 yadif_store_ref( (const uint8_t**)pv->pic_in.data, pv );
755 hb_buffer_copy_settings( pv->buf_settings, buf_in );
757 /* don't let 'work_loop' send a chapter mark upstream */
758 buf_in->new_chap = 0;
765 /* Perform yadif and mcdeint filtering */
767 for( frame = 0; frame <= (pv->yadif_mode & 1); frame++ )
769 int parity = frame ^ tff ^ 1;
771 avpicture_fill( &pv->pic_out, pv->buf_out[!(frame^1)]->data,
772 pix_fmt, width, height );
774 yadif_filter( pv->pic_out.data, parity, tff, pv );
776 if( pv->mcdeint_mode >= 0 )
778 avpicture_fill( &pv->pic_in, pv->buf_out[(frame^1)]->data,
779 pix_fmt, width, height );
781 mcdeint_filter( pv->pic_in.data, pv->pic_out.data, parity, pv );
783 *buf_out = pv->buf_out[ (frame^1)];
787 *buf_out = pv->buf_out[!(frame^1)];
791 /* Copy buffered settings to output buffer settings */
792 hb_buffer_copy_settings( *buf_out, pv->buf_settings );
794 /* Replace buffered settings with input buffer settings */
795 hb_buffer_copy_settings( pv->buf_settings, buf_in );
797 /* don't let 'work_loop' send a chapter mark upstream */
798 buf_in->new_chap = 0;