~ubuntu-branches/ubuntu/precise/mpeg4ip/precise

« back to all changes in this revision

Viewing changes to player/src/mp4_bytestream.h

  • Committer: Bazaar Package Importer
  • Author(s): Mario Limonciello
  • Date: 2008-01-12 15:59:56 UTC
  • Revision ID: james.westby@ubuntu.com-20080112155956-1vznw5njidvrh649
Tags: upstream-1.6dfsg
ImportĀ upstreamĀ versionĀ 1.6dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Mozilla Public
 
3
 * License Version 1.1 (the "License"); you may not use this file
 
4
 * except in compliance with the License. You may obtain a copy of
 
5
 * the License at http://www.mozilla.org/MPL/
 
6
 * 
 
7
 * Software distributed under the License is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the License for the specific language governing
 
10
 * rights and limitations under the License.
 
11
 * 
 
12
 * The Original Code is MPEG4IP.
 
13
 * 
 
14
 * The Initial Developer of the Original Code is Cisco Systems Inc.
 
15
 * Portions created by Cisco Systems Inc. are
 
16
 * Copyright (C) Cisco Systems Inc. 2000, 2001.  All Rights Reserved.
 
17
 * 
 
18
 * Contributor(s): 
 
19
 *              Bill May        wmay@cisco.com
 
20
 */
 
21
/*
 
22
 * mp4_bytestream.h - provides bytestream access to quicktime files
 
23
 */
 
24
 
 
25
#ifndef __MP4_BYTESTREAM_H__
 
26
#define __MP4_BYTESTREAM_H__
 
27
#include <mp4.h>
 
28
#include <ismacryplib.h>
 
29
#include "our_bytestream.h"
 
30
#include "mp4_file.h"
 
31
#include "player_util.h"
 
32
 
 
33
//Uncomment these #defines to dump buffers to file.
 
34
//#define OUTPUT_TO_FILE 1
 
35
//#define ISMACRYP_DEBUG 1
 
36
 
 
37
/*
 
38
 * CMp4ByteStreamBase provides base class access to quicktime files.
 
39
 * Most functions are shared between audio and video.
 
40
 */
 
41
class CMp4ByteStream : public COurInByteStream
 
42
{
 
43
 public:
 
44
  CMp4ByteStream(CMp4File *parent,
 
45
                 MP4TrackId track,
 
46
                 const char *type,
 
47
                 bool has_video);
 
48
  ~CMp4ByteStream();
 
49
  int eof(void);
 
50
  void reset(void);
 
51
  bool start_next_frame(uint8_t **buffer,
 
52
                        uint32_t *buflen,
 
53
                        frame_timestamp_t *pts,
 
54
                        void **ud);
 
55
  void used_bytes_for_frame(uint32_t bytes);
 
56
  int can_skip_frame(void) { return 1; };
 
57
  bool skip_next_frame(frame_timestamp_t *ts, int *hasSyncFrame, 
 
58
                      uint8_t **buffer,
 
59
                      uint32_t *buflen, void **ud);
 
60
  void check_for_end_of_frame(void);
 
61
  double get_max_playtime(void);
 
62
 
 
63
  const char *get_inuse_kms_uri(void);
 
64
 
 
65
  void play(uint64_t start);
 
66
 
 
67
  u_int8_t *get_buffer() {return m_buffer; }
 
68
  void set_buffer(u_int8_t *buffer) {
 
69
    memset(m_buffer, 0, m_max_frame_size * sizeof(u_int8_t));
 
70
  }
 
71
  uint32_t get_this_frame_size() {return m_this_frame_size;}
 
72
  void set_this_frame_size(uint32_t frame_size)
 
73
    {m_this_frame_size = frame_size;}
 
74
#ifdef ISMACRYP_DEBUG
 
75
  FILE *my_enc_file;
 
76
  FILE *my_unenc_file;
 
77
  FILE *my_unenc_file2;
 
78
#endif
 
79
 protected:
 
80
#ifdef OUTPUT_TO_FILE
 
81
  FILE *m_output_file;
 
82
#endif
 
83
  void read_frame(uint32_t frame, frame_timestamp_t *ts);
 
84
  CMp4File *m_parent;
 
85
  bool m_eof;
 
86
  MP4TrackId m_track;
 
87
  MP4SampleId m_frames_max;
 
88
  uint32_t m_sample_freq;
 
89
 
 
90
  MP4SampleId m_frame_on;
 
91
  uint64_t m_frame_on_ts;
 
92
  uint32_t m_frame_on_sample_ts;
 
93
  int m_frame_on_has_sync;
 
94
  
 
95
  MP4SampleId m_frame_in_buffer;
 
96
  uint64_t m_frame_in_buffer_ts;
 
97
  int m_frame_in_buffer_has_sync;
 
98
 
 
99
  u_int32_t m_max_frame_size;
 
100
  u_int8_t *m_buffer;
 
101
  uint32_t m_byte_on;
 
102
  uint32_t m_this_frame_size;
 
103
  uint64_t m_total;
 
104
  void set_timebase(MP4SampleId frame);
 
105
  double m_max_time;
 
106
  bool m_has_video;
 
107
};
 
