~ubuntu-branches/ubuntu/oneiric/mplayer2/oneiric-proposed

« back to all changes in this revision

Viewing changes to mplayer/libmpdemux/yuv4mpeg.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2011-03-20 22:48:03 UTC
  • Revision ID: james.westby@ubuntu.com-20110320224803-kc2nlrxz6pcphmf1
Tags: upstream-2.0~rc2
ImportĀ upstreamĀ versionĀ 2.0~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  yuv4mpeg.c:  Functions for reading and writing "new" YUV4MPEG streams
 
3
 *
 
4
 *  Copyright (C) 2001 Matthew J. Marjanovic <maddog@mir.com>
 
5
 *
 
6
 *  This file is part of the MJPEG Tools package (mjpeg.sourceforge.net).
 
7
 *  Ported to mplayer by Rik Snel <rsnel@cube.dyndns.org>
 
8
 *
 
9
 *  This program is free software; you can redistribute it and/or
 
10
 *  modify it under the terms of the GNU General Public License
 
11
 *  as published by the Free Software Foundation; either version 2
 
12
 *  of the License, or (at your option) any later version.
 
13
 *
 
14
 *  This program is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with this program; if not, write to the Free Software
 
21
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
22
 *
 
23
 */
 
24
 
 
25
#include "config.h"
 
26
 
 
27
#include <unistd.h>
 
28
#include <stdio.h>
 
29
#include <stdlib.h>
 
30
#include <string.h>
 
31
#include "yuv4mpeg.h"
 
32
#include "yuv4mpeg_intern.h"
 
33
#include "mp_msg.h"
 
34
 
 
35
static int _y4mparam_allow_unknown_tags = 1;  /* default is forgiveness */
 
36
 
 
37
static void *(*_y4m_alloc)(size_t bytes) = malloc;
 
38
static void (*_y4m_free)(void *ptr) = free;
 
39
 
 
40
int y4m_allow_unknown_tags(int yn) {
 
41
        int old = _y4mparam_allow_unknown_tags;
 
42
        if (yn >= 0) _y4mparam_allow_unknown_tags = (yn) ? 1 : 0;
 
43
        return old;
 
44
}
 
45
 
 
46
 
 
47
 
 
48
/*************************************************************************
 
49
 *
 
50
 * Convenience functions for fd read/write
 
51
 *
 
52
 *   - guaranteed to transfer entire payload (or fail)
 
53
 *   - returns:
 
54
 *               0 on complete success
 
55
 *               +(# of remaining bytes) on eof (for y4m_read)
 
56
 *               -(# of rem. bytes) on error (and ERRNO should be set)
 
57
 *
 
58
 *************************************************************************/
 
59
 
 
60
 
 
61
ssize_t y4m_read(stream_t *s, char *buf, size_t len)
 
62
{
 
63
   ssize_t n;
 
64
 
 
65
   while (len > 0) {
 
66
     n = stream_read(s, buf, len);
 
67
     if (n <= 0) {
 
68
       /* return amount left to read */
 
69
       if (n == 0)
 
70
         return len;  /* n == 0 --> eof */
 
71
       else
 
72
         return -len; /* n < 0 --> error */
 
73
     }
 
74
     buf += n;
 
75
     len -= n;
 
76
   }
 
77
   return 0;
 
78
}
 
79
 
 
80
 
 
81
#if 0 /* not needed */
 
82
ssize_t y4m_write(int fd, char *buf, size_t len)
 
83
{
 
84
   ssize_t n;
 
85
 
 
86
   while (len > 0) {
 
87
     n = write(fd, buf, len);
 
88
     if (n < 0) return -len;  /* return amount left to write */
 
89
     buf += n;
 
90
     len -= n;
 
91
   }
 
92
   return 0;
 
93
}
 
94
#endif
 
95
 
 
96
 
 
97
/*************************************************************************
 
98
 *
 
99
 * "Extra tags" handling
 
100
 *
 
101
 *************************************************************************/
 
102
 
 
103
 
 
104
static char *y4m_new_xtag(void)
 
105
{
 
106
  return _y4m_alloc(Y4M_MAX_XTAG_SIZE);
 
107
}
 
108
 
 
109
 
 
110
void y4m_init_xtag_list(y4m_xtag_list_t *xtags)
 
