summaryrefslogtreecommitdiff
path: root/tools/mpeg.c
blob: 029f8ede445ea0ccd8b7c3aa397f2fa6d0052e79 (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
/*
 * mpeg.c:
 *
 * See the main source file 'xineliboutput.c' for copyright information and
 * how to reach the author.
 *
 * $Id: mpeg.c,v 1.2 2008-06-11 15:51:19 phintuka Exp $
 *
 */

#include "mpeg.h"


const char * const picture_type_str[] = {
  "(none)",
  "I-Frame",
  "B-Frame",
  "P-Frame"
};

int mpeg2_get_picture_type(const uint8_t *buf, int len)
{
  int i;
  for (i = 0; i < len-5; i++) {
    if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1) {
      switch (buf[i + 3]) {
        case SC_PICTURE:
	  return (buf[i + 5] >> 3) & 0x07;
      }
    }
  }
  return NO_PICTURE;
}

int mpeg2_get_video_size(const uint8_t *buf, int len, video_size_t *size)
{
  int i;
  for (i = 0; i < len-6; i++) {
    if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1) {
      if (buf[i + 3] == SC_SEQUENCE) {
	static const mpeg_rational_t mpeg2_aspect[16] = {
	  {0,1}, {1,1}, {4,3}, {16,9}, {221,100},
	  {0,1}, {0,1}, {0,1}, { 0,1}, {  0,1},
	  {0,1}, {0,1}, {0,1}, { 0,1}, {  0,1},
	  {0,1},
	};

	int d = (buf[i+4] << 16) | (buf[i+5] << 8) | buf[i+6];
	int a = buf[i+7] >> 4;

	size->width  = (d >> 12);
	size->height = (d & 0xfff);

	memcpy(&size->pixel_aspect, &mpeg2_aspect[a], sizeof(mpeg_rational_t));
	size->pixel_aspect.num *= size->height;
	size->pixel_aspect.den *= size->width;

	return 1;
      }
    }
  }
  return 0;
}