108
 
 
109
/*
 
110
 * CMp4VideoByteStream is for video streams.  It is inherited from
 
111
 * CMp4ByteStreamBase.
 
112
 */
 
113
class CMp4VideoByteStream : public CMp4ByteStream
 
114
{
 
115
 public:
 
116
  CMp4VideoByteStream(CMp4File *parent,
 
117
                      MP4TrackId track) :
 
118
    CMp4ByteStream(parent, track, "video", 1) {};
 
119
};
 
120
 
 
121
class CMp4TextByteStream : public CMp4ByteStream
 
122
{
 
123
 public:
 
124
  CMp4TextByteStream(CMp4File *parent, MP4TrackId track,
 
125
                     const char *base_url) :
 
126
    CMp4ByteStream(parent, track, "cntl", 0) {
 
127
    m_base_url = base_url;
 
128
  };
 
129
  ~CMp4TextByteStream(void) {
 
130
    //CHECK_AND_FREE(m_base_url);
 
131
  };
 
132
  bool start_next_frame(uint8_t **buffer, 
 
133
                        uint32_t *buflen,
 
134
                        frame_timestamp_t *ptr,
 
135
                        void **ud);
 
136
 protected:
 
137
  const char *m_base_url;
 
138
};
 
139
 
 
140
                          
 
141
class CMp4H264VideoByteStream : public CMp4VideoByteStream
 