111
{
 
112
  int i;
 
113
  xtags->count = 0;
 
114
  for (i = 0; i < Y4M_MAX_XTAGS; i++) {
 
115
    xtags->tags[i] = NULL;
 
116
  }
 
117
}
 
118
 
 
119
 
 
120
void y4m_fini_xtag_list(y4m_xtag_list_t *xtags)
 
121
{
 
122
  int i;
 
123
  for (i = 0; i < Y4M_MAX_XTAGS; i++) {
 
124
    if (xtags->tags[i] != NULL) {
 
125
      _y4m_free(xtags->tags[i]);
 
126
      xtags->tags[i] = NULL;
 
127
    }
 
128
  }
 
129
  xtags->count = 0;
 
130
}
 
131
 
 
132
 
 
133
void y4m_copy_xtag_list(y4m_xtag_list_t *dest, const y4m_xtag_list_t *src)
 
134
{
 
135
  int i;
 
136
  for (i = 0; i < src->count; i++) {
 
137
    if (dest->tags[i] == NULL)
 
138
      dest->tags[i] = y4m_new_xtag();
 
139
    strncpy(dest->tags[i], src->tags[i], Y4M_MAX_XTAG_SIZE);
 
140
  }
 
141
  dest->count = src->count;
 
142
}
 
143
 
 
144
 
 
145
 
 
146
#if 0
 
147
static int y4m_snprint_xtags(char *s, int maxn, y4m_xtag_list_t *xtags)
 
148
{
 
149
  int i, room;
 
150
 
 
151
  for (i = 0, room = maxn - 1; i < xtags->count; i++) {
 
152
    int n = snprintf(s, room + 1, " %s", xtags->tags[i]);
 
153
    if ((n < 0) || (n > room)) return Y4M_ERR_HEADER;
 
154
    s += n;
 
155
    room -= n;
 
156
  }
 
157
  s[0] = '\n';  /* finish off header with newline */
 
158
  s[1] = '\0';  /* ...and end-of-string           */
 
159
  return Y4M_OK;
 
160
}
 
161
#endif
 
162
 
 
163
 
 
164
int y4m_xtag_count(const y4m_xtag_list_t *xtags)
 
165
{
 
166
  return xtags->count;
 
167
}
 
168
 
 
169
 
 
170
const char *y4m_xtag_get(const y4m_xtag_list_t *xtags, int n)
 
171
{
 
172
  if (n >= xtags->count)
 
173
    return NULL;
 
174
  else
 
175
    return xtags->tags[n];
 
176
}
 
177
 
 
178
 
 
179
int y4m_xtag_add(y4m_xtag_list_t *xtags, const char *tag)
 
180
{
 
181
  if (xtags->count >= Y4M_MAX_XTAGS) return Y4M_ERR_XXTAGS;
 
182
  if (xtags->tags[xtags->count] == NULL) {
 
183
    xtags->tags[xtags->count] = y4m_new_xtag();
 
184
  }
 
185
  strncpy(xtags->tags[xtags->count], tag, Y4M_MAX_XTAG_SIZE);
 
186
  (xtags->count)++;
 
187
  return Y4M_OK;
 
188
}
 
189
 
 
190
 
 
191
int y4m_xtag_remove(y4m_xtag_list_t *xtags, int n)
 
192
{
 
193
  int i;
 
194
  char *q;
 
195
 
 
196
  if ((n < 0) || (n >= xtags->count)) return Y4M_ERR_RANGE;
 
197
  q = xtags->tags[n];
 
198
  for (i = n; i < (xtags->count - 1); i++)
 
199
    xtags->tags[i] = xtags->tags[i+1];
 
200
  xtags->tags[i] = q;
 
201
  (xtags->count)--;
 
202
  return Y4M_OK;
 
203
}
 
204
 
 
205
 
 
206
int y4m_xtag_clearlist(y4m_xtag_list_t *xtags)
 
207
{
 
208
  xtags->count = 0;
 
209
  return Y4M_OK;
 
210
}
 
211
 
 
212
 
 
213
int y4m_xtag_addlist(y4m_xtag_list_t *dest, const y4m_xtag_list_t *src)
 
