wfd-video-codec.c 11.8 KB
Newer Older
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
#include <string.h>
#include "wfd-video-codec.h"

G_DEFINE_BOXED_TYPE (WfdVideoCodec, wfd_video_codec, wfd_video_codec_ref, wfd_video_codec_unref)

static const WfdResolution cea_resolutions[] = {
  { 640,  480, 60, FALSE},
  { 720,  480, 60, FALSE},
  { 720,  480, 60, TRUE},
  { 720,  576, 50, FALSE},
  { 720,  576, 50, TRUE},
  {1280,  720, 30, FALSE},
  {1280,  720, 60, FALSE},
  {1920, 1080, 30, FALSE},
  {1920, 1080, 60, FALSE},
  {1920, 1080, 60, TRUE},
  {1290,  720, 25, FALSE},
  {1280,  720, 50, FALSE},
  {1920, 1080, 25, FALSE},
  {1920, 1080, 50, FALSE},
  {1920, 1080, 50, TRUE},
  {1280,  720, 24, FALSE},
  {1920, 1080, 25, FALSE},
};

static const WfdResolution vesa_resolutions[] = {
  { 800,  600, 30, FALSE},
  { 800,  600, 60, FALSE},
  {1024,  768, 30, FALSE},
  {1024,  768, 60, FALSE},
  {1152,  864, 30, FALSE},
  {1152,  864, 60, FALSE},
  {1280,  768, 30, FALSE},
  {1280,  768, 60, FALSE},
  {1280,  800, 30, FALSE},
  {1280,  800, 60, FALSE},
  {1360,  768, 30, FALSE},
  {1360,  768, 60, FALSE},
  {1366,  768, 30, FALSE},
  {1366,  768, 60, FALSE},
  {1280, 1024, 30, FALSE},
  {1280, 1024, 60, FALSE},
  {1400, 1050, 30, FALSE},
  {1400, 1050, 60, FALSE},
  {1440,  900, 30, FALSE},
  {1440,  900, 60, FALSE},
  {1600,  900, 30, FALSE},
  {1600,  900, 60, FALSE},
  {1600, 1200, 30, FALSE},
  {1600, 1200, 60, FALSE},
  {1680, 1024, 30, FALSE},
  {1680, 1024, 30, FALSE},
  {1680, 1050, 30, FALSE},
  {1680, 1050, 60, FALSE},
  {1920, 1200, 30, FALSE},
};

static const WfdResolution hh_resolutions[] = {
  {800, 480, 30, FALSE},
  {800, 480, 60, FALSE},
  {854, 480, 30, FALSE},
  {854, 480, 60, FALSE},
  {864, 480, 30, FALSE},
  {864, 480, 60, FALSE},
  {640, 360, 30, FALSE},
  {640, 360, 60, FALSE},
  {960, 540, 30, FALSE},
  {960, 540, 60, FALSE},
  {848, 480, 30, FALSE},
  {848, 480, 60, FALSE},
};

enum {
  RESOLUTION_TABLE_CEA   = 0x00,
  RESOLUTION_TABLE_HH    = 0x01,
  RESOLUTION_TABLE_VESA  = 0x02,
};

const WfdResolution *
resolution_table_lookup (gint table, guint offset)
{
  const WfdResolution *resolutions;
  gint length;

  if (table == RESOLUTION_TABLE_CEA)
    {
      resolutions = cea_resolutions;
      length = G_N_ELEMENTS (cea_resolutions);
    }
  else if (table == RESOLUTION_TABLE_HH)
    {
      resolutions = hh_resolutions;
      length = G_N_ELEMENTS (hh_resolutions);
    }
  else if (table == RESOLUTION_TABLE_VESA)
    {
      resolutions = vesa_resolutions;
98
      length = G_N_ELEMENTS (vesa_resolutions);
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
    }
  else
    {
      g_return_val_if_reached (NULL);
    }

  if (offset < length)
    return &resolutions[offset];
  else
    return NULL;
}

guint32
sup_for_resolution (gint table, const WfdResolution *resolution)
{
  const WfdResolution *resolutions;
  gint length;

  if (table == RESOLUTION_TABLE_CEA)
    {
      resolutions = cea_resolutions;
      length = G_N_ELEMENTS (cea_resolutions);
    }
  else if (table == RESOLUTION_TABLE_HH)
    {
      resolutions = hh_resolutions;
      length = G_N_ELEMENTS (hh_resolutions);
    }
  else if (table == RESOLUTION_TABLE_VESA)
    {
      resolutions = vesa_resolutions;
130
      length = G_N_ELEMENTS (vesa_resolutions);
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
    }
  else
    {
      g_return_val_if_reached (0);
    }

  for (gint i = 0; i < length; i++)
    {
      if (resolutions[i].width != resolution->width)
        continue;
      if (resolutions[i].height != resolution->height)
        continue;
      if (resolutions[i].refresh_rate != resolution->refresh_rate)
        continue;
      if (resolutions[i].interlaced != resolution->interlaced)
        continue;

      return 1 << i;
    }

  return 0;
}