142
{
 
143
 public:
 
144
  CMp4H264VideoByteStream(CMp4File *parent, 
 
145
                          MP4TrackId track) :
 
146
    CMp4VideoByteStream(parent, track) {
 
147
    m_translate_buffer = NULL;
 
148
    m_translate_buffer_size = 0;
 
149
    m_buflen_size = 0;
 
150
  };
 
151
  bool start_next_frame(uint8_t **buffer,
 
152
                        uint32_t *buflen,
 
153
                        frame_timestamp_t *pts,
 
154
                        void **ud);
 
155
 protected:
 
156
  uint32_t read_nal_size(uint8_t *buffer) {
 
157
    if (m_buflen_size == 1) {
 
158
      return *buffer;
 
159
    } else if (m_buflen_size == 2) {
 
160
      return (buffer[0] << 8) | buffer[1];
 
161
    } else if (m_buflen_size == 3) {
 
162
      return (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
 
163
    }
 
164
    return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
 
165
  };
 
166
  uint8_t *m_translate_buffer;
 
167
  uint32_t m_translate_buffer_size;
 
168
  uint32_t m_buflen_size;
 
169
};
 
170
                          
 
171
/*
 
172
 * CMp4AudioByteStream is for audio streams.  It is inherited from
 
173
 * CMp4ByteStreamBase.
 
174
 */
 
175
class CMp4AudioByteStream : public CMp4ByteStream
 
176
{
 
177
 public:
 
178
  CMp4AudioByteStream(CMp4File *parent,
 
179
                      MP4TrackId track) :
 
180
    CMp4ByteStream(parent, track, "audio", 0) {};
 
181
 
 
182
};
 
183
 
 
184
/*
 
185
 * CMp4EncVideoByteStream is for encrypted video streams.  
 
186
 * It is inherited from CMp4VideoByteStreamBase.
 
187
 */
 
188
class CMp4EncVideoByteStream : public CMp4VideoByteStream
 
189
{
 
190
 public:
 
191
  CMp4EncVideoByteStream(CMp4File   *parent,
 
192
                         MP4TrackId track,
 
193
                         uint64_t   IVLength ) :
 
194
    CMp4VideoByteStream(parent, track) {
 
195
    ismacryp_rc_t rc = ismacrypInitSession(&m_ismaCryptSId,KeyTypeVideo);
 
196
 
 
197
    if (rc != ismacryp_rc_ok ) {
 
198
      player_error_message("can't initialize video ismacryp session rc: %u\n", rc);
 
199
    }
 
200
    else {
 
201
       rc = ismacrypSetIVLength(m_ismaCryptSId, (uint8_t)IVLength);
 
202
       if( rc != ismacryp_rc_ok )
 
203
         player_error_message(
 
204
          "can't set IV length for ismacryp decode session %d, rc: %u\n",
 
205
          m_ismaCryptSId, rc);
 
206
    }
 
207
  };
 
208
  ~CMp4EncVideoByteStream() {
 
209
    ismacryp_rc_t rc = ismacrypEndSession(m_ismaCryptSId);
 
210
    if (rc != ismacryp_rc_ok ) {
 
211
       player_error_message(
 
212
          "could not end video ismacryp session %d, rc: %u\n",
 
213
          m_ismaCryptSId, rc);
 
214
     }
 
215
  }
 
216
  bool start_next_frame(uint8_t **buffer,
 
217
                        uint32_t *buflen,
 
218
                        frame_timestamp_t *ts,
 
219
                        void **ud);
 
220
  ismacryp_session_id_t m_ismaCryptSId; // eventually make it private
 
221
                                        // and add accessor function
 
222
};  
 
223
 
 
224
/*
 
225
 * CMp4EncH264VideoByteStream is for encrypted H264 AVC video streams.  
 
226
 * It is inherited from CMp4VideoByteStreamBase.
 
227
 */
 
228
class CMp4EncH264VideoByteStream : public CMp4VideoByteStream
 
229
{
 
230
 public:
 
231
  CMp4EncH264VideoByteStream(CMp4File   *parent,
 
232
                         MP4TrackId track,
 
233
                         uint64_t   IVLength ) :
 
234
    CMp4VideoByteStream(parent, track) {
 
235
    m_translate_buffer = NULL;
 
236
    m_translate_buffer_size = 0;
 
237
    m_buflen_size = 0;
 
238
    m_kms_uri = NULL;
 
239
    ismacryp_rc_t rc = ismacrypInitSession(&m_ismaCryptSId,KeyTypeVideo);
 
240
 
 
241
    if (rc != ismacryp_rc_ok ) {
 
242
      player_error_message("can't initialize video ismacryp session rc: %u\n", rc);
 
243
    }
 
244
    else {
 
245
       rc = ismacrypSetIVLength(m_ismaCryptSId, (uint8_t)IVLength);
 
246
       if( rc != ismacryp_rc_ok ) {
 
247
         player_error_message(
 
248
          "can't set IV length for ismacryp decode session %d, rc: %u\n",
 
249
          m_ismaCryptSId, rc);
 
250
       }
 
251
      ismacrypGetKMSUri(m_ismaCryptSId, &m_kms_uri);
 
252
    }
 
253
  };
 
254
  ~CMp4EncH264VideoByteStream() {
 
255
    ismacryp_rc_t rc = ismacrypEndSession(m_ismaCryptSId);
 
256
    if (rc != ismacryp_rc_ok ) {
 
257
       player_error_message(
 
258
          "could not end video ismacryp session %d, rc: %u\n",
 
259
          m_ismaCryptSId, rc);
 
260
     }
 
261
     if (m_kms_uri != NULL)
 
262
        free((void *)m_kms_uri);
 
263
  }
 
264
  bool start_next_frame(uint8_t **buffer,
 
265
                        uint32_t *buflen,
 
266
                        frame_timestamp_t *ts,
 
267
                        void **ud);
 
268
 
 
269
  const char *get_inuse_kms_uri() { return((const char *)m_kms_uri); }
 
270
 
 
271
  ismacryp_session_id_t m_ismaCryptSId; // eventually make it private
 
272
                                        // and add accessor function
 
273
 
 
274
 protected:
 
275
  uint32_t read_nal_size(uint8_t *buffer) {
 
276
    if (m_buflen_size == 1) {
 
277
      return *buffer;
 
278
    } else if (m_buflen_size == 2) {
 
279
      return (buffer[0] << 8) | buffer[1];
 
280
    } else if (m_buflen_size == 3) {
 
281
      return (buffer[0] << 16) | (buffer[1] << 8) | buffer[2];
 
282
    }
 
283
    return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3];
 
284
  };
 
285
  uint8_t *m_translate_buffer;
 
286
  uint32_t m_translate_buffer_size;
 
287
  uint32_t m_buflen_size;
 
288
  const char *m_kms_uri;
 
289
};  
 