214
{
 
215
  int i, j;
 
216
 
 
217
  if ((dest->count + src->count) > Y4M_MAX_XTAGS) return Y4M_ERR_XXTAGS;
 
218
  for (i = dest->count, j = 0;
 
219
       j < src->count;
 
220
       i++, j++) {
 
221
    if (dest->tags[i] == NULL)
 
222
      dest->tags[i] = y4m_new_xtag();
 
223
    strncpy(dest->tags[i], src->tags[i], Y4M_MAX_XTAG_SIZE);
 
224
  }
 
225
  dest->count += src->count;
 
226
  return Y4M_OK;
 
227
}
 
228
 
 
229
 
 
230
/*************************************************************************
 
231
 *
 
232
 * Creators/destructors for y4m_*_info_t structures
 
233
 *
 
234
 *************************************************************************/
 
235
 
 
236
 
 
237
void y4m_init_stream_info(y4m_stream_info_t *info)
 
238
{
 
239
  if (info == NULL) return;
 
240
  /* initialize info */
 
241
  info->width = Y4M_UNKNOWN;
 
242
  info->height = Y4M_UNKNOWN;
 
243
  info->interlace = Y4M_UNKNOWN;
 
244
  info->framerate = y4m_fps_UNKNOWN;
 
245
  info->sampleaspect = y4m_sar_UNKNOWN;
 
246
  y4m_init_xtag_list(&(info->x_tags));
 
247
}
 
248
 
 
249
 
 
250
void y4m_copy_stream_info(y4m_stream_info_t *dest, y4m_stream_info_t *src)
 
251
{
 
252
  if ((dest == NULL) || (src == NULL)) return;
 
253
  /* copy info */
 
254
  dest->width = src->width;
 
255
  dest->height = src->height;
 
256
  dest->interlace = src->interlace;
 
257
  dest->framerate = src->framerate;
 
258
  dest->sampleaspect = src->sampleaspect;
 
259
  y4m_copy_xtag_list(&(dest->x_tags), &(src->x_tags));
 
260
}
 
261
 
 
262
 
 
263
void y4m_fini_stream_info(y4m_stream_info_t *info)
 
264
{
 
265
  if (info == NULL) return;
 
266
  y4m_fini_xtag_list(&(info->x_tags));
 
267
}
 
268
 
 
269
 
 
270
void y4m_si_set_width(y4m_stream_info_t *si, int width)
 
271
{
 
272
  si->width = width;
 
273
  si->framelength = (si->height * si->width) * 3 / 2;
 
274
}
 
275
 
 
276
int y4m_si_get_width(y4m_stream_info_t *si)
 
277
{ return si->width; }
 
278
 
 
279
void y4m_si_set_height(y4m_stream_info_t *si, int height)
 
280
{
 
281
  si->height = height;
 
282
  si->framelength = (si->height * si->width) * 3 / 2;
 
283
}
 
284
 
 
285
int y4m_si_get_height(y4m_stream_info_t *si)
 
286
{ return si->height; }
 
287
 
 
288
void y4m_si_set_interlace(y4m_stream_info_t *si, int interlace)
 
289
{ si->interlace = interlace; }
 
290
 
 
291
int y4m_si_get_interlace(y4m_stream_info_t *si)
 
292
{ return si->interlace; }
 
293
 
 
294
void y4m_si_set_framerate(y4m_stream_info_t *si, y4m_ratio_t framerate)
 
295
{ si->framerate = framerate; }
 
296
 
 
297
y4m_ratio_t y4m_si_get_framerate(y4m_stream_info_t *si)
 
298
{ return si->framerate; }
 
299
 
 
300
void y4m_si_set_sampleaspect(y4m_stream_info_t *si, y4m_ratio_t sar)
 
301
{ si->sampleaspect = sar; }
 
302
 
 
303
y4m_ratio_t y4m_si_get_sampleaspect(y4m_stream_info_t *si)
 
304
{ return si->sampleaspect; }
 
305
 
 
306
int y4m_si_get_framelength(y4m_stream_info_t *si)
 
307
{ return si->framelength; }
 
308
 
 
309
y4m_xtag_list_t *y4m_si_xtags(y4m_stream_info_t *si)
 
310
{ return &(si->x_tags); }
 
