aboutsummaryrefslogtreecommitdiffhomepage
path: root/libmpdemux/parse_mp4.c
blob: bdae91a0fe4722edb459b4f83edad4afc3b029e2 (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
/*
 * MP4 file format parser code
 *
 * Copyright (C) 2002 Felix Buenemann <atmosfear at users.sourceforge.net>
 * Code inspired by libmp4 from http://mpeg4ip.sourceforge.net/.
 *
 * This file is part of MPlayer.
 *
 * MPlayer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * MPlayer 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include "parse_mp4.h"
#include "mp_msg.h"
#include "stream/stream.h"

//#define MP4_DUMPATOM

#define MP4_DL MSGL_V
#define freereturn(a,b) free(a); return b

int mp4_read_descr_len(stream_t *s) {
  uint8_t b;
  uint8_t numBytes = 0;
  uint32_t length = 0;

  do {
    b = stream_read_char(s);
    numBytes++;
    length = (length << 7) | (b & 0x7F);
  } while ((b & 0x80) && numBytes < 4);

  //printf("MP4 read desc len: %d\n", length);
  return length;
}

/* parse the data part of MP4 esds atoms */
int mp4_parse_esds(unsigned char *data, int datalen, esds_t *esds) {
  /* create memory stream from data */
  stream_t *s = new_memory_stream(data, datalen);
  uint16_t len;
#ifdef MP4_DUMPATOM
  {int i;
  printf("ESDS Dump (%dbyte):\n", datalen);
  for(i = 0; i < datalen; i++)
    printf("%02X ", data[i]);
  printf("\nESDS Dumped\n");}
#endif
  memset(esds, 0, sizeof(esds_t));

  esds->version = stream_read_char(s);
  esds->flags = stream_read_int24(s);
  mp_msg(MSGT_DEMUX, MP4_DL,
      "ESDS MPEG4 version: %d  flags: 0x%06X\n",
      esds->version, esds->flags);

  /* get and verify ES_DescrTag */
  if (stream_read_char(s) == MP4ESDescrTag) {
    /* read length */
    len = mp4_read_descr_len(s);

    esds->ESId = stream_read_word(s);
    esds->streamPriority = stream_read_char(s);
    mp_msg(MSGT_DEMUX, MP4_DL,
      	"ESDS MPEG4 ES Descriptor (%dBytes):\n"
	" -> ESId: %d\n"
	" -> streamPriority: %d\n",
	len, esds->ESId, esds->streamPriority);

    if (len < (5 + 15)) {
      freereturn(s,1);
    }
  } else {
    esds->ESId = stream_read_word(s);
    mp_msg(MSGT_DEMUX, MP4_DL,
      	"ESDS MPEG4 ES Descriptor (%dBytes):\n"
	" -> ESId: %d\n", 2, esds->ESId);
  }

  /* get and verify DecoderConfigDescrTab */
  if (stream_read_char(s) != MP4DecConfigDescrTag) {
    freereturn(s,1);
  }

  /* read length */
  len = mp4_read_descr_len(s);

  esds->objectTypeId = stream_read_char(s);
  esds->streamType = stream_read_char(s);
  esds->bufferSizeDB = stream_read_int24(s);
  esds->maxBitrate = stream_read_dword(s);
  esds->avgBitrate = stream_read_dword(s);
  mp_msg(MSGT_DEMUX, MP4_DL,
      "ESDS MPEG4 Decoder Config Descriptor (%dBytes):\n"
      " -> objectTypeId: %d\n"
      " -> streamType: 0x%02X\n"
      " -> bufferSizeDB: 0x%06X\n"
      " -> maxBitrate: %.3fkbit/s\n"
      " -> avgBitrate: %.3fkbit/s\n",
      len, esds->objectTypeId, esds->streamType,
      esds->bufferSizeDB, esds->maxBitrate/1000.0,
      esds->avgBitrate/1000.0);

  esds->decoderConfigLen=0;

  if (len < 15) {
    freereturn(s,0);
  }

  /* get and verify DecSpecificInfoTag */
  if (stream_read_char(s) != MP4DecSpecificDescrTag) {
    freereturn(s,0);
  }

  /* read length */
  esds->decoderConfigLen = len = mp4_read_descr_len(s);

  esds->decoderConfig = malloc(esds->decoderConfigLen);
  if (esds->decoderConfig) {
    stream_read(s, esds->decoderConfig, esds->decoderConfigLen);
  } else {
    esds->decoderConfigLen = 0;
  }
  mp_msg(MSGT_DEMUX, MP4_DL,
      "ESDS MPEG4 Decoder Specific Descriptor (%dBytes)\n", len);

  /* get and verify SLConfigDescrTag */
  if(stream_read_char(s) != MP4SLConfigDescrTag) {
    freereturn(s,0);
  }

  /* Note: SLConfig is usually constant value 2, size 1Byte */
  esds->SLConfigLen = len = mp4_read_descr_len(s);
  esds->SLConfig = malloc(esds->SLConfigLen);
  if (esds->SLConfig) {
    stream_read(s, esds->SLConfig, esds->SLConfigLen);
  } else {
    esds->SLConfigLen = 0;
  }
  mp_msg(MSGT_DEMUX, MP4_DL,
      "ESDS MPEG4 Sync Layer Config Descriptor (%dBytes)\n"
      " -> predefined: %d\n", len, esds->SLConfig[0]);

  /* will skip the remainder of the atom */
  freereturn(s,0);

}

/* cleanup all mem occupied by mp4_parse_esds */
void mp4_free_esds(esds_t *esds) {
  if(esds->decoderConfigLen)
    free(esds->decoderConfig);
  if(esds->SLConfigLen)
    free(esds->SLConfig);
}

#undef freereturn
#undef MP4_DL