/**
 * wfd_video_codec_new:
 *
 * Creates a new #WfdVideoCodec.
 *
 * Returns: (transfer full): A newly created #WfdVideoCodec
 */
WfdVideoCodec *
wfd_video_codec_new (void)
{
  WfdVideoCodec *self;

  self = g_slice_new0 (WfdVideoCodec);
  self->ref_count = 1;

  return self;
}

/**
 * wfd_video_codec_copy:
 * @self: a #WfdVideoCodec
 *
 * Makes a deep copy of a #WfdVideoCodec. The parsed information about
 * supported resolutions is *not* copied.
 *
 * Returns: (transfer full): A newly created #WfdVideoCodec with the same
 *   contents as @self
 */
WfdVideoCodec *
wfd_video_codec_copy (WfdVideoCodec *self)
{
  WfdVideoCodec *copy;

  g_return_val_if_fail (self, NULL);
  g_return_val_if_fail (self->ref_count, NULL);

  copy = wfd_video_codec_new ();
  copy->profile = self->profile;
  copy->level   = self->level;
  copy->latency = self->latency;
  copy->native = wfd_resolution_copy (self->native);

  return copy;
}

static void
wfd_video_codec_free (WfdVideoCodec *self)
{
  g_assert (self);
  g_assert_cmpint (self->ref_count, ==, 0);

  g_slice_free (WfdVideoCodec, self);
}

/**
 * wfd_video_codec_ref:
 * @self: A #WfdVideoCodec
 *
 * Increments the reference count of @self by one.
 *
 * Returns: (transfer none): @self
 */
WfdVideoCodec *
wfd_video_codec_ref (WfdVideoCodec *self)
{
  g_return_val_if_fail (self, NULL);
  g_return_val_if_fail (self->ref_count, NULL);

  g_atomic_int_inc (&self->ref_count);

  return self;
}

/**
 * wfd_video_codec_unref:
 * @self: (transfer none): A #WfdVideoCodec
 *
 * Decrements the reference count of @self by one, freeing the structure when
 * the reference count reaches zero.
 */
void
wfd_video_codec_unref (WfdVideoCodec *self)
{
  g_return_if_fail (self);
  g_return_if_fail (self->ref_count);

  if (g_atomic_int_dec_and_test (&self->ref_count))
    wfd_video_codec_free (self);
}

/**
 * wfd_video_codec_new_from_descr:
 * @native: Integer describing the native resolution
 * @descr: A video codec descriptor string
 *
 * Parses the given video descriptor description string.
 *
 * Returns: (transfer full): A newly allocated #WfdVideoCodec, or #NULL on failure.
 */
WfdVideoCodec *
wfd_video_codec_new_from_desc (gint native, const gchar *descr)
{
  g_autoptr(WfdVideoCodec) res;
  g_auto(GStrv) tokens = NULL;
  const WfdResolution *native_res;
  guint32 tmp;

  tokens = g_strsplit (descr, " ", 11);

  if (g_strv_length (tokens) < 9)
    return NULL;

  res = wfd_video_codec_new ();

  res->profile = (WfdH264ProfileFlags) g_ascii_strtoll (tokens[0], NULL, 16);
  if (res->profile != WFD_H264_PROFILE_BASE && res->profile != WFD_H264_PROFILE_HIGH)
    {
      g_warning ("Unknown profile 0x%x", res->profile);
      return NULL;
    }

  res->level = g_ascii_strtoll (tokens[1], NULL, 16);
  if (res->level > 255)
    {
      g_warning ("Unreasonable level 0x%x", res->level);
      return NULL;
    }

  res->cea_sup = g_ascii_strtoll (tokens[2], NULL, 16);
  res->vesa_sup = g_ascii_strtoll (tokens[3], NULL, 16);
  res->hh_sup = g_ascii_strtoll (tokens[4], NULL, 16);

  res->latency = g_ascii_strtoll (tokens[5], NULL, 16);

  res->min_slice_size = g_ascii_strtoll (tokens[6], NULL, 16);

  tmp = g_ascii_strtoll (tokens[7], NULL, 16);
  res->max_slice_num = (tmp & 0x1ff) + 1;
  res->max_slice_size_ratio = (tmp >> 10) & 0x7;

  /* frame_rate_ctrl_sup */
  tmp = g_ascii_strtoll (tokens[8], NULL, 16);
  res->frame_skipping_allowed = tmp & 0x1;

  native_res = resolution_table_lookup (native & 0x7, native >> 3);
  if (native_res)
300
    res->native = wfd_resolution_copy ((WfdResolution*) native_res);
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

  return g_steal_pointer (&res);
}

/**
 * wfd_video_codec_get_max_bitrate_kbit:
 * @self: a #WfdVideoCodec
 *
 * Returns the maximum bitrate supported by the sink in kbit/s.
 *
 * Returns: (transfer full): The maximum bitrate.
 */