311
 
 
312
 
 
313
 
 
314
void y4m_init_frame_info(y4m_frame_info_t *info)
 
315
{
 
316
  if (info == NULL) return;
 
317
  /* initialize info */
 
318
  y4m_init_xtag_list(&(info->x_tags));
 
319
}
 
320
 
 
321
 
 
322
void y4m_copy_frame_info(y4m_frame_info_t *dest, y4m_frame_info_t *src)
 
323
{
 
324
  if ((dest == NULL) || (src == NULL)) return;
 
325
  /* copy info */
 
326
  y4m_copy_xtag_list(&(dest->x_tags), &(src->x_tags));
 
327
}
 
328
 
 
329
 
 
330
void y4m_fini_frame_info(y4m_frame_info_t *info)
 
331
{
 
332
  if (info == NULL) return;
 
333
  y4m_fini_xtag_list(&(info->x_tags));
 
334
}
 
335
 
 
336
 
 
337
 
 
338
/*************************************************************************
 
339
 *
 
340
 * Tag parsing
 
341
 *
 
342
 *************************************************************************/
 
343
 
 
344
int y4m_parse_stream_tags(char *s, y4m_stream_info_t *i)
 
345
{
 
346
  char *token, *value;
 
347
  char tag;
 
348
  int err;
 
349
 
 
350
  /* parse fields */
 
351
  for (token = strtok(s, Y4M_DELIM);
 
352
       token != NULL;
 
353
       token = strtok(NULL, Y4M_DELIM)) {
 
354
    if (token[0] == '\0') continue;   /* skip empty strings */
 
355
    tag = token[0];
 
356
    value = token + 1;
 
357
    switch (tag) {
 
358
    case 'W':  /* width */
 
359
      i->width = atoi(value);
 
360
      if (i->width <= 0) return Y4M_ERR_RANGE;
 
361
      break;
 
362
    case 'H':  /* height */
 
363
      i->height = atoi(value);
 
364
      if (i->height <= 0) return Y4M_ERR_RANGE;
 
365
      break;
 
366
    case 'F':  /* frame rate (fps) */
 
367
      if ((err = y4m_parse_ratio(&(i->framerate), value)) != Y4M_OK)
 
368
        return err;
 
369
      if (i->framerate.n < 0) return Y4M_ERR_RANGE;
 
370
      break;
 
371
    case 'I':  /* interlacing */
 
372
      switch (value[0]) {
 
373
      case 'p':  i->interlace = Y4M_ILACE_NONE; break;
 
374
      case 't':  i->interlace = Y4M_ILACE_TOP_FIRST; break;
 
375
      case 'b':  i->interlace = Y4M_ILACE_BOTTOM_FIRST; break;
 
376
      case '?':
 
377
      default:
 
378
        i->interlace = Y4M_UNKNOWN; break;
 
379
      }
 
380
      break;
 
381
    case 'A':  /* sample (pixel) aspect ratio */
 
382
      if ((err = y4m_parse_ratio(&(i->sampleaspect), value)) != Y4M_OK)
 
383
        return err;
 
384
      if (i->sampleaspect.n < 0) return Y4M_ERR_RANGE;
 
385
      break;
 
386
    case 'X':  /* 'X' meta-tag */
 
387
      if ((err = y4m_xtag_add(&(i->x_tags), token)) != Y4M_OK) return err;
 
388
      break;
 
389
    default:
 
390
      /* possible error on unknown options */
 
391
      if (_y4mparam_allow_unknown_tags) {
 
392
        /* unknown tags ok:  store in xtag list and warn... */
 
393
        if ((err = y4m_xtag_add(&(i->x_tags), token)) != Y4M_OK) return err;
 
394
        mp_msg(MSGT_DEMUX, MSGL_WARN, "Unknown stream tag encountered:  '%s'\n", token);
 
395
      } else {
 
396
        /* unknown tags are *not* ok */
 
397
        return Y4M_ERR_BADTAG;
 
398
      }
 
399
      break;
 
400
    }
 
401
  }
 
402
  /* Error checking... width and height must be known since we can't
 
403
   * parse without them
 
404
   */
 
405
  if( i->width == Y4M_UNKNOWN || i->height == Y4M_UNKNOWN )
 
406
          return Y4M_ERR_HEADER;
 
407
  /* ta da!  done. */
 
408
  return Y4M_OK;
 
409
}
 
