OSDN Git Service

Removes unused variable (and gets rid of the compile warning for it). Change copied...
[handbrake-jp/handbrake-jp-git.git] / libhb / detelecine.c
1 #include "hb.h"
2 #include "ffmpeg/avcodec.h"
3 #include "mpeg2dec/mpeg2.h"
4
5 /*
6  *
7  * PULLUP DEFINITIONS
8  *
9  */
10
11 #define PULLUP_FMT_Y         1
12 #define PULLUP_HAVE_BREAKS   1
13 #define PULLUP_HAVE_AFFINITY 2
14 #define PULLUP_BREAK_LEFT    1
15 #define PULLUP_BREAK_RIGHT   2
16
17 #define PULLUP_ABS( a ) (((a)^((a)>>31))-((a)>>31))
18
19 #ifndef PIC_FLAG_REPEAT_FIRST_FIELD
20 #define PIC_FLAG_REPEAT_FIRST_FIELD 256
21 #endif
22
23 struct pullup_buffer
24 {
25         int lock[2];
26         unsigned char **planes;
27 };
28
29 struct pullup_field
30 {
31         int parity;
32         struct pullup_buffer *buffer;
33         unsigned int flags;
34         int breaks;
35         int affinity;
36         int *diffs;
37         int *comb;
38         int *var;
39         struct pullup_field *prev, *next;
40 };
41
42 struct pullup_frame
43 {
44         int lock;
45         int length;
46         int parity;
47         struct pullup_buffer **ifields, *ofields[2];
48         struct pullup_buffer *buffer;
49 };
50
51 struct pullup_context
52 {
53         /* Public interface */
54         int format;
55         int nplanes;
56         int *bpp, *w, *h, *stride, *background;
57         unsigned int cpu;
58         int junk_left, junk_right, junk_top, junk_bottom;
59         int verbose;
60         int metric_plane;
61         int strict_breaks;
62         int strict_pairs;
63         /* Internal data */
64         struct pullup_field *first, *last, *head;
65         struct pullup_buffer *buffers;
66         int nbuffers;
67         int (*diff)(unsigned char *, unsigned char *, int);
68         int (*comb)(unsigned char *, unsigned char *, int);
69         int (*var)(unsigned char *, unsigned char *, int);
70         int metric_w, metric_h, metric_len, metric_offset;
71         struct pullup_frame *frame;
72 };
73
74 /*
75  *
76  * DETELECINE FILTER DEFINITIONS
77  *
78  */
79
80 struct hb_filter_private_s 
81 {
82     int              pix_fmt;
83     int              width[3];
84     int              height[3];       
85     
86     struct pullup_context * pullup_ctx;
87     int                     pullup_fakecount;
88     int                     pullup_skipflag;
89     
90     AVPicture        pic_in;
91     AVPicture        pic_out;            
92     hb_buffer_t    * buf_out;
93 };
94
95 hb_filter_private_t * hb_detelecine_init( int pix_fmt, 
96                                           int width, 
97                                           int height,
98                                           char * settings );
99
100 int hb_detelecine_work( const hb_buffer_t * buf_in,
101                         hb_buffer_t ** buf_out,
102                         int pix_fmt,
103                         int width, 
104                         int height,
105                         hb_filter_private_t * pv );
106
107 void hb_detelecine_close( hb_filter_private_t * pv );
108
109 hb_filter_object_t hb_filter_detelecine =
110 {   
111     FILTER_DETELECINE,
112     "Detelecine (pullup)",
113     NULL,
114     hb_detelecine_init,
115     hb_detelecine_work,
116     hb_detelecine_close,
117 };
118
119 /*
120  *
121  * PULLUP STATIC FUNCTIONS
122  *
123  */
124
125 static int pullup_diff_y( unsigned char  *a, unsigned char * b, int s )
126 {
127         int i, j, diff = 0;
128         for( i = 4; i; i-- ) 
129     {
130                 for( j = 0; j < 8; j++ ) 
131         {
132             diff += PULLUP_ABS( a[j]-b[j] );
133         }
134                 a+=s; b+=s;
135         }
136         return diff;
137 }
138
139 static int pullup_licomb_y( unsigned char * a, unsigned char * b, int s )
140 {
141         int i, j, diff = 0;
142         for( i = 4; i; i-- ) 
143     {
144                 for( j = 0; j < 8; j++ )
145         {
146                         diff += PULLUP_ABS( (a[j]<<1) - b[j-s] - b[j] )
147                                   + PULLUP_ABS( (b[j]<<1) - a[j] - a[j+s] );
148         }
149                 a+=s; b+=s;
150         }
151         return diff;
152 }
153
154 static int pullup_var_y( unsigned char * a, unsigned char * b, int s )
155 {
156         int i, j, var = 0;
157         for( i = 3; i; i-- ) 
158     {
159                 for( j = 0; j < 8; j++ ) 
160         {
161                         var += PULLUP_ABS( a[j]-a[j+s] );
162                 }
163                 a+=s; b+=s;
164         }
165         return 4*var;
166 }
167
168 static void pullup_alloc_metrics( struct pullup_context * c, 
169                                   struct pullup_field * f )
170 {
171         f->diffs = calloc( c->metric_len, sizeof(int) );
172         f->comb  = calloc( c->metric_len, sizeof(int) );
173         f->var   = calloc( c->metric_len, sizeof(int) );
174 }
175
176 static void pullup_compute_metric( struct pullup_context * c,
177                                    struct pullup_field * fa, int pa,
178                                    struct pullup_field * fb, int pb,
179                                    int (* func)( unsigned char *, 
180                                                  unsigned char *, int), 
181                                    int * dest )
182 {
183         unsigned char *a, *b;
184         int x, y;
185         int mp    = c->metric_plane;
186         int xstep = c->bpp[mp];
187         int ystep = c->stride[mp]<<3;
188         int s     = c->stride[mp]<<1; /* field stride */
189         int w     = c->metric_w*xstep;
190     
191         if( !fa->buffer || !fb->buffer ) return;
192     
193         /* Shortcut for duplicate fields (e.g. from RFF flag) */
194         if( fa->buffer == fb->buffer && pa == pb ) 
195     {
196                 memset( dest, 0, c->metric_len * sizeof(int) );
197                 return;
198         }
199     
200         a = fa->buffer->planes[mp] + pa * c->stride[mp] + c->metric_offset;
201         b = fb->buffer->planes[mp] + pb * c->stride[mp] + c->metric_offset;
202     
203         for( y = c->metric_h; y; y-- ) 
204     {
205                 for( x = 0; x < w; x += xstep ) 
206         {
207                         *dest++ = func( a + x, b + x, s );
208                 }
209                 a += ystep; b += ystep;
210         }
211 }
212
213 static struct pullup_field * pullup_make_field_queue( struct pullup_context * c, 
214                                                       int len )
215 {
216         struct pullup_field * head, * f;
217         f = head = calloc( 1, sizeof(struct pullup_field) );
218         pullup_alloc_metrics( c, f );
219         for ( ; len > 0; len-- ) 
220     {
221                 f->next = calloc( 1, sizeof(struct pullup_field) );
222                 f->next->prev = f;
223                 f = f->next;
224                 pullup_alloc_metrics( c, f );
225         }
226         f->next = head;
227         head->prev = f;
228         return head;
229 }
230
231 static void pullup_check_field_queue( struct pullup_context * c )
232 {
233         if( c->head->next == c->first ) 
234     {
235                 struct pullup_field *f = calloc( 1, sizeof(struct pullup_field) );
236                 pullup_alloc_metrics( c, f );
237                 f->prev = c->head;
238                 f->next = c->first;
239                 c->head->next = f;
240                 c->first->prev = f;
241         }
242 }
243
244 static void pullup_copy_field( struct pullup_context * c, 
245                                struct pullup_buffer * dest,
246                                struct pullup_buffer * src, 
247                                int parity )
248 {
249         int i, j;
250         unsigned char *d, *s;
251         for( i = 0; i < c->nplanes; i++ ) 
252     {
253                 s = src->planes[i] + parity*c->stride[i];
254                 d = dest->planes[i] + parity*c->stride[i];
255                 for( j = c->h[i]>>1; j; j-- ) 
256         {
257                         memcpy( d, s, c->stride[i] );
258                         s += c->stride[i]<<1;
259                         d += c->stride[i]<<1;
260                 }
261         }
262 }
263
264
265 static int pullup_queue_length( struct pullup_field * begin, 
266                                 struct pullup_field * end )
267 {
268         int count = 1;
269         struct pullup_field * f;
270         
271         if( !begin || !end ) return 0;
272         for( f = begin; f != end; f = f->next ) count++;
273         return count;
274 }
275
276 static int pullup_find_first_break( struct pullup_field * f, int max )
277 {
278         int i;
279         for( i = 0; i < max; i++ ) 
280     {
281                 if( f->breaks & PULLUP_BREAK_RIGHT || 
282             f->next->breaks & PULLUP_BREAK_LEFT )
283         {
284                         return i+1;
285         }
286                 f = f->next;
287         }
288         return 0;
289 }
290
291 static void pullup_compute_breaks( struct pullup_context * c, 
292                                    struct pullup_field * f0 )
293 {
294         int i;
295         struct pullup_field *f1 = f0->next;
296         struct pullup_field *f2 = f1->next;
297         struct pullup_field *f3 = f2->next;
298         int l, max_l=0, max_r=0;
299     
300         if( f0->flags & PULLUP_HAVE_BREAKS ) return;
301         f0->flags |= PULLUP_HAVE_BREAKS;
302     
303         /* Special case when fields are 100% identical */
304         if( f0->buffer == f2->buffer && f1->buffer != f3->buffer ) 
305     {
306                 f2->breaks |= PULLUP_BREAK_RIGHT;
307                 return;
308         }
309         if( f0->buffer != f2->buffer && f1->buffer == f3->buffer ) 
310     {
311                 f1->breaks |= PULLUP_BREAK_LEFT;
312                 return;
313         }
314     
315         for( i = 0; i < c->metric_len; i++ ) 
316     {
317                 l = f2->diffs[i] - f3->diffs[i];
318                 if(  l > max_l) max_l = l;
319                 if( -l > max_r) max_r = -l;
320         }
321
322         /* Don't get tripped up when differences are mostly quant error */
323         if( max_l + max_r < 128 ) return;
324         if( max_l > 4*max_r ) f1->breaks |= PULLUP_BREAK_LEFT;
325         if( max_r > 4*max_l ) f2->breaks |= PULLUP_BREAK_RIGHT;
326 }
327
328 static void pullup_compute_affinity( struct pullup_context * c, 
329                                      struct pullup_field * f )
330 {
331         int i;
332         int max_l = 0, max_r = 0, l;
333         
334     if( f->flags & PULLUP_HAVE_AFFINITY ) 
335     {
336         return;
337     }
338         f->flags |= PULLUP_HAVE_AFFINITY;
339     
340         if( f->buffer == f->next->next->buffer ) 
341     {
342                 f->affinity             =  1;
343                 f->next->affinity       =  0;
344                 f->next->next->affinity = -1;
345         
346                 f->next->flags       |= PULLUP_HAVE_AFFINITY;
347                 f->next->next->flags |= PULLUP_HAVE_AFFINITY;
348         
349                 return;
350         }
351     
352     for( i = 0; i < c->metric_len; i++ ) 
353     {
354         int lv = f->prev->var[i];
355         int rv = f->next->var[i];
356         int v  = f->var[i];
357         int lc = f->comb[i] - (v+lv) + PULLUP_ABS( v-lv );
358         int rc = f->next->comb[i] - (v+rv) + PULLUP_ABS( v-rv );
359         
360         lc = (lc > 0) ? lc : 0;
361         rc = (rc > 0) ? rc : 0;
362         l = lc - rc;
363         if(  l > max_l ) max_l = l;
364         if( -l > max_r ) max_r = -l;
365     }
366     
367     if( max_l + max_r < 64 ) 
368     {
369         return;
370     }
371     
372     if( max_r > 6*max_l ) 
373     {
374         f->affinity = -1;
375     }
376     else if( max_l > 6*max_r ) 
377     {
378         f->affinity = 1;
379     }
380 }
381
382 static void pullup_foo( struct pullup_context * c )
383 {
384         struct pullup_field * f = c->first;
385         int i, n = pullup_queue_length (f, c->last );
386         for( i = 0; i < n-1; i++ ) 
387     {
388                 if( i < n-3 ) pullup_compute_breaks( c, f );
389                 pullup_compute_affinity( c, f );
390                 f = f->next;
391         }
392 }
393
394 static int pullup_decide_frame_length( struct pullup_context * c )
395 {
396         struct pullup_field *f0 = c->first;
397         struct pullup_field *f1 = f0->next;
398         struct pullup_field *f2 = f1->next;
399         int l;
400         
401         if( pullup_queue_length( c->first, c->last ) < 4 ) 
402     {
403         return 0;
404     }
405         pullup_foo( c );
406     
407         if( f0->affinity == -1 ) return 1;
408     
409         l = pullup_find_first_break( f0, 3 );
410         if( l == 1 && c->strict_breaks < 0 ) l = 0;
411         
412         switch (l) 
413     {
414         case 1:
415             if ( c->strict_breaks < 1 && 
416                  f0->affinity == 1 && 
417                  f1->affinity == -1 )
418             {
419                 return 2;
420             }
421             else
422             {
423                 return 1;
424             }
425             
426         case 2:
427             /* FIXME: strictly speaking, f0->prev is no longer valid... :) */
428             if( c->strict_pairs &&
429                 (f0->prev->breaks & PULLUP_BREAK_RIGHT) && 
430                 (f2->breaks & PULLUP_BREAK_LEFT) &&
431                 (f0->affinity != 1 || f1->affinity != -1) )
432             {
433                 return 1;
434             }
435             if( f1->affinity == 1 ) 
436             {
437                 return 1;
438             }
439             else
440             {
441                 return 2;
442             }
443
444         case 3:
445             if( f2->affinity == 1 ) 
446             {
447                 return 2;
448             }
449             else
450             {
451                 return 3;
452             }
453             
454         default:
455             /* 9 possibilities covered before switch */
456             if( f1->affinity == 1 ) 
457             {
458                 return 1; /* covers 6 */
459             }
460             else if( f1->affinity == -1 ) 
461             {
462                 return 2; /* covers 6 */
463             }
464             else if( f2->affinity == -1 ) 
465             { 
466                 /* covers 2 */
467                 if( f0->affinity == 1 ) 
468                 {
469                     return 3;
470                 }
471                 else
472                 {
473                     return 1;
474                 }
475             }
476             else
477             {
478                 return 2; /* the remaining 6 */
479             }
480         }
481 }
482
483 static void pullup_print_aff_and_breaks(struct pullup_context * c, 
484                                         struct pullup_field * f )
485 {
486         int i;
487         struct pullup_field * f0 = f;
488         const char aff_l[] = "+..", aff_r[] = "..+";
489         printf( "\naffinity: " );
490         for( i = 0; i < 4; i++ ) 
491     {
492                 printf( "%c%d%c", 
493                 aff_l[1+f->affinity], 
494                 i, 
495                 aff_r[1+f->affinity] );
496         
497                 f = f->next;
498         }
499         f = f0;
500         printf("\nbreaks:   ");
501         for( i = 0; i < 4; i++ ) 
502     {
503                 printf( "%c%d%c", 
504                 f->breaks & PULLUP_BREAK_LEFT  ? '|' : '.', 
505                 i, 
506                 f->breaks & PULLUP_BREAK_RIGHT ? '|' : '.' );
507         
508                 f = f->next;
509         }
510         printf("\n");
511 }
512
513 /*
514  *
515  * PULLUP CONTEXT FUNCTIONS
516  *
517  */
518
519 struct pullup_context * pullup_alloc_context( void )
520 {
521         struct pullup_context * c;
522     
523         c = calloc( 1, sizeof(struct pullup_context)) ;
524     
525         return c;
526 }
527
528 void pullup_preinit_context( struct pullup_context * c )
529 {
530         c->bpp        = calloc( c->nplanes, sizeof(int) );
531         c->w          = calloc( c->nplanes, sizeof(int) );
532         c->h          = calloc( c->nplanes, sizeof(int) );
533         c->stride     = calloc( c->nplanes, sizeof(int) );
534         c->background = calloc( c->nplanes, sizeof(int) );
535 }
536
537 void pullup_init_context( struct pullup_context * c )
538 {
539         int mp = c->metric_plane;
540         if ( c->nbuffers < 10 )
541     {
542         c->nbuffers = 10;
543     }
544         c->buffers = calloc( c->nbuffers, sizeof (struct pullup_buffer) );
545     
546         c->metric_w      = (c->w[mp] - ((c->junk_left + c->junk_right) << 3)) >> 3;
547         c->metric_h      = (c->h[mp] - ((c->junk_top + c->junk_bottom) << 1)) >> 3;
548         c->metric_offset = c->junk_left*c->bpp[mp] + (c->junk_top<<1)*c->stride[mp];
549         c->metric_len    = c->metric_w * c->metric_h;
550         
551         c->head = pullup_make_field_queue( c, 8 );
552     
553         c->frame = calloc( 1, sizeof (struct pullup_frame) );
554         c->frame->ifields = calloc( 3, sizeof (struct pullup_buffer *) );
555     
556         if( c->format == PULLUP_FMT_Y ) 
557     {
558         c->diff = pullup_diff_y;
559         c->comb = pullup_licomb_y;
560         c->var  = pullup_var_y;
561         }
562 }
563
564 void pullup_free_context( struct pullup_context * c )
565 {
566         struct pullup_field * f;
567         
568     free( c->buffers );
569
570         f = c->head;
571         do 
572     {
573                 free( f->diffs );
574                 free( f->comb );
575                 f = f->next;
576                 free( f->prev );
577         } 
578     while( f != c->head );
579         
580     free( c->frame );
581         free( c );
582 }
583
584 /*
585  *
586  * PULLUP BUFFER FUNCTIONS
587  *
588  */
589
590 static void pullup_alloc_buffer( struct pullup_context * c, 
591                                  struct pullup_buffer * b )
592 {
593         int i;
594         if( b->planes ) return;
595         b->planes = calloc( c->nplanes, sizeof(unsigned char *) );
596         for ( i = 0; i < c->nplanes; i++ ) 
597     {
598                 b->planes[i] = malloc(c->h[i]*c->stride[i]);
599                 /* Deal with idiotic 128=0 for chroma: */
600                 memset( b->planes[i], c->background[i], c->h[i]*c->stride[i] );
601         }
602 }
603
604 struct pullup_buffer * pullup_lock_buffer( struct pullup_buffer * b, 
605                                            int parity )
606 {
607         if( !b ) return 0;
608         if( (parity+1) & 1 ) b->lock[0]++;
609         if( (parity+1) & 2 ) b->lock[1]++;
610
611         return b;
612 }
613
614 void pullup_release_buffer( struct pullup_buffer * b, 
615                             int parity )
616 {
617         if( !b ) return;
618         if( (parity+1) & 1 ) b->lock[0]--;
619         if( (parity+1) & 2 ) b->lock[1]--;
620 }
621
622 struct pullup_buffer * pullup_get_buffer( struct pullup_context * c, 
623                                           int parity )
624 {
625         int i;
626     
627         /* Try first to get the sister buffer for the previous field */
628         if( parity < 2 && 
629         c->last && 
630         parity != c->last->parity &&
631             !c->last->buffer->lock[parity]) 
632     {
633                 pullup_alloc_buffer( c, c->last->buffer );
634                 return pullup_lock_buffer( c->last->buffer, parity );
635         }
636         
637         /* Prefer a buffer with both fields open */
638         for( i = 0; i < c->nbuffers; i++ ) 
639     {
640                 if( c->buffers[i].lock[0] ) continue;
641                 if( c->buffers[i].lock[1] ) continue;
642                 pullup_alloc_buffer( c, &c->buffers[i] );
643                 return pullup_lock_buffer( &c->buffers[i], parity );
644         }
645     
646         if( parity == 2 ) return 0;
647         
648         /* Search for any half-free buffer */
649         for( i = 0; i < c->nbuffers; i++ ) 
650     {
651                 if( ((parity+1) & 1) && c->buffers[i].lock[0] ) continue;
652                 if( ((parity+1) & 2) && c->buffers[i].lock[1] ) continue;
653                 pullup_alloc_buffer( c, &c->buffers[i] );
654                 return pullup_lock_buffer( &c->buffers[i], parity );
655         }
656         
657         return 0;
658 }
659
660 /*
661  *
662  * PULLUP FRAME FUNCTIONS
663  *
664  */
665
666 struct pullup_frame * pullup_get_frame( struct pullup_context * c )
667 {
668         int i;
669         struct pullup_frame * fr = c->frame;
670         int n = pullup_decide_frame_length( c );
671         int aff = c->first->next->affinity;
672     
673         if ( !n ) return 0;
674         if ( fr->lock ) return 0;
675     
676         if ( c->verbose ) 
677     {
678                 pullup_print_aff_and_breaks(c, c->first);
679                 printf("duration: %d    \n", n);
680         }
681     
682         fr->lock++;
683         fr->length = n;
684         fr->parity = c->first->parity;
685         fr->buffer = 0;
686         for( i = 0; i < n; i++ ) 
687     {
688                 /* We cheat and steal the buffer without release+relock */
689                 fr->ifields[i] = c->first->buffer;
690                 c->first->buffer = 0;
691                 c->first = c->first->next;
692         }
693         
694         if( n == 1 ) 
695     {
696                 fr->ofields[fr->parity] = fr->ifields[0];
697                 fr->ofields[fr->parity^1] = 0;
698         } 
699     else if( n == 2 ) 
700     {
701                 fr->ofields[fr->parity] = fr->ifields[0];
702                 fr->ofields[fr->parity^1] = fr->ifields[1];
703         } 
704     else if( n == 3 ) 
705     {
706                 if( aff == 0 )
707         {
708                         aff = (fr->ifields[0] == fr->ifields[1]) ? -1 : 1;
709         }
710                 fr->ofields[fr->parity]   = fr->ifields[1+aff];
711                 fr->ofields[fr->parity^1] = fr->ifields[1];
712         }
713         pullup_lock_buffer( fr->ofields[0], 0 );
714         pullup_lock_buffer( fr->ofields[1], 1 );
715         
716         if( fr->ofields[0] == fr->ofields[1] ) 
717     {
718                 fr->buffer = fr->ofields[0];
719                 pullup_lock_buffer(fr->buffer, 2);
720                 return fr;
721         }
722         return fr;
723 }
724
725 void pullup_pack_frame( struct pullup_context * c, struct pullup_frame * fr)
726 {
727         int i;
728         if (fr->buffer) return;
729         if (fr->length < 2) return; /* FIXME: deal with this */
730         for( i = 0; i < 2; i++ )
731         {
732                 if( fr->ofields[i]->lock[i^1] ) continue;
733                 fr->buffer = fr->ofields[i];
734                 pullup_lock_buffer(fr->buffer, 2);
735                 pullup_copy_field( c, fr->buffer, fr->ofields[i^1], i^1 );
736                 return;
737         }
738         fr->buffer = pullup_get_buffer( c, 2 );
739         pullup_copy_field( c, fr->buffer, fr->ofields[0], 0 );
740         pullup_copy_field( c, fr->buffer, fr->ofields[1], 1 );
741 }
742
743 void pullup_release_frame( struct pullup_frame * fr )
744 {
745         int i;
746         for( i = 0; i < fr->length; i++ )
747     {
748                 pullup_release_buffer( fr->ifields[i], fr->parity ^ (i&1) );
749     }
750         pullup_release_buffer( fr->ofields[0], 0 );
751         pullup_release_buffer( fr->ofields[1], 1 );
752         if (fr->buffer) pullup_release_buffer( fr->buffer, 2 );
753         fr->lock--;
754 }
755
756 /*
757  *
758  * PULLUP FIELD FUNCTIONS
759  *
760  */
761
762 void pullup_submit_field( struct pullup_context * c, 
763                           struct pullup_buffer * b, 
764                           int parity )
765 {
766         struct pullup_field * f;
767         
768         /* Grow the circular list if needed */
769         pullup_check_field_queue( c );
770         
771         /* Cannot have two fields of same parity in a row; drop the new one */
772         if( c->last && c->last->parity == parity ) return;
773     
774         f = c->head;
775         f->parity = parity;
776         f->buffer = pullup_lock_buffer( b, parity );
777         f->flags = 0;
778         f->breaks = 0;
779         f->affinity = 0;
780     
781         pullup_compute_metric( c, f, parity, f->prev->prev, 
782                            parity, c->diff, f->diffs );
783         pullup_compute_metric( c, parity?f->prev:f, 0, 
784                            parity?f:f->prev, 1, c->comb, f->comb );
785         pullup_compute_metric( c, f, parity, f, 
786                            -1, c->var, f->var );
787     
788         /* Advance the circular list */
789         if( !c->first ) c->first = c->head;
790         c->last = c->head;
791         c->head = c->head->next;
792 }
793
794 void pullup_flush_fields( struct pullup_context * c )
795 {
796         struct pullup_field * f;
797         
798         for( f = c->first; f && f != c->head; f = f->next ) 
799     {
800                 pullup_release_buffer( f->buffer, f->parity );
801                 f->buffer = 0;
802         }
803         c->first = c->last = 0;
804 }
805
806 /*
807  *
808  * DETELECINE FILTER FUNCTIONS
809  *
810  */
811
812 hb_filter_private_t * hb_detelecine_init( int pix_fmt, 
813                                           int width, 
814                                           int height,
815                                           char * settings )
816 {
817     if( pix_fmt != PIX_FMT_YUV420P )
818     {
819         return 0;
820     }
821     
822     hb_filter_private_t * pv = malloc( sizeof(struct hb_filter_private_s) );
823     
824     pv->pix_fmt  = pix_fmt;    
825     pv->width[0]  = width;
826     pv->height[0] = height;    
827     pv->width[1]  = pv->width[2] = width >> 1;
828     pv->height[1] = pv->height[2] = height >> 1;    
829
830     int buf_size = 3 * width * height / 2;    
831     pv->buf_out = hb_buffer_init( buf_size );
832     
833     struct pullup_context * ctx;
834     pv->pullup_ctx = ctx = pullup_alloc_context();
835     
836     ctx->junk_left = ctx->junk_right  = 1;
837     ctx->junk_top  = ctx->junk_bottom = 4;    
838     ctx->strict_breaks = 0;
839     ctx->metric_plane  = 0;
840
841     if( settings ) 
842     {
843                 sscanf( settings, "%d:%d:%d:%d:%d:%d", 
844                 &ctx->junk_left, 
845                 &ctx->junk_right, 
846                 &ctx->junk_top, 
847                 &ctx->junk_bottom, 
848                 &ctx->strict_breaks, 
849                 &ctx->metric_plane );
850         }
851     
852     ctx->format = PULLUP_FMT_Y;
853     ctx->nplanes = 4;
854     
855     pullup_preinit_context( ctx );
856     
857     ctx->bpp[0] = ctx->bpp[1] = ctx->bpp[2] = 8;
858     ctx->background[1] = ctx->background[2] = 128;
859
860     ctx->w[0]      = pv->width[0];
861     ctx->h[0]      = pv->height[0];
862     ctx->stride[0] = pv->width[0];
863
864     ctx->w[1]      = pv->width[1];
865     ctx->h[1]      = pv->height[1];
866     ctx->stride[1] = pv->width[1];
867
868     ctx->w[2]      = pv->width[2];
869     ctx->h[2]      = pv->height[2];
870     ctx->stride[2] = pv->width[2];    
871     
872     ctx->w[3]      = ((width+15)/16) * ((height+15)/16);
873     ctx->h[3]      = 2;
874     ctx->stride[3] = ctx->w[3];    
875     
876 #if 0
877     ctx->verbose = 1;
878 #endif
879     
880     pullup_init_context( ctx );
881
882     pv->pullup_fakecount = 1;
883     pv->pullup_skipflag = 0;
884     
885     return pv;
886 }
887
888 void hb_detelecine_close( hb_filter_private_t * pv )
889 {
890     if( !pv )
891     {
892         return;
893     }
894     
895     if( pv->buf_out )
896     {
897         hb_buffer_close( &pv->buf_out );
898     }
899     
900     if( pv->pullup_ctx )
901     {
902         pullup_free_context( pv->pullup_ctx );
903     }
904     
905     free( pv );
906 }
907
908 int hb_detelecine_work( const hb_buffer_t * buf_in,
909                         hb_buffer_t ** buf_out,
910                         int pix_fmt,
911                         int width, 
912                         int height,
913                         hb_filter_private_t * pv )
914 {
915     if( !pv || 
916         pix_fmt != pv->pix_fmt ||
917         width != pv->width[0] ||
918         height != pv->height[0] )
919     {
920         return FILTER_FAILED;
921     }        
922    
923         struct pullup_context * ctx = pv->pullup_ctx;
924         struct pullup_buffer  * buf;
925         struct pullup_frame   * frame;
926     
927     buf = pullup_get_buffer( ctx, 2 );
928     if( !buf )
929     {
930         frame = pullup_get_frame( ctx );
931         pullup_release_frame( frame );        
932         hb_log( "Could not get buffer from pullup!" );
933         return FILTER_FAILED;
934     }
935     
936     /* Copy input buffer into pullup buffer */    
937     avpicture_fill( &pv->pic_in, buf_in->data, 
938                     pix_fmt, width, height );
939     
940     hb_buffer_copy_settings( pv->buf_out, buf_in );
941     
942     memcpy( buf->planes[0], pv->pic_in.data[0], 
943             pv->width[0] * pv->height[0] * sizeof(uint8_t) );
944     memcpy( buf->planes[1], pv->pic_in.data[1], 
945             pv->width[1] * pv->height[1] * sizeof(uint8_t) );
946     memcpy( buf->planes[2], pv->pic_in.data[2], 
947             pv->width[2] * pv->height[2] * sizeof(uint8_t) );
948     
949     /* Submit buffer fields based on buffer flags */
950     int parity = 1;
951     if( buf_in->flags & PIC_FLAG_TOP_FIELD_FIRST )
952     {
953         parity = 0;
954     }
955         pullup_submit_field( ctx, buf, parity );
956         pullup_submit_field( ctx, buf, parity^1 );
957     if( buf_in->flags & PIC_FLAG_REPEAT_FIRST_FIELD )
958     {
959         pullup_submit_field( ctx, buf, parity );        
960     }    
961         pullup_release_buffer( buf, 2 );    
962     
963     /* Get frame and check if pullup is ready */
964         frame = pullup_get_frame( ctx );    
965     if( !frame )
966     {
967         if( pv->pullup_fakecount )
968         {
969             pv->pullup_fakecount--;
970             
971             memcpy( pv->buf_out->data, buf_in->data, buf_in->size );                  
972
973             goto output_frame;
974         }
975         else
976         {
977             goto discard_frame;
978         }
979     }
980     
981     /* Check to see if frame should be dropped */
982     if( frame->length < 2 )
983     {
984                 pullup_release_frame( frame );
985                 frame = pullup_get_frame( ctx );
986         
987                 if (!frame) 
988         {
989             goto discard_frame;
990         }
991                 if( frame->length < 2 ) 
992         {
993                         pullup_release_frame( frame );
994
995                         if( !(buf_in->flags & PIC_FLAG_REPEAT_FIRST_FIELD) )
996             {
997                 goto discard_frame;
998             }
999                         
1000             frame = pullup_get_frame( ctx );
1001                         
1002             if( !frame ) 
1003             {
1004                 goto discard_frame;
1005             }
1006                         if( frame->length < 2 ) 
1007             {
1008                                 pullup_release_frame( frame );
1009                 goto discard_frame;
1010                         }
1011                 }
1012     }
1013     
1014     /* Check to see if frame buffer is ready for export */
1015     if( !frame->buffer )
1016     {
1017         pullup_pack_frame( ctx, frame );
1018     }
1019     
1020     /* Copy pullup frame buffer into output buffer */
1021     avpicture_fill( &pv->pic_out, pv->buf_out->data, 
1022                     pix_fmt, width, height ); 
1023     
1024     memcpy( pv->pic_out.data[0], frame->buffer->planes[0],
1025             pv->width[0] * pv->height[0] * sizeof(uint8_t) );
1026     memcpy( pv->pic_out.data[1], frame->buffer->planes[1], 
1027             pv->width[1] * pv->height[1] * sizeof(uint8_t) );
1028     memcpy( pv->pic_out.data[2], frame->buffer->planes[2],  
1029             pv->width[2] * pv->height[2] * sizeof(uint8_t) );
1030     
1031     pullup_release_frame( frame );    
1032
1033 output_frame:    
1034     *buf_out = pv->buf_out;    
1035     return FILTER_OK;
1036
1037 /* This and all discard_frame calls shown above are
1038    the result of me restoring the functionality in
1039    pullup that huevos_rancheros disabled because
1040    HB couldn't handle it.                           */
1041 discard_frame:    
1042     *buf_out = pv->buf_out;
1043     return FILTER_DROP;
1044
1045 }
1046
1047