290
 
 
291
/*
 
292
 * CMp4EncAudioByteStream is for encrypted audio streams.  
 
293
 * It is inherited from CMp4AudioByteStreamBase.
 
294
 */
 
295
class CMp4EncAudioByteStream : public CMp4AudioByteStream
 
296
{
 
297
 public:
 
298
  CMp4EncAudioByteStream(CMp4File   *parent,
 
299
                         MP4TrackId track,
 
300
                         uint64_t   IVLength ) :
 
301
    CMp4AudioByteStream(parent, track) {
 
302
    ismacryp_rc_t rc = ismacrypInitSession(&m_ismaCryptSId,KeyTypeAudio);
 
303
    if ( rc != ismacryp_rc_ok ) {
 
304
      player_error_message("can't initialize audio ismacryp session rc: %u\n", rc);
 
305
    }
 
306
    else {
 
307
       rc = ismacrypSetIVLength(m_ismaCryptSId, (uint8_t)IVLength);
 
308
       if( rc != ismacryp_rc_ok )
 
309
         player_error_message(
 
310
          "can't set IV length for ismacryp decode session %d, rc: %u\n",
 
311
          m_ismaCryptSId, rc);
 
312
    }
 
313
  };
 
314
  ~CMp4EncAudioByteStream() {
 
315
    ismacryp_rc_t rc = ismacrypEndSession(m_ismaCryptSId);
 
316
    if ( rc != ismacryp_rc_ok) {
 
317
       player_error_message(
 
318
          "could not end audio ismacryp session %d, rc: %u\n",
 
319
          m_ismaCryptSId, rc);
 
320
    }
 
321
  };
 
322
  bool start_next_frame(uint8_t **buffer,
 
323
                        uint32_t *buflen,
 
324
                        frame_timestamp_t *ts,
 
325
                        void **ud); 
 
326
 
 
327
  ismacryp_session_id_t m_ismaCryptSId; // eventually make it private
 
328
                                        // and add accessor function
 
329
};
 
330
 
 
331
#ifdef _WIN32
 
332
DEFINE_MESSAGE_MACRO(mp4f_message, "mp4file")
 
333
#else
 
334
#define mp4f_message(loglevel, fmt...) message(loglevel, "mp4file", fmt)
 
335
#endif
 
336
 
 
337
#endif
 
338
 
 
339
 
 
340