410
 
 
411
 
 
412
 
 
413
static int y4m_parse_frame_tags(char *s, y4m_frame_info_t *i)
 
414
{
 
415
  char *token, *value;
 
416
  char tag;
 
417
  int err;
 
418
 
 
419
  /* parse fields */
 
420
  for (token = strtok(s, Y4M_DELIM);
 
421
       token != NULL;
 
422
       token = strtok(NULL, Y4M_DELIM)) {
 
423
    if (token[0] == '\0') continue;   /* skip empty strings */
 
424
    tag = token[0];
 
425
    value = token + 1;
 
426
    switch (tag) {
 
427
    case 'X':  /* 'X' meta-tag */
 
428
      if ((err = y4m_xtag_add(&(i->x_tags), token)) != Y4M_OK) return err;
 
429
      break;
 
430
    default:
 
431
      /* possible error on unknown options */
 
432
      if (_y4mparam_allow_unknown_tags) {
 
433
        /* unknown tags ok:  store in xtag list and warn... */
 
434
        if ((err = y4m_xtag_add(&(i->x_tags), token)) != Y4M_OK) return err;
 
435
        mp_msg(MSGT_DEMUX, MSGL_WARN, "Unknown frame tag encountered:  '%s'\n", token);
 
436
      } else {
 
437
        /* unknown tags are *not* ok */
 
438
        return Y4M_ERR_BADTAG;
 
439
      }
 
440
      break;
 
441
    }
 
442
  }
 
443
  /* ta da!  done. */
 
444
  return Y4M_OK;
 
445
}
 
446
 
 
447
 
 
448
 
 
449
 
 
450
 
 
451
/*************************************************************************
 
452
 *
 
453
 * Read/Write stream header
 
454
 *
 
455
 *************************************************************************/
 
456
 
 
457
 
 
458
int y4m_read_stream_header(stream_t *s, y4m_stream_info_t *i)
 
459
{
 
460
   char line[Y4M_LINE_MAX];
 
461
   char *p;
 
462
   int n;
 
463
   int err;
 
464
 
 
465
   /* read the header line */
 
466
   for (n = 0, p = line; n < Y4M_LINE_MAX; n++, p++) {
 
467
     if (y4m_read(s, p, 1))
 
468
       return Y4M_ERR_SYSTEM;
 
469
     if (*p == '\n') {
 
470
       *p = '\0';           /* Replace linefeed by end of string */
 
471
       break;
 
472
     }
 
473
   }
 
474
   if (n >= Y4M_LINE_MAX)
 
475
      return Y4M_ERR_HEADER;
 
476
   /* look for keyword in header */
 
477
   if (strncmp(line, Y4M_MAGIC, strlen(Y4M_MAGIC)))
 
478
    return Y4M_ERR_MAGIC;
 
479
   if ((err = y4m_parse_stream_tags(line + strlen(Y4M_MAGIC), i)) != Y4M_OK)
 
480
     return err;
 
481
 
 
482
   i->framelength = (i->height * i->width) * 3 / 2;
 
483
   return Y4M_OK;
 
484
}
 
485
 
 
486
 
 
487
#if 0
 
488
int y4m_write_stream_header(int fd, y4m_stream_info_t *i)
 
489
{
 
490
  char s[Y4M_LINE_MAX+1];
 
491
  int n;
 
492
  int err;
 
493
 
 
494
  y4m_ratio_reduce(&(i->framerate));
 
495
  y4m_ratio_reduce(&(i->sampleaspect));
 
496
  n = snprintf(s, sizeof(s), "%s W%d H%d F%d:%d I%s A%d:%d",
 
497
               Y4M_MAGIC,
 
498
               i->width,
 
499
               i->height,
 
500
               i->framerate.n, i->framerate.d,
 
501
               (i->interlace == Y4M_ILACE_NONE) ? "p" :
 
502
               (i->interlace == Y4M_ILACE_TOP_FIRST) ? "t" :
 
503
               (i->interlace == Y4M_ILACE_BOTTOM_FIRST) ? "b" : "?",
 
504
               i->sampleaspect.n, i->sampleaspect.d);
 
505
  if ((n < 0) || (n > Y4M_LINE_MAX)) return Y4M_ERR_HEADER;
 
506
  if ((err = y4m_snprint_xtags(s + n, sizeof(s) - n - 1, &(i->x_tags)))
 
507
      != Y4M_OK)
 
508
    return err;
 
509
  /* non-zero on error */
 
510
  return (y4m_write(fd, s, strlen(s)) ? Y4M_ERR_SYSTEM : Y4M_OK);
 
511
}
 
