MythTV  master
filter_vflip.c
Go to the documentation of this file.
1 /*
2  * filter_vflip
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 
9 #include "filter.h"
10 #include "mythframe.h"
11 
12 typedef struct ThisFilter
13 {
15  TF_STRUCT;
16 } ThisFilter;
17 
18 static void reverse_memcpy(unsigned char *dst, const unsigned char *src, int n)
19 {
20  for (int i = 0; i < n; ++i)
21  dst[n - 1 - i] = src[i];
22 }
23 
24 static int swap(VideoFrame *frame, int datasize, int offset, int shift)
25 {
26  unsigned char *temp = malloc(datasize);
27  if (temp == NULL)
28  {
29  fprintf(stderr, "Couldn't allocate memory for temp\n");
30  return 0;
31  }
32  for (int i = 0, j = (frame->height - 1); i < frame->height / 2; i++, j--)
33  {
34  int newoffset = i * datasize + offset;
35  if (shift)
36  newoffset += datasize;
37  int oldoffset = j * datasize + offset;
38  if (!shift || i != frame->height - 1)
39  {
40  memcpy(temp, frame->buf + newoffset,
41  datasize); // new -> temp
42  reverse_memcpy(frame->buf + newoffset, frame->buf + oldoffset,
43  datasize); // old -> new
44  reverse_memcpy(frame->buf + oldoffset, temp,
45  datasize); // temp -> old
46  }
47  }
48  if (temp)
49  free(temp);
50 
51  return 1;
52 }
53 
54 static int comp(const void *va, const void *vb)
55 {
56  int *a = (int *) va;
57  int *b = (int *) vb;
58  if (*a == *b)
59  return 0;
60  if (*a < *b)
61  return -1;
62  return 1;
63 }
64 
65 static int vflip(VideoFilter *vf, VideoFrame *frame, int field)
66 {
67  (void)field;
68 
69  if (frame->codec != FMT_YV12)
70  {
71  fprintf(stderr, "codec %d unsupported for vflip, skipping\n",
72  frame->codec);
73  return 0;
74  }
75 
76  TF_VARS;
77 
78  (void)vf;
79 
80  TF_START;
81 
82  int ouroffsets[3];
83  memcpy(ouroffsets, frame->offsets, 3 * sizeof(int));
84  qsort(ouroffsets, 3, sizeof(int), comp);
85 
86  int datasize;
87  datasize = (ouroffsets[1] - ouroffsets[0]) / frame->height;
88  if (!swap(frame, datasize, ouroffsets[0], 0))
89  return 0;
90  datasize = (ouroffsets[2] - ouroffsets[1]) / frame->height;
91  if (!swap(frame, datasize, ouroffsets[1], 0))
92  return 0;
93  datasize = (frame->size - ouroffsets[2]) / frame->height;
94  if (!swap(frame, datasize, ouroffsets[2], 0))
95  return 0;
96 
97  TF_END((ThisFilter *)vf, "VFlip");
98 
99  return 1;
100 }
101 
103  VideoFrameType outpixfmt,
104  const int *width, const int *height, const char *options,
105  int threads)
106 {
107  ThisFilter *filter;
108 
109  (void)width;
110  (void)height;
111  (void)options;
112  (void)threads;
113 
114  if ((inpixfmt != outpixfmt) ||
115  (inpixfmt != FMT_YV12 && inpixfmt != FMT_RGB24 &&
116  inpixfmt != FMT_YUV422P) )
117  return NULL;
118 
119  filter = malloc(sizeof(ThisFilter));
120 
121  if (filter == NULL)
122  {
123  fprintf(stderr, "Couldn't allocate memory for filter\n");
124  return NULL;
125  }
126  filter->vf.filter = &vflip;
127  filter->vf.cleanup = NULL;
128  TF_INIT(filter)
129  return (VideoFilter *) filter;
130 }
131 
132 static FmtConv FmtList[] =
133 {
134  { FMT_YV12, FMT_YV12 },
135  FMT_NULL
136 };
137 
139 {
140  {
142  .name= (char*)"vflip",
143  .descript= (char*)"flips the video image vertically",
144  .formats= FmtList,
145  .libname= NULL
146  },
147  FILT_NULL
148 };
int(* filter)(struct VideoFilter_ *, VideoFrame *, int)
Definition: filter.h:37
static int swap(VideoFrame *frame, int datasize, int offset, int shift)
Definition: filter_vflip.c:24
init_filter filter_init
Definition: filter.h:28
stderr
Definition: ttvdb.py:1426
#define TF_VARS
Definition: filter.h:112
#define NULL
Definition: H264Parser.h:62
void(* cleanup)(struct VideoFilter_ *)
Definition: filter.h:38
static void reverse_memcpy(unsigned char *dst, const unsigned char *src, int n)
Definition: filter_vflip.c:18
enum FrameType_ VideoFrameType
static FmtConv FmtList[]
Definition: filter_vflip.c:132
unsigned char b
Definition: ParseText.cpp:329
static int vflip(VideoFilter *vf, VideoFrame *frame, int field)
Definition: filter_vflip.c:65
int offsets[3]
Y, U, & V offsets.
Definition: mythframe.h:64
VideoFilter vf
Definition: filter_adjust.c:36
int height
Definition: mythframe.h:42
#define FMT_NULL
Definition: filter.h:20
#define TF_END(filter, prefix)
Definition: filter.h:114
#define FILT_NULL
Definition: filter.h:47
static VideoFilter * new_filter(VideoFrameType inpixfmt, VideoFrameType outpixfmt, const int *width, const int *height, const char *options, int threads)
Definition: filter_vflip.c:102
const FilterInfo filter_table[]
Definition: filter_vflip.c:138
#define TF_INIT(filter)
Definition: filter.h:110
static int comp(const void *va, const void *vb)
Definition: filter_vflip.c:54
#define TF_START
Definition: filter.h:113
unsigned char * buf
Definition: mythframe.h:39
struct ThisFilter ThisFilter
VideoFrameType codec
Definition: mythframe.h:38