guint32
wfd_video_codec_get_max_bitrate_kbit (WfdVideoCodec *self)
{
  guint32 bitrate = 14000;

  switch (self->level)
    {
    case 1:
      bitrate = 14000;
      break;

    case 1 << 1:
        bitrate = 20000;
      break;

    case 1 << 2:
        bitrate = 20000;
      break;

    case 1 << 3:
        bitrate = 50000;
      break;

    case 1 << 4:
        bitrate = 50000;

    default:
      g_warning ("WfdVideoCodec: Unknown level %i", self->level);
    }

  if (self->profile == WFD_H264_PROFILE_HIGH)
    bitrate = bitrate * 1.25;

  return bitrate;
}

/**
 * wfd_video_codec_get_resolutions:
 * @self: a #WfdVideoCodec
 *
 * Returns a list of supported resolutions
 *
 * Returns: (transfer container) (element-type WfdResolution): A newly allocated #WfdVideoCodec, or #NULL on failure.
 */
GList *
wfd_video_codec_get_resolutions (WfdVideoCodec *self)
{
360
  WfdResolution *resolution;
361

362
363
364
365
366
367
368
  g_autoptr(GList) res = NULL;
  guint32 bits, i;

  i = 0;
  for (bits = self->hh_sup; bits; bits = bits >> 1)
    {
      if (bits & 0x1)
369
370
371
372
373
374
375
        {
          resolution = (gpointer) resolution_table_lookup (RESOLUTION_TABLE_HH, i);
          if (resolution)
            res = g_list_append (res, resolution);
          else
            g_warning ("Resolution %d not found in table HH, but was in suppliments 0x%X\n", i, self->hh_sup);
        }
376
377
378
379
      i++;
    }

  i = 0;
380
  for (bits = self->cea_sup; bits; bits = bits >> 1)
381
382
    {
      if (bits & 0x1)
383
384
385
386
387
388
389
        {
          resolution = (gpointer) resolution_table_lookup (RESOLUTION_TABLE_CEA, i);
          if (resolution)
            res = g_list_append (res, resolution);
          else
            g_warning ("Resolution %d not found in table CAE, but was in suppliments 0x%X\n", i, self->cea_sup);
        }
390
391
392
393
      i++;
    }

  i = 0;
394
  for (bits = self->vesa_sup; bits; bits = bits >> 1)
395
396
    {
      if (bits & 0x1)
397
398
399
400
401
402
403
        {
          resolution = (gpointer) resolution_table_lookup (RESOLUTION_TABLE_VESA, i);
          if (resolution)
            res = g_list_append (res, resolution);
          else
            g_warning ("Resolution %d not found in table VESA, but was in suppliments 0x%X\n", i, self->vesa_sup);
        }
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
      i++;
    }

  return g_steal_pointer (&res);
}

/**
 * wfd_video_codec_get_descriptor_for_resolution:
 * @self: a #WfdVideoCodec
 * @resolution: the #WfdResolution
 *
 * Returns the descriptor string for the selected resolution. This can be send
 * to a WFD sink.
 *
 * Returns: (transfer full): The WFD descriptor string for the given resolution.
 */
gchar *
wfd_video_codec_get_descriptor_for_resolution (WfdVideoCodec *self, const WfdResolution *resolution)
{
  guint16 cae_sup, vesa_sup, hh_sup;
  guint32 slice_enc_params;
  guint8 frame_rate_ctrl_sup;

  /* Need to find the resolution in one of the tables. */
  cae_sup = sup_for_resolution (RESOLUTION_TABLE_CEA, resolution);
  hh_sup = sup_for_resolution (RESOLUTION_TABLE_HH, resolution);
  vesa_sup = sup_for_resolution (RESOLUTION_TABLE_VESA, resolution);

  /* Set dynamic frame rate flag? */
  frame_rate_ctrl_sup = self->frame_skipping_allowed & 0x01;
  slice_enc_params = 0;

  return g_strdup_printf ("00 00 %02X %02X %08X %08X %08X %02X %04X %04X %02x none none",
                          /* static: native, resolution and prefered display mode */
                          self->profile, self->level,
                          cae_sup, vesa_sup, hh_sup,
                          self->latency, self->min_slice_size,
                          slice_enc_params, frame_rate_ctrl_sup
                          /* static: maximum width and height */
                         );
}

/**
 * wfd_video_codec_dump:
 * @self: A #WfdVideoCodec
 *
 * Write out information about the video codec for debugging. Uses g_debug()
 * internally.
 */
void
wfd_video_codec_dump (WfdVideoCodec *self)
{
  g_autoptr(GList) resolutions = NULL;
  GList *res = NULL;

  g_debug ("WfdVideoCodec:");
  g_debug (" * profile: %d", self->profile);
  g_debug (" * level: %d", self->level);
  if (self->native)
    {
      g_debug (" * native: %dx%d %d%s",
               self->native->width,
               self->native->height,
               self->native->refresh_rate,
               self->native->interlaced ? "i" : "p");
    }
  resolutions = wfd_video_codec_get_resolutions (self);
  g_debug ("Supported resolutions:");
  for (res = resolutions; res != NULL; res = res->next)
    {
      WfdResolution *resolution = res->data;
      g_debug (" * %dx%d %d%s",
               resolution->width,
               resolution->height,
               resolution->refresh_rate,
               resolution->interlaced ? "i" : "p");
    }
}