512
#endif
 
513
 
 
514
 
 
515
 
 
516
 
 
517
/*************************************************************************
 
518
 *
 
519
 * Read/Write frame header
 
520
 *
 
521
 *************************************************************************/
 
522
 
 
523
int y4m_read_frame_header(stream_t *s, y4m_frame_info_t *i)
 
524
{
 
525
  char line[Y4M_LINE_MAX];
 
526
  char *p;
 
527
  int n;
 
528
  ssize_t remain;
 
529
 
 
530
  /* This is more clever than read_stream_header...
 
531
     Try to read "FRAME\n" all at once, and don't try to parse
 
532
     if nothing else is there...
 
533
  */
 
534
  remain = y4m_read(s, line, sizeof(Y4M_FRAME_MAGIC));
 
535
  if (remain != 0)
 
536
  {
 
537
          /* A clean EOF should end exactly at a frame-boundary */
 
538
          if( remain == sizeof(Y4M_FRAME_MAGIC) )
 
539
                  return Y4M_ERR_EOF;
 
540
          else
 
541
                  return Y4M_ERR_SYSTEM;
 
542
  }
 
543
  if (strncmp(line, Y4M_FRAME_MAGIC, sizeof(Y4M_FRAME_MAGIC)-1))
 
544
    return Y4M_ERR_MAGIC;
 
545
  if (line[sizeof(Y4M_FRAME_MAGIC)-1] == '\n')
 
546
    return Y4M_OK; /* done -- no tags:  that was the end-of-line. */
 
547
 
 
548
  if (line[sizeof(Y4M_FRAME_MAGIC)-1] != Y4M_DELIM[0]) {
 
549
    return Y4M_ERR_MAGIC; /* wasn't a space -- what was it? */
 
550
  }
 
551
 
 
552
  /* proceed to get the tags... (overwrite the magic) */
 
553
  for (n = 0, p = line; n < Y4M_LINE_MAX; n++, p++) {
 
554
    if (y4m_read(s, p, 1))
 
555
      return Y4M_ERR_SYSTEM;
 
556
    if (*p == '\n') {
 
557
      *p = '\0';           /* Replace linefeed by end of string */
 
558
      break;
 
559
    }
 
560
  }
 
561
  if (n >= Y4M_LINE_MAX) return Y4M_ERR_HEADER;
 
562
  /* non-zero on error */
 
563
  return y4m_parse_frame_tags(line, i);
 
564
}
 
565
 
 
566
 
 
567
#if 0
 
568
int y4m_write_frame_header(int fd, y4m_frame_info_t *i)
 
569
{
 
570
  char s[Y4M_LINE_MAX+1];
 
571
  int n;
 
572
  int err;
 
573
 
 
574
  n = snprintf(s, sizeof(s), "%s", Y4M_FRAME_MAGIC);
 
575
  if ((n < 0) || (n > Y4M_LINE_MAX)) return Y4M_ERR_HEADER;
 
576
  if ((err = y4m_snprint_xtags(s + n, sizeof(s) - n - 1, &(i->x_tags)))
 
577
      != Y4M_OK)
 
578
    return err;
 
579
  /* non-zero on error */
 
580
  return (y4m_write(fd, s, strlen(s)) ? Y4M_ERR_SYSTEM : Y4M_OK);
 
581
}
 
582
#endif
 
583
 
 
584
 
 
585
 
 
586
/*************************************************************************
 
587
 *
 
588
 * Read/Write entire frame
 
589
 *
 
590
 *************************************************************************/
 
591
 
 
592
int y4m_read_frame(stream_t *s, y4m_stream_info_t *si,
 
593
                   y4m_frame_info_t *fi, unsigned char *yuv[3])
 
