~ubuntu-branches/ubuntu/trusty/mpeg4ip/trusty

« back to all changes in this revision

Viewing changes to player/src/avi_file.cpp

  • 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
 * avi_file.cpp - provides generic class for avi file access control.
 
23
 * file access is then used by avi audio and video bytestreams.
 
24
 */
 
25
#include "mpeg4ip.h"
 
26
#include "player_session.h"
 
27
#include "player_media.h"
 
28
#include "player_util.h"
 
29
#include "media_utils.h"
 
30
#include "avi_bytestream.h"
 
31
#include "avi_file.h"
 
32
#include "codec_plugin_private.h"
 
33
#include "our_config_file.h"
 
34
 
 
35
static void close_avi_file (void *data)
 
36
{
 
37
  CAviFile *Avifile1 = (CAviFile *)data;
 
38
  if (Avifile1 != NULL) {
 
39
    delete Avifile1;
 
40
    Avifile1 = NULL;
 
41
  }
 
42
}
 
43
 
 
44
/*
 
45
 * Create the media for the quicktime file, and set up some session stuff.
 
46
 */
 
47
int create_media_for_avi_file (CPlayerSession *psptr, 
 
48
                               const char *name,
 
49
                               int have_audio_driver,
 
50
                               control_callback_vft_t *cc_vft)
 
