| 1 | /* |
| 2 | * Field-order deinterlacer |
| 3 | * |
| 4 | * Written by Paul Gardiner (mythtv@glidos.net), based on overal |
| 5 | * structure of yadif deinterlacer. |
| 6 | */ |
| 7 | #include <stdlib.h> |
| 8 | #include <stdio.h> |
| 9 | |
| 10 | #include "config.h" |
| 11 | #ifdef HAVE_STDINT_H |
| 12 | #include <stdint.h> |
| 13 | #endif |
| 14 | #include <inttypes.h> |
| 15 | |
| 16 | #include <string.h> |
| 17 | #include <math.h> |
| 18 | |
| 19 | #include "filter.h" |
| 20 | #include "frame.h" |
| 21 | |
| 22 | #define NREFS 2 |
| 23 | #define NCHANS 3 |
| 24 | |
| 25 | typedef struct ThisFilter |
| 26 | { |
| 27 | VideoFilter vf; |
| 28 | |
| 29 | long long last_framenr; |
| 30 | |
| 31 | uint8_t *ref[NREFS+1][NCHANS]; |
| 32 | int stride[NCHANS]; |
| 33 | int8_t got_frames[NREFS+1]; |
| 34 | |
| 35 | int width; |
| 36 | int height; |
| 37 | |
| 38 | TF_STRUCT; |
| 39 | } ThisFilter; |
| 40 | |
| 41 | |
| 42 | static void AllocFilter(ThisFilter* filter, int width, int height) |
| 43 | { |
| 44 | int i,j; |
| 45 | if ((width != filter->width) || height != filter->height) |
| 46 | { |
| 47 | for (i=0; i<NCHANS*NREFS; i++) |
| 48 | { |
| 49 | uint8_t **p= &filter->ref[i/NCHANS][i%NCHANS]; |
| 50 | if (*p) free(*p); |
| 51 | *p= NULL; |
| 52 | } |
| 53 | for (i=0; i<NCHANS; i++) |
| 54 | { |
| 55 | int is_chroma= !!i; |
| 56 | int w= ((width + 31) & (~31))>>is_chroma; |
| 57 | int h= ((height + 31) & (~31))>>is_chroma; |
| 58 | |
| 59 | filter->stride[i]= w; |
| 60 | for (j=0; j<NREFS; j++) |
| 61 | { |
| 62 | filter->ref[j][i]= (uint8_t*)calloc(w*h*sizeof(uint8_t),1); |
| 63 | } |
| 64 | } |
| 65 | filter->width = width; |
| 66 | filter->height = height; |
| 67 | memset(filter->got_frames, 0, sizeof(filter->got_frames)); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | static inline void * memcpy_pic(void * dst, const void * src, |
| 72 | int bytesPerLine, int height, |
| 73 | int dstStride, int srcStride) |
| 74 | { |
| 75 | int i; |
| 76 | void *retval=dst; |
| 77 | |
| 78 | if (dstStride == srcStride) |
| 79 | { |
| 80 | if (srcStride < 0) |
| 81 | { |
| 82 | src = (uint8_t*)src + (height-1)*srcStride; |
| 83 | dst = (uint8_t*)dst + (height-1)*dstStride; |
| 84 | srcStride = -srcStride; |
| 85 | } |
| 86 | memcpy(dst, src, srcStride*height); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | for (i=0; i<height; i++) |
| 91 | { |
| 92 | memcpy(dst, src, bytesPerLine); |
| 93 | src = (uint8_t*)src + srcStride; |
| 94 | dst = (uint8_t*)dst + dstStride; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return retval; |
| 99 | } |
| 100 | |
| 101 | static void store_ref(struct ThisFilter *p, uint8_t *src, int src_offsets[3], int src_stride[3], int width, int height) |
| 102 | { |
| 103 | int i; |
| 104 | |
| 105 | memcpy (p->ref[NREFS], p->ref[0], sizeof(uint8_t *)*NCHANS); |
| 106 | memmove(p->ref[0], p->ref[1], sizeof(uint8_t *)*NREFS*NCHANS); |
| 107 | |
| 108 | memcpy (&p->got_frames[NREFS], &p->got_frames[0], sizeof(uint8_t)); |
| 109 | memmove(&p->got_frames[0], &p->got_frames[1], sizeof(uint8_t) * NREFS); |
| 110 | |
| 111 | for (i=0; i<NCHANS; i++) |
| 112 | { |
| 113 | int is_chroma= !!i; |
| 114 | memcpy_pic(p->ref[NREFS-1][i], src + src_offsets[i], width>>is_chroma, height>>is_chroma, p->stride[i], src_stride[i]); |
| 115 | } |
| 116 | p->got_frames[NREFS-1] = 1; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | static void filter_func(struct ThisFilter *p, uint8_t *dst, int dst_offsets[3], int dst_stride[3], int width, int height, int parity, int tff) |
| 122 | { |
| 123 | int y, i; |
| 124 | |
| 125 | uint8_t nr_p, nr_c; |
| 126 | |
| 127 | //check if we already got this frames |
| 128 | nr_c = NREFS-1;//always there after store_ref |
| 129 | nr_p = p->got_frames[NREFS-2]?(NREFS-2):nr_c; |
| 130 | |
| 131 | for (i=0; i<NCHANS; i++) |
| 132 | { |
| 133 | int is_chroma= !!i; |
| 134 | int w= width >>is_chroma; |
| 135 | int h= height>>is_chroma; |
| 136 | int refs= p->stride[i]; |
| 137 | |
| 138 | for (y=0; y<h; y++) |
| 139 | { |
| 140 | if(parity) |
| 141 | { |
| 142 | /* Second call: put whole frame back to previous state when first passed in */ |
| 143 | memcpy(dst + dst_offsets[i] + y*dst_stride[i], &p->ref[nr_c][i][y*refs], w); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | /* First call: replace second field by that of the previous frame */ |
| 148 | if (((y ^ tff) & 1) == 0) |
| 149 | { |
| 150 | memcpy(dst + dst_offsets[i] + y*dst_stride[i], &p->ref[nr_p][i][y*refs], w); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | |
| 158 | static int FieldorderDeint (VideoFilter * f, VideoFrame * frame) |
| 159 | { |
| 160 | ThisFilter *filter = (ThisFilter *) f; |
| 161 | TF_VARS; |
| 162 | |
| 163 | int second_field = 0; |
| 164 | AllocFilter(filter, frame->width, frame->height); |
| 165 | |
| 166 | if (filter->last_framenr != frame->frameNumber) |
| 167 | { |
| 168 | if (filter->last_framenr != (frame->frameNumber - 1)) |
| 169 | { |
| 170 | memset(filter->got_frames, 0, sizeof(filter->got_frames)); |
| 171 | } |
| 172 | store_ref(filter, frame->buf, frame->offsets, frame->pitches, frame->width, frame->height); |
| 173 | second_field = 0; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | second_field = 1; |
| 178 | } |
| 179 | |
| 180 | /* filter all frames, even if frame->interlaced_frame is not set */ |
| 181 | filter_func( |
| 182 | filter, frame->buf, frame->offsets, frame->pitches, |
| 183 | frame->width, frame->height, second_field, frame->top_field_first); |
| 184 | |
| 185 | filter->last_framenr = frame->frameNumber; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | void CleanupFieldorderDeintFilter (VideoFilter * filter) |
| 192 | { |
| 193 | int i; |
| 194 | ThisFilter* f = (ThisFilter*)filter; |
| 195 | for (i=0; i<NCHANS*NREFS; i++) |
| 196 | { |
| 197 | uint8_t **p= &f->ref[i/NCHANS][i%NCHANS]; |
| 198 | if (*p) free(*p); |
| 199 | *p= NULL; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | VideoFilter * FieldorderDeintFilter (VideoFrameType inpixfmt, VideoFrameType outpixfmt, |
| 204 | int *width, int *height, char *options) |
| 205 | { |
| 206 | ThisFilter *filter; |
| 207 | (void) height; |
| 208 | (void) options; |
| 209 | |
| 210 | fprintf(stderr, "Initialize Fieldorder Deinterlacer. In-Pixformat = %d Out-Pixformat=%d\n", inpixfmt, outpixfmt); |
| 211 | filter = (ThisFilter *) malloc (sizeof(ThisFilter)); |
| 212 | if (filter == NULL) |
| 213 | { |
| 214 | fprintf (stderr, "FieldorderDeint: failed to allocate memory for filter.\n"); |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | filter->width = 0; |
| 219 | filter->height = 0; |
| 220 | memset(filter->ref, 0, sizeof(filter->ref)); |
| 221 | |
| 222 | AllocFilter(filter, *width, *height); |
| 223 | |
| 224 | filter->vf.filter = &FieldorderDeint; |
| 225 | filter->vf.cleanup = &CleanupFieldorderDeintFilter; |
| 226 | return (VideoFilter *) filter; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | static FmtConv FmtList[] = |
| 231 | { |
| 232 | { FMT_YV12, FMT_YV12 } , |
| 233 | FMT_NULL |
| 234 | }; |
| 235 | |
| 236 | FilterInfo filter_table[] = |
| 237 | { |
| 238 | { |
| 239 | symbol: "FieldorderDeintFilter", |
| 240 | name: "fieldorderdoubleprocessdeint", |
| 241 | descript: "avoids synchronisation problems when matching an interlaced video mode to an interlaced source", |
| 242 | formats: FmtList, |
| 243 | libname: NULL |
| 244 | },FILT_NULL |
| 245 | }; |
| 246 | |
| 247 | /* vim: set expandtab tabstop=4 shiftwidth=4: */ |