594
{
 
595
  int err;
 
596
  int w = si->width;
 
597
  int h = si->height;
 
598
 
 
599
  /* Read frame header */
 
600
  if ((err = y4m_read_frame_header(s, fi)) != Y4M_OK) return err;
 
601
  /* Read luminance scanlines */
 
602
  if (y4m_read(s, yuv[0], w*h)) return Y4M_ERR_SYSTEM;
 
603
  /* Read chrominance scanlines */
 
604
  if (y4m_read(s, yuv[1], w*h/4)) return Y4M_ERR_SYSTEM;
 
605
  if (y4m_read(s, yuv[2], w*h/4)) return Y4M_ERR_SYSTEM;
 
606
 
 
607
  return Y4M_OK;
 
608
}
 
609
 
 
610
 
 
611
 
 
612
#if 0
 
613
int y4m_write_frame(int fd, y4m_stream_info_t *si,
 
614
                    y4m_frame_info_t *fi, unsigned char *yuv[3])
 
615
{
 
616
  int err;
 
617
  int w = si->width;
 
618
  int h = si->height;
 
619
 
 
620
  /* Write frame header */
 
621
  if ((err = y4m_write_frame_header(fd, fi)) != Y4M_OK) return err;
 
622
  /* Write luminance,chrominance scanlines */
 
623
  if (y4m_write(fd, yuv[0], w*h) ||
 
624
      y4m_write(fd, yuv[1], w*h/4) ||
 
625
      y4m_write(fd, yuv[2], w*h/4))
 
626
    return Y4M_ERR_SYSTEM;
 
627
  return Y4M_OK;
 
628
}
 
629
#endif
 
630
 
 
631
 
 
632
/*************************************************************************
 
633
 *
 
634
 * Read/Write entire frame, (de)interleaved (to)from two separate fields
 
635
 *
 
636
 *************************************************************************/
 
637
 
 
638
#if 0
 
639
int y4m_read_fields(int fd, y4m_stream_info_t *si, y4m_frame_info_t *fi,
 
640
                    unsigned char *upper_field[3],
 
641
                    unsigned char *lower_field[3])
 
642
{
 
643
  int i, y, err;
 
644
  int width = si->width;
 
645
  int height = si->height;
 
646
 
 
647
  /* Read frame header */
 
648
  if ((err = y4m_read_frame_header(fd, fi)) != Y4M_OK) return err;
 
649
  /* Read Y', Cb, and Cr planes */
 
650
  for (i = 0; i < 3; i++) {
 
651
    unsigned char *srctop = upper_field[i];
 
652
    unsigned char *srcbot = lower_field[i];
 
653
    /* alternately write one line from each */
 
654
    for (y = 0; y < height; y += 2) {
 
655
      if (y4m_read(fd, srctop, width)) return Y4M_ERR_SYSTEM;
 
656
      srctop += width;
 
657
      if (y4m_read(fd, srcbot, width)) return Y4M_ERR_SYSTEM;
 
658
      srcbot += width;
 
659
    }
 
660
    /* for chroma, width/height are half as big */
 
661
    if (i == 0) {
 
662
      width /= 2;
 
663
      height /= 2;
 
664
    }
 
665
  }
 
666
  return Y4M_OK;
 
667
}
 
668
 
 
669
 
 
670
 
 
671
int y4m_write_fields(int fd, y4m_stream_info_t *si, y4m_frame_info_t *fi,
 
672
                     unsigned char *upper_field[3],
 
673
                     unsigned char *lower_field[3])
 
674
{
 
675
  int i, y, err;
 
676
  int width = si->width;
 
677
  int height = si->height;
 
678
 
 
679
  /* Write frame header */
 
680
  if ((err = y4m_write_frame_header(fd, fi)) != Y4M_OK) return err;
 
681
  /* Write Y', Cb, and Cr planes */
 
682
  for (i = 0; i < 3; i++) {
 
683
    unsigned char *srctop = upper_field[i];
 
684
    unsigned char *srcbot = lower_field[i];
 
685
    /* alternately write one line from each */
 
686
    for (y = 0; y < height; y += 2) {
 
687
      if (y4m_write(fd, srctop, width)) return Y4M_ERR_SYSTEM;
 
688
      srctop += width;
 
689
      if (y4m_write(fd, srcbot, width)) return Y4M_ERR_SYSTEM;
 
690
      srcbot += width;
 
691
    }
 
692
    /* for chroma, width/height are half as big */
 
693
    if (i == 0) {
 
694
      width /= 2;
 
695
      height /= 2;
 
696
    }
 
697
  }
 
698
  return Y4M_OK;
 
699
}
 