51
{
 
52
  CAviFile *Avifile1 = NULL;
 
53
  avi_t *avi;
 
54
  CPlayerMedia *mptr;
 
55
  avi = AVI_open_input_file(name, 1);
 
56
  if (avi == NULL) {
 
57
    psptr->set_message("%s", AVI_strerror());
 
58
    player_error_message("%s", AVI_strerror());
 
59
    return (-1);
 
60
  }
 
61
 
 
62
  int video_count = 1;
 
63
  codec_plugin_t *plugin;
 
64
  video_query_t vq;
 
65
 
 
66
  const char *codec_name = AVI_video_compressor(avi);
 
67
  player_debug_message("Trying avi video codec %s", codec_name);
 
68
  plugin = check_for_video_codec(STREAM_TYPE_AVI_FILE,
 
69
                                 codec_name, 
 
70
                                 NULL,
 
71
                                 -1,
 
72
                                 -1,
 
73
                                 NULL,
 
74
                                 0, 
 
75
                                 &config);
 
76
  if (plugin == NULL) {
 
77
    video_count = 0;
 
78
  } else {
 
79
    vq.track_id = 1;
 
80
    vq.stream_type = STREAM_TYPE_AVI_FILE;
 
81
    vq.compressor = codec_name;
 
82
    vq.type = -1;
 
83
    vq.profile = -1;
 
84
    vq.fptr = NULL;
 
85
    vq.h = AVI_video_height(avi);
 
86
    vq.w = AVI_video_width(avi);
 
87
    vq.frame_rate = AVI_video_frame_rate(avi);
 
88
    vq.config = NULL;
 
89
    vq.config_len = 0;
 
90
    vq.enabled = 0;
 
91
    vq.reference = NULL;
 
92
  }
 
93
 
 
94
  int have_audio = 0;
 
95
  int audio_count = 0;
 
96
  audio_query_t aq;
 
97
 
 
98
  if (AVI_audio_bytes(avi) != 0) {
 
99
    have_audio = 1;
 
100
    plugin = check_for_audio_codec(STREAM_TYPE_AVI_FILE,
 
101
                                   NULL,
 
102
                                   NULL,
 
103
                                   AVI_audio_format(avi), 
 
104
                                   -1, 
 
105
                                   NULL, 
 
106
                                   0,
 
107
                                   &config);
 
108
    if (plugin != NULL) {
 
109
      audio_count = 1;
 
110
      aq.track_id = 1;
 
111
      aq.stream_type = STREAM_TYPE_AVI_FILE;
 
112
      aq.compressor = NULL;
 
113
      aq.type = AVI_audio_format(avi);
 
114
      aq.profile = -1;
 
115
      aq.fptr = NULL;
 
116
      aq.sampling_freq = AVI_audio_rate(avi);
 
117
      aq.chans = AVI_audio_channels(avi);
 
118
      aq.config = NULL;
 
119
      aq.config_len = 0;
 
120
      aq.enabled = 0;
 
121
      aq.reference = NULL;
 
122
    }
 
123
  }
 
124
 
 
125
  if (cc_vft != NULL && cc_vft->media_list_query != NULL) {
 
126
    (cc_vft->media_list_query)(psptr, video_count, &vq, audio_count, &aq,
 
127
                               0, NULL);
 
128
  } else {
 
129
    if (video_count != 0) vq.enabled = 1;
 
130
    if (audio_count != 0) aq.enabled = 1;
 
131
  }
 
132
 
 
133
 
 
134
  if ((video_count == 0 || vq.enabled == 0) && 
 
135
      (audio_count == 0 || aq.enabled == 0)) {
 
136
    psptr->set_message("No audio or video tracks enabled or playable");
 
137
    AVI_close(avi);
 
138
    return -1;
 
139
  }
 
140
  
 
141
  Avifile1 = new CAviFile(name, avi, vq.enabled, audio_count);
 
142
  psptr->set_media_close_callback(close_avi_file, Avifile1);
 
143
 
 
144
  if (video_count != 0 && vq.enabled) {
 
145
    mptr = new CPlayerMedia(psptr, VIDEO_SYNC);
 
146
    if (mptr == NULL) {
 
147
      return (-1);
 
148
    }
 
149
  
 
150
    video_info_t *vinfo = MALLOC_STRUCTURE(video_info_t);
 
151
    if (vinfo == NULL) 
 
152
      return (-1);
 
153
    vinfo->height = vq.h;
 
154
    vinfo->width = vq.w;
 
155
    player_debug_message("avi file h %d w %d frame rate %g", 
 
156
                         vinfo->height,
 
157
                         vinfo->width,
 
158
                         vq.frame_rate);
 
159
 
 
160
    plugin = check_for_video_codec(STREAM_TYPE_AVI_FILE,
 
161
                                   codec_name, 
 
162
                                   NULL,
 
163
                                   -1,
 
164
                                   -1,
 
165
                                   NULL,
 
166
                                   0,
 
167
                                   &config);
 
168
    int ret;
 
169
    ret = mptr->create_video_plugin(plugin,
 
170
                                    STREAM_TYPE_AVI_FILE,
 
171
                                    codec_name,
 
172
                                    -1,
 
173
                                    -1,
 
174
                                    NULL,
 
175
                                    vinfo,
 
176
                                    NULL,
 
177
                                    0);
 
178
    if (ret < 0) {
 
179
      psptr->set_message("Failed to create video plugin %s", 
 
180
               codec_name);
 
181
      player_error_message("Failed to create plugin data");
 
182
      delete mptr;
 
183
      return -1;
 
184
    }
 
185
    CAviVideoByteStream *vbyte = new CAviVideoByteStream(Avifile1);
 
186
    if (vbyte == NULL) {
 
187
      delete mptr;
 
188
      return (-1);
 
189
    }
 
190
    vbyte->config(AVI_video_frames(avi), vq.frame_rate);
 
191
    ret = mptr->create_media("video", vbyte);
 
192
    if (ret != 0) {
 
193
      return (-1);
 
194
    }
 
195
  }
 
196
    
 
197
  int seekable = 1;
 
198
  if (have_audio_driver > 0 && audio_count > 0 && aq.enabled != 0) {
 
199
    plugin = check_for_audio_codec(STREAM_TYPE_AVI_FILE,
 
200
                                   NULL,
 
201
                                   NULL,
 
202
                                   aq.type,
 
203
                                   -1, 
 
204
                                   NULL, 
 
205
                                   0,
 
206
                                   &config);
 
207
    CAviAudioByteStream *abyte;
 
208
    mptr = new CPlayerMedia(psptr, AUDIO_SYNC);
 
209
    if (mptr == NULL) {
 
210
      return (-1);
 
211
    }
 
212
    audio_info_t *ainfo;
 
213
    ainfo = MALLOC_STRUCTURE(audio_info_t);
 
214
    ainfo->freq = aq.sampling_freq;
 
215
    ainfo->chans = aq.chans;
 
216
    ainfo->bitspersample = AVI_audio_bits(avi); 
 
217
 
 
218
  
 
219
    int ret;
 
220
    ret = mptr->create_audio_plugin(plugin, 
 
221
                                    aq.stream_type,
 
222
                                    aq.compressor,
 
223
                                    aq.type,
 
224
                                    aq.profile,
 
225
                                    NULL, 
 
226
                                    ainfo,
 
227
                                    NULL, 
 
228
                                    0);
 
229
    if (ret < 0) {
 
230
      delete mptr;
 
231
      player_error_message("Couldn't create audio from plugin %s", 
 
232
                           plugin->c_name);
 
233
      return -1;
 
234
    }
 
235
    abyte = new CAviAudioByteStream(Avifile1);
 
236
 
 
237
    ret = mptr->create_media("audio", abyte);
 
238
    if (ret != 0) {
 
239
      return (-1);
 
240
    }
 
241
    seekable = 0;
 
242
  } 
 
243
  psptr->session_set_seekable(seekable);
 
244
 
 
245
  if (audio_count == 0 && have_audio != 0) {
 
246
    psptr->set_message("Unknown Audio Codec in avi file ");
 
247
    return (1);
 
248
  }
 
249
  if (video_count != 1) {
 
250
    psptr->set_message("Unknown Video Codec %s in avi file",
 
251
             codec_name);
 
252
    return (1);
 
253
  }
 
254
  return (0);
 
255
}
 
256
 
 
257
CAviFile::CAviFile (const char *name, avi_t *avi,
 
258
                    int at, int vt)
 
259
{
 
260
  m_name = strdup(name);
 
261
  m_file = avi;
 
262
  m_file_mutex = SDL_CreateMutex();
 
263
  m_video_tracks = vt;
 
264
  m_audio_tracks = at;
 
265
}
 
266
 
 
267
CAviFile::~CAviFile (void)
 
268
{
 
269
  free(m_name);
 
270
  m_name = NULL;
 
271
  AVI_close(m_file);
 
272
  if (m_file_mutex) {
 
273
    SDL_DestroyMutex(m_file_mutex);
 
274
    m_file_mutex = NULL;
 
275
  }
 
276
}
 
277
 
 
278
/* end file avi_file.cpp */