File: transform_context.c

package info (click to toggle)
gavl 1.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 14,912 kB
  • sloc: ansic: 437,319; sh: 10,487; makefile: 238
file content (392 lines) | stat: -rw-r--r-- 11,146 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*****************************************************************
 * gavl - a general purpose audio/video processing library
 *
 * Copyright (c) 2001 - 2011 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <gavl/gavl.h>
#include <video.h>
#include <transform.h>
#include <stdio.h>
#include <accel.h>


static gavl_transform_scanline_func get_func(gavl_transform_funcs_t * tab,
                                             gavl_pixelformat_t pixelformat,
                                             int * bits)
  {
  switch(pixelformat)
    {
    case GAVL_PIXELFORMAT_NONE:
      break;
    case GAVL_RGB_15:
    case GAVL_BGR_15:
      *bits = tab->bits_rgb_15;
      return tab->transform_rgb_15;
      break;
    case GAVL_RGB_16:
    case GAVL_BGR_16:
      *bits = tab->bits_rgb_16;
      return tab->transform_rgb_16;
      break;
    case GAVL_RGB_24:
    case GAVL_BGR_24:
      *bits = tab->bits_uint8_noadvance;
      return tab->transform_uint8_x_3;
      break;
    case GAVL_RGB_32:
    case GAVL_BGR_32:
      *bits = tab->bits_uint8_noadvance;
      return tab->transform_uint8_x_3;
      break;
    case GAVL_GRAYA_16:
      *bits = tab->bits_uint8_noadvance;
      return tab->transform_uint8_x_2;
      break;
    case GAVL_GRAY_16:
      *bits = tab->bits_uint16_x_1;
      return tab->transform_uint16_x_1;
      break;
    case GAVL_GRAYA_32:
      *bits = tab->bits_uint16_x_2;
      return tab->transform_uint16_x_2;
      break;
    case GAVL_YUVA_32:
    case GAVL_RGBA_32:
      *bits = tab->bits_uint8_noadvance;
      return tab->transform_uint8_x_4;
      break;
    case GAVL_YUY2:
      *bits = tab->bits_uint8_advance;
      return tab->transform_uint8_x_1_advance;
      break;
    case GAVL_UYVY:
      *bits = tab->bits_uint8_advance;
      return tab->transform_uint8_x_1_advance;
      break;
    case GAVL_YUV_420_P:
    case GAVL_YUV_422_P:
    case GAVL_YUV_444_P:
    case GAVL_YUV_411_P:
    case GAVL_YUV_410_P:
    case GAVL_YUVJ_420_P:
    case GAVL_YUVJ_422_P:
    case GAVL_YUVJ_444_P:
    case GAVL_GRAY_8:
      *bits = tab->bits_uint8_noadvance;
      return tab->transform_uint8_x_1_noadvance;
      break;
    case GAVL_YUV_444_P_16:
    case GAVL_YUV_422_P_16:
      *bits = tab->bits_uint16_x_1;
      return tab->transform_uint16_x_1;
      break;
    case GAVL_RGB_48:
      *bits = tab->bits_uint16_x_3;
      return tab->transform_uint16_x_3;
      break;
    case GAVL_RGBA_64:
    case GAVL_YUVA_64:
      *bits = tab->bits_uint16_x_4;
      return tab->transform_uint16_x_4;
      break;
    case GAVL_GRAY_FLOAT:
      *bits = 0;
      return tab->transform_float_x_1;
      break;
    case GAVL_GRAYA_FLOAT:
      *bits = 0;
      return tab->transform_float_x_2;
      break;
    case GAVL_YUV_FLOAT:
    case GAVL_RGB_FLOAT:
      *bits = 0;
      return tab->transform_float_x_3;
      break;
    case GAVL_RGBA_FLOAT:
    case GAVL_YUVA_FLOAT:
      *bits = 0;
      return tab->transform_float_x_4;
      break;
    }
  return NULL;
  }