700
#endif
 
701
 
 
702
 
 
703
/*************************************************************************
 
704
 *
 
705
 * Handy logging of stream info
 
706
 *
 
707
 *************************************************************************/
 
708
 
 
709
void y4m_log_stream_info(const char *prefix, y4m_stream_info_t *i)
 
710
{
 
711
  char s[256];
 
712
 
 
713
  snprintf(s, sizeof(s), "  frame size:  ");
 
714
  if (i->width == Y4M_UNKNOWN)
 
715
    snprintf(s+strlen(s), sizeof(s)-strlen(s), "(?)x");
 
716
  else
 
717
    snprintf(s+strlen(s), sizeof(s)-strlen(s), "%dx", i->width);
 
718
  if (i->height == Y4M_UNKNOWN)
 
719
    snprintf(s+strlen(s), sizeof(s)-strlen(s), "(?) pixels ");
 
720
  else
 
721
    snprintf(s+strlen(s), sizeof(s)-strlen(s), "%d pixels ", i->height);
 
722
  if (i->framelength == Y4M_UNKNOWN)
 
723
    snprintf(s+strlen(s), sizeof(s)-strlen(s), "(? bytes)");
 
724
  else
 
725
    snprintf(s+strlen(s), sizeof(s)-strlen(s), "(%d bytes)", i->framelength);
 
726
  mp_msg(MSGT_DEMUX, MSGL_V, "%s%s\n", prefix, s);
 
727
  if ((i->framerate.n == 0) && (i->framerate.d == 0))
 
728
    mp_msg(MSGT_DEMUX, MSGL_V, "%s  frame rate:  ??? fps\n", prefix);
 
729
  else
 
730
    mp_msg(MSGT_DEMUX, MSGL_V, "%s  frame rate:  %d/%d fps (~%f)\n", prefix,
 
731
              i->framerate.n, i->framerate.d,
 
732
              (double) i->framerate.n / (double) i->framerate.d);
 
733
  mp_msg(MSGT_DEMUX, MSGL_V, "%s   interlace:  %s\n", prefix,
 
734
          (i->interlace == Y4M_ILACE_NONE) ? "none/progressive" :
 
735
          (i->interlace == Y4M_ILACE_TOP_FIRST) ? "top-field-first" :
 
736
          (i->interlace == Y4M_ILACE_BOTTOM_FIRST) ? "bottom-field-first" :
 
737
          "anyone's guess");
 
738
  if ((i->sampleaspect.n == 0) && (i->sampleaspect.d == 0))
 
739
    mp_msg(MSGT_DEMUX, MSGL_V, "%ssample aspect ratio:  ?:?\n", prefix);
 
740
  else
 
741
    mp_msg(MSGT_DEMUX, MSGL_V, "%ssample aspect ratio:  %d:%d\n", prefix,
 
742
              i->sampleaspect.n, i->sampleaspect.d);
 
743
}
 
744
 
 
745
 
 
746
/*************************************************************************
 
747
 *
 
748
 * Convert error code to string
 
749
 *
 
750
 *************************************************************************/
 
751
 
 
752
const char *y4m_strerr(int err)
 
753
{
 
754
  switch (err) {
 
755
  case Y4M_OK:          return "no error";
 
756
  case Y4M_ERR_RANGE:   return "parameter out of range";
 
757
  case Y4M_ERR_SYSTEM:  return "stream ended unexpectedly (failed read/write)";
 
758
  case Y4M_ERR_HEADER:  return "bad stream or frame header";
 
759
  case Y4M_ERR_BADTAG:  return "unknown header tag";
 
760
  case Y4M_ERR_MAGIC:   return "bad header magic";
 
761
  case Y4M_ERR_XXTAGS:  return "too many xtags";
 
762
  case Y4M_ERR_EOF:     return "end-of-file";
 
763
  default:
 
764
    return "unknown error code";
 
765
  }
 
766
}