summaryrefslogtreecommitdiff
path: root/gst-libs/gst/video/video-vbi.c
blob: 59bae579e24ba536145fd65e881935435f5c3a86 (plain)
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
/* GStreamer
 * Copyright (C) 2018 Edward Hervey <edward@centricular.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <gst/base/gstbytereader.h>
#include "video-vbi.h"

#ifndef GST_DISABLE_GST_DEBUG
#define GST_CAT_DEFAULT ensure_debug_category()
static GstDebugCategory *
ensure_debug_category (void)
{
  static gsize cat_gonce = 0;

  if (g_once_init_enter (&cat_gonce)) {
    gsize cat_done;

    cat_done = (gsize) _gst_debug_category_new ("video-vbi", 0,
        "video VBI parser");

    g_once_init_leave (&cat_gonce, cat_done);
  }

  return (GstDebugCategory *) cat_gonce;
}
#else
#define ensure_debug_category() /* NOOP */
#endif /* GST_DISABLE_GST_DEBUG */

struct _GstVideoVBIParser
{
  GstVideoInfo info;            /* format of the lines provided */
  guint16 *work_data;           /* Converted line in planar 16bit format */
  guint32 work_data_size;       /* Size (in guint16) of work_data */
  guint offset;                 /* Current offset (in guint16) in work_data */
};

/* Smallest ANC size (which would have a size Data Count of 0 though) */
#define SMALLEST_ANC_SIZE 7

GstVideoVBIParserResult
gst_video_vbi_parser_get_ancillary (GstVideoVBIParser * parser,
    GstVideoAncillary * anc)
{
  gboolean found = FALSE;

  g_return_val_if_fail (parser != NULL, GST_VIDEO_VBI_PARSER_RESULT_ERROR);
  g_return_val_if_fail (anc != NULL, GST_VIDEO_VBI_PARSER_RESULT_ERROR);

  while (parser->offset < parser->work_data_size + SMALLEST_ANC_SIZE) {
    guint8 DID, SDID, DC;

    /* Look for ADF
     * FIXME : This assumes 10bit data with parity ! */
    if (parser->work_data[parser->offset] != 0x000 ||
        parser->work_data[parser->offset + 1] != 0x3ff ||
        parser->work_data[parser->offset + 2] != 0x3ff) {
      parser->offset += 1;
      continue;
    }

    /* FIXME : Add parity and checksum checks at some point if using
     * 10bit data */

    /* We have a valid ADF */
    DID = parser->work_data[parser->offset + 3] & 0xff;
    SDID = parser->work_data[parser->offset + 4] & 0xff;
    DC = parser->work_data[parser->offset + 5] & 0xff;
    /* Check if we have enough room to get the User Data */
    if (parser->offset >= parser->work_data_size + SMALLEST_ANC_SIZE + DC)
      goto not_enough_data;

    /* We found a valid ANC \o/ */
    anc->DID = DID;
    anc->SDID_block_number = SDID;
    anc->data_count = DC;
    anc->data = parser->work_data + parser->offset + 6;
    found = TRUE;
    parser->offset += SMALLEST_ANC_SIZE + DC;
    break;
  }

  if (found)
    return GST_VIDEO_VBI_PARSER_RESULT_OK;

  return GST_VIDEO_VBI_PARSER_RESULT_DONE;

  /* ERRORS */
not_enough_data:
  {
    GST_WARNING ("ANC requires more User Data that available line size");
    /* Avoid further calls to go in the same error */
    parser->offset = parser->work_data_size;
    return GST_VIDEO_VBI_PARSER_RESULT_ERROR;
  }
}

GstVideoVBIParser *
gst_video_vbi_parser_new (GstVideoFormat format, guint32 pixel_width)
{
  GstVideoVBIParser *parser;

  switch (format) {
    case GST_VIDEO_FORMAT_v210:
      parser = g_new0 (GstVideoVBIParser, 1);
      break;
    default:
      GST_WARNING ("Format not supported by GstVideoVBIParser");
      return NULL;
  }

  gst_video_info_init (&parser->info);
  if (!gst_video_info_set_format (&parser->info, format, pixel_width, 1)) {
    GST_ERROR ("Could not create GstVideoInfo");
    g_free (parser);
    return NULL;
  }

  /* Allocate the workspace which is going to be 2 * pixel_width * data big */
  parser->work_data_size = 2 * pixel_width;
  parser->work_data = g_new0 (guint16, parser->work_data_size);
  parser->offset = 0;

  return parser;
}

void
gst_video_vbi_parser_free (GstVideoVBIParser * parser)
{
  g_free (parser->work_data);
  g_free (parser);
}

static void
gst_info_dump_mem16_line (gchar * linebuf, gsize linebuf_size,
    const guint16 * mem, gsize mem_offset, gsize mem_size)
{
  gchar hexstr[50], digitstr[6];

  if (mem_size > 8)
    mem_size = 8;

  hexstr[0] = '\0';

  if (mem != NULL) {
    guint i = 0;

    mem += mem_offset;
    while (i < mem_size) {
      g_snprintf (digitstr, sizeof (digitstr), "%04x ", mem[i]);
      g_strlcat (hexstr, digitstr, sizeof (hexstr));
      ++i;
    }
  }

  g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s",
      (guint) mem_offset, hexstr);
}


void
gst_video_vbi_parser_add_line (GstVideoVBIParser * parser, const guint8 * data)
{
  guint i;
  guint16 *y = parser->work_data;
  guint16 *uv = y + parser->info.width;
  guint32 a, b, c, d;

  parser->offset = 0;
  /* FIXME : Only supports v210 ! */

  /* Convert the line \o/ */
  for (i = 0; i < parser->info.width - 5; i += 6) {
    a = GST_READ_UINT32_LE (data + (i / 6) * 16 + 0);
    b = GST_READ_UINT32_LE (data + (i / 6) * 16 + 4);
    c = GST_READ_UINT32_LE (data + (i / 6) * 16 + 8);
    d = GST_READ_UINT32_LE (data + (i / 6) * 16 + 12);

    *uv++ = (a >> 0) & 0x3ff;
    *y++ = (a >> 10) & 0x3ff;
    *uv++ = (a >> 20) & 0x3ff;
    *y++ = (b >> 0) & 0x3ff;

    *uv++ = (b >> 10) & 0x3ff;
    *y++ = (b >> 20) & 0x3ff;
    *uv++ = (c >> 0) & 0x3ff;
    *y++ = (c >> 10) & 0x3ff;

    *uv++ = (c >> 20) & 0x3ff;
    *y++ = (d >> 0) & 0x3ff;
    *uv++ = (d >> 10) & 0x3ff;
    *y++ = (d >> 20) & 0x3ff;
  }
  if (1) {
    guint off = 0;
    gsize length = parser->info.width * 2;

    GST_TRACE ("--------"
        "-------------------------------------------------------------------");

    while (off < length) {
      gchar buf[128];

      /* gst_info_dump_mem_line will process 16 bytes (8 16bit chunks) at most */
      gst_info_dump_mem16_line (buf, sizeof (buf), parser->work_data, off,
          length - off);
      GST_TRACE ("%s", buf);
      off += 8;
    }
    GST_TRACE ("--------"
        "-------------------------------------------------------------------");
  }
}