static void init_func_tab(gavl_video_options_t * opt,
                          gavl_transform_context_t * ctx,
                          gavl_transform_funcs_t * func_tab)
  {
  if(!opt->quality)
    memset(func_tab, 0, sizeof(*func_tab));
  
  switch(ctx->tab.factors_per_pixel)
    {
    case 1:
      if((opt->quality > 0) || (opt->accel_flags & GAVL_ACCEL_C))
        gavl_init_transform_funcs_nearest_c(func_tab, ctx->advance);
      break;
    case 2:
      if((opt->quality > 0) || (opt->accel_flags & GAVL_ACCEL_C))
        gavl_init_transform_funcs_bilinear_c(func_tab, ctx->advance);
#ifdef HAVE_MMX
      if((opt->quality < 3) && (opt->accel_flags & GAVL_ACCEL_MMX))
        gavl_init_transform_funcs_bilinear_mmx(func_tab, ctx->advance);
      if((opt->quality < 3) && (opt->accel_flags & GAVL_ACCEL_MMXEXT))
        gavl_init_transform_funcs_bilinear_mmxext(func_tab, ctx->advance);
      
#endif
      break;
    case 3:
      if((opt->quality > 0) || (opt->accel_flags & GAVL_ACCEL_C))
        gavl_init_transform_funcs_quadratic_c(func_tab, ctx->advance);
#ifdef HAVE_MMX
      if((opt->quality < 3) && (opt->accel_flags & GAVL_ACCEL_MMX))
        gavl_init_transform_funcs_quadratic_mmx(func_tab, ctx->advance);
      if((opt->quality < 3) && (opt->accel_flags & GAVL_ACCEL_MMXEXT))
        gavl_init_transform_funcs_quadratic_mmxext(func_tab, ctx->advance);
#endif
      break;
    case 4:
      if((opt->quality > 0) || (opt->accel_flags & GAVL_ACCEL_C))
        gavl_init_transform_funcs_bicubic_c(func_tab, ctx->advance);
#ifdef HAVE_MMX
      if((opt->quality < 3) && (opt->accel_flags & GAVL_ACCEL_MMX))
        gavl_init_transform_funcs_bicubic_mmx(func_tab, ctx->advance);
      if((opt->quality < 3) && (opt->accel_flags & GAVL_ACCEL_MMXEXT))
        gavl_init_transform_funcs_bicubic_mmxext(func_tab, ctx->advance);
#endif
      break;
    default:
      fprintf(stderr, "BUG: Filter taps > 4 in image transform\n");
      break;
    }
  }

int
gavl_transform_context_init(gavl_image_transform_t * t,
                            gavl_video_options_t * opt,
                            int field_index, int plane_index,
                            gavl_image_transform_func func, void * priv)
  {
  gavl_transform_funcs_t func_tab;
  int bits = 0;
  
  float off_x, off_y;
  float scale_x, scale_y;
  int sub_h, sub_v;
  gavl_transform_context_t * ctx;
  ctx = &t->contexts[field_index][plane_index];
  
  if(field_index == 1)
    {
    ctx->field = 1;
    ctx->num_fields = 2;
    }
  else
    {
    ctx->field = 0;
    if((t->format.interlace_mode == GAVL_INTERLACE_TOP_FIRST) ||
       (t->format.interlace_mode == GAVL_INTERLACE_BOTTOM_FIRST))
      ctx->num_fields = 2;
    else
      ctx->num_fields = 1;
    }
  if((t->format.pixelformat == GAVL_YUY2) ||
     (t->format.pixelformat == GAVL_UYVY))
    ctx->plane = 0;
  else
    ctx->plane = plane_index;
  
  gavl_pixelformat_get_offset(t->format.pixelformat,
                              plane_index,
                              &ctx->advance,
                              &ctx->offset);

  ctx->dst_width  = t->format.image_width;
  ctx->dst_height = t->format.image_height;

  scale_x = 1.0;
  scale_y = 1.0;

  ctx->dst_height /= ctx->num_fields;
  scale_y *= ctx->num_fields;

  off_x = 0.5;
  off_y = 0.5 + (float)(ctx->field);
  //  off_x = 0.0;
  //  off_y = (float)(ctx->field);
  
  if(plane_index)
    {
    gavl_pixelformat_chroma_sub(t->format.pixelformat,
                                &sub_h, &sub_v);
    ctx->dst_width  /= sub_h;
    ctx->dst_height /= sub_v;
    scale_x *= sub_h;
    scale_y *= sub_v;

    if((sub_h == 2) && (sub_v == 2))
      {
      switch(t->format.chroma_placement)
        {
        case GAVL_CHROMA_PLACEMENT_DEFAULT:
          off_x += 0.5;
          off_y += 0.5;
          break;
        case GAVL_CHROMA_PLACEMENT_MPEG2:
          if(ctx->num_fields == 1)
            off_y += 0.5;
          else if(ctx->field == 0) /* Top field */
            off_y += 0.5; /* In FRAME coordinates */
          else /* Bottom field */
            off_y += 1.5; /* In FRAME coordinates */
          break;
        case GAVL_CHROMA_PLACEMENT_DVPAL:
          if(ctx->plane == 1) /* Cb */
            off_y += 2.0; /* In FRAME coordinates */
          break;
        }
      }
    }

  gavl_transform_table_init(&ctx->tab, opt,
                            func, priv,
                            off_x, off_y, scale_x,
                            scale_y,
                            ctx->dst_width, ctx->dst_height);

  /* Get function */

  init_func_tab(opt, ctx, &func_tab);

  ctx->func = get_func(&func_tab,
                       t->format.pixelformat,
                       &bits);

  if(!ctx->func)
    return 0;
  
  /* Now we know the bits, convert to int */
  if(bits)
    gavl_transform_table_init_int(&ctx->tab,
                                  bits, ctx->dst_width, ctx->dst_height);
  return 1;
  }

static void func_1(void* p, int start, int end)
  {
  int i;
  uint8_t * dst_save;
  int dst_stride;
  
  gavl_transform_context_t * ctx = p;
  dst_stride =
    ctx->dst_frame->strides[ctx->plane] *
    ctx->num_fields;
  
  dst_save = ctx->dst_frame->planes[ctx->plane] +
    ctx->offset + ctx->field * ctx->dst_frame->strides[ctx->plane] +
    start * dst_stride;
  
  for(i = start; i < end; i++)
    {
    ctx->func(ctx, ctx->tab.pixels[i], dst_save);
    dst_save += dst_stride;
    }
#ifdef HAVE_MMX
  if(ctx->need_emms)
    {
    __asm__ __volatile__ ("emms");
    }
#endif
  }


void gavl_transform_context_transform(gavl_transform_context_t * ctx,
                                 const gavl_video_frame_t * src,
                                 gavl_video_frame_t * dst)
  {
  int i;
  /* Things set while transforming */
  //  uint8_t * src; /* Beginning of plane */
  //  int src_stride;
  
  //  uint8_t * dst; /* Current scanline */
  //  int dst_stride;
  
  ctx->src = src->planes[ctx->plane] +
    ctx->offset + ctx->field * src->strides[ctx->plane];
  
  ctx->src_stride = src->strides[ctx->plane] * ctx->num_fields;
  
  if(ctx->opt->num_threads > 1)
    {
    int delta;
    int scanline;
    int nt;
    
    ctx->dst_frame = dst;
    nt = ctx->opt->num_threads;
    if(nt > ctx->dst_height)
      nt = ctx->dst_height;
    
    delta = ctx->dst_height / nt;
    scanline = 0;
    for(i = 0; i < nt - 1; i++)
      {
      ctx->opt->run_func(func_1, ctx, scanline,
                         scanline+delta, ctx->opt->run_data, i);
      scanline += delta;
      }
    ctx->opt->run_func(func_1, ctx, scanline, ctx->dst_height,
                       ctx->opt->run_data, nt - 1);
    
    for(i = 0; i < nt; i++)
      ctx->opt->stop_func(ctx->opt->stop_data, i);
    }
  else
    {
    int dst_stride;
    uint8_t * dst_save;

    dst_save = dst->planes[ctx->plane] +
      ctx->offset + ctx->field * dst->strides[ctx->plane];
    
    dst_stride = dst->strides[ctx->plane] * ctx->num_fields;
    for(i = 0; i < ctx->dst_height; i++)
      {
      ctx->func(ctx, ctx->tab.pixels[i], dst_save);
      dst_save += dst_stride;
      }
#ifdef HAVE_MMX
    if(ctx->need_emms)
      {
      __asm__ __volatile__ ("emms");
      // fprintf(stderr, "emms");
      }
#endif
    }
  
  }

void
gavl_transform_context_free(gavl_transform_context_t * ctx)
  {
  gavl_transform_table_free(&ctx->tab);
  }