~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavformat/psxstr.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * Sony Playstation (PSX) STR File Demuxer
3
3
 * Copyright (c) 2003 The ffmpeg Project
4
4
 *
5
 
 * This library is free software; you can redistribute it and/or
 
5
 * This file is part of FFmpeg.
 
6
 *
 
7
 * FFmpeg is free software; you can redistribute it and/or
6
8
 * modify it under the terms of the GNU Lesser General Public
7
9
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
 
10
 * version 2.1 of the License, or (at your option) any later version.
9
11
 *
10
 
 * This library is distributed in the hope that it will be useful,
 
12
 * FFmpeg is distributed in the hope that it will be useful,
11
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
15
 * Lesser General Public License for more details.
14
16
 *
15
17
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this library; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 * License along with FFmpeg; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
20
 */
19
21
 
20
22
/**
31
33
 
32
34
//#define PRINTSTUFF
33
35
 
34
 
#define LE_16(x)  ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
35
 
#define LE_32(x)  ((((uint8_t*)(x))[3] << 24) | \
36
 
                   (((uint8_t*)(x))[2] << 16) | \
37
 
                   (((uint8_t*)(x))[1] << 8) | \
38
 
                    ((uint8_t*)(x))[0])
39
 
 
40
 
#define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
41
 
        ( (long)(unsigned char)(ch0) | \
42
 
        ( (long)(unsigned char)(ch1) << 8 ) | \
43
 
        ( (long)(unsigned char)(ch2) << 16 ) | \
44
 
        ( (long)(unsigned char)(ch3) << 24 ) )
45
 
 
46
 
#define RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F')
47
 
#define CDXA_TAG FOURCC_TAG('C', 'D', 'X', 'A')
 
36
#define RIFF_TAG MKTAG('R', 'I', 'F', 'F')
 
37
#define CDXA_TAG MKTAG('C', 'D', 'X', 'A')
48
38
 
49
39
#define RAW_CD_SECTOR_SIZE 2352
50
40
#define RAW_CD_SECTOR_DATA_SIZE 2304
89
79
    int64_t pts;
90
80
 
91
81
    unsigned char *video_chunk;
 
82
    AVPacket tmp_pkt;
92
83
} StrDemuxContext;
93
84
 
 
85
static const char sync_header[12] = {0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00};
 
86
 
94
87
static int str_probe(AVProbeData *p)
95
88
{
96
89
    int start;
99
92
    if (p->buf_size < 0x38)
100
93
        return 0;
101
94
 
102
 
    if ((LE_32(&p->buf[0]) == RIFF_TAG) &&
103
 
        (LE_32(&p->buf[8]) == CDXA_TAG)) {
 
95
    if ((AV_RL32(&p->buf[0]) == RIFF_TAG) &&
 
96
        (AV_RL32(&p->buf[8]) == CDXA_TAG)) {
104
97
 
105
98
        /* RIFF header seen; skip 0x2C bytes */
106
99
        start = RIFF_HEADER_SIZE;
108
101
        start = 0;
109
102
 
110
103
    /* look for CD sync header (00, 0xFF x 10, 00) */
111
 
    if ((p->buf[start + 0] != 0x00) || (p->buf[start + 1] != 0xFF) ||
112
 
        (p->buf[start + 2] != 0xFF) || (p->buf[start + 3] != 0xFF) ||
113
 
        (p->buf[start + 4] != 0xFF) || (p->buf[start + 5] != 0xFF) ||
114
 
        (p->buf[start + 6] != 0xFF) || (p->buf[start + 7] != 0xFF) ||
115
 
        (p->buf[start + 8] != 0xFF) || (p->buf[start + 9] != 0xFF) ||
116
 
        (p->buf[start + 10] != 0xFF) || (p->buf[start + 11] != 0x00))
 
104
    if (memcmp(p->buf+start,sync_header,sizeof(sync_header)))
117
105
        return 0;
118
106
 
119
107
    /* MPEG files (like those ripped from VCDs) can also look like this;
121
109
    return 50;
122
110
}
123
111
 
 
112
#if 0
 
113
static void dump(unsigned char *buf,size_t len)
 
114
{
 
115
    int i;
 
116
    for(i=0;i<len;i++) {
 
117
        if ((i&15)==0) av_log(NULL, AV_LOG_DEBUG, "%04x  ",i);
 
118
        av_log(NULL, AV_LOG_DEBUG, "%02x ",buf[i]);
 
119
        if ((i&15)==15) av_log(NULL, AV_LOG_DEBUG, "\n");
 
120
    }
 
121
    av_log(NULL, AV_LOG_DEBUG, "\n");
 
122
}
 
123
#endif
 
124
 
124
125
static int str_read_header(AVFormatContext *s,
125
126
                           AVFormatParameters *ap)
126
127
{
138
139
    str->video_channel = -1;
139
140
    str->video_chunk = NULL;
140
141
 
141
 
    /* set the pts reference (1 pts = 1/90000) */
142
 
    s->pts_num = 1;
143
 
    s->pts_den = 90000;
144
142
 
145
143
    /* skip over any RIFF header */
146
144
    if (get_buffer(pb, sector, RIFF_HEADER_SIZE) != RIFF_HEADER_SIZE)
147
145
        return AVERROR_IO;
148
 
    if (LE_32(&sector[0]) == RIFF_TAG)
 
146
    if (AV_RL32(&sector[0]) == RIFF_TAG)
149
147
        start = RIFF_HEADER_SIZE;
150
148
    else
151
149
        start = 0;
157
155
        if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
158
156
            return AVERROR_IO;
159
157
 
 
158
//printf("%02x %02x %02x %02x\n",sector[0x10],sector[0x11],sector[0x12],sector[0x13]);
 
159
 
160
160
        channel = sector[0x11];
161
161
        if (channel >= 32)
162
162
            return AVERROR_INVALIDDATA;
168
168
            /* check if this channel gets to be the dominant video channel */
169
169
            if (str->video_channel == -1) {
170
170
                /* qualify the magic number */
171
 
                if (LE_32(&sector[0x18]) != STR_MAGIC)
 
171
                if (AV_RL32(&sector[0x18]) != STR_MAGIC)
172
172
                    break;
173
173
                str->video_channel = channel;
174
174
                str->channels[channel].type = STR_VIDEO;
175
 
                str->channels[channel].width = LE_16(&sector[0x28]);
176
 
                str->channels[channel].height = LE_16(&sector[0x2A]);
 
175
                str->channels[channel].width = AV_RL16(&sector[0x28]);
 
176
                str->channels[channel].height = AV_RL16(&sector[0x2A]);
177
177
 
178
178
                /* allocate a new AVStream */
179
179
                st = av_new_stream(s, 0);
180
180
                if (!st)
181
181
                    return AVERROR_NOMEM;
 
182
                av_set_pts_info(st, 64, 1, 15);
182
183
 
183
184
                str->channels[channel].video_stream_index = st->index;
184
185
 
185
 
                st->codec.codec_type = CODEC_TYPE_VIDEO;
186
 
                st->codec.codec_id = CODEC_ID_MDEC; 
187
 
                st->codec.codec_tag = 0;  /* no fourcc */
188
 
                st->codec.width = str->channels[channel].width;
189
 
                st->codec.height = str->channels[channel].height;
 
186
                st->codec->codec_type = CODEC_TYPE_VIDEO;
 
187
                st->codec->codec_id = CODEC_ID_MDEC;
 
188
                st->codec->codec_tag = 0;  /* no fourcc */
 
189
                st->codec->width = str->channels[channel].width;
 
190
                st->codec->height = str->channels[channel].height;
190
191
            }
191
192
            break;
192
193
 
193
194
        case CDXA_TYPE_AUDIO:
194
195
            /* check if this channel gets to be the dominant audio channel */
195
196
            if (str->audio_channel == -1) {
 
197
                int fmt;
196
198
                str->audio_channel = channel;
197
199
                str->channels[channel].type = STR_AUDIO;
198
 
                str->channels[channel].channels = 
 
200
                str->channels[channel].channels =
199
201
                    (sector[0x13] & 0x01) ? 2 : 1;
200
 
                str->channels[channel].sample_rate = 
 
202
                str->channels[channel].sample_rate =
201
203
                    (sector[0x13] & 0x04) ? 18900 : 37800;
202
 
                str->channels[channel].bits = 
 
204
                str->channels[channel].bits =
203
205
                    (sector[0x13] & 0x10) ? 8 : 4;
 
206
 
 
207
                /* allocate a new AVStream */
 
208
                st = av_new_stream(s, 0);
 
209
                if (!st)
 
210
                    return AVERROR_NOMEM;
 
211
                av_set_pts_info(st, 64, 128, str->channels[channel].sample_rate);
 
212
 
 
213
                str->channels[channel].audio_stream_index = st->index;
 
214
 
 
215
                fmt = sector[0x13];
 
216
                st->codec->codec_type = CODEC_TYPE_AUDIO;
 
217
                st->codec->codec_id = CODEC_ID_ADPCM_XA;
 
218
                st->codec->codec_tag = 0;  /* no fourcc */
 
219
                st->codec->channels = (fmt&1)?2:1;
 
220
                st->codec->sample_rate = (fmt&4)?18900:37800;
 
221
            //    st->codec->bit_rate = 0; //FIXME;
 
222
                st->codec->block_align = 128;
204
223
            }
205
224
            break;
206
225
 
211
230
    }
212
231
 
213
232
if (str->video_channel != -1)
214
 
  printf (" video channel = %d, %d x %d\n", str->video_channel,
 
233
  av_log (s, AV_LOG_DEBUG, " video channel = %d, %d x %d %d\n", str->video_channel,
215
234
    str->channels[str->video_channel].width,
216
 
    str->channels[str->video_channel].height);
 
235
    str->channels[str->video_channel].height,str->channels[str->video_channel].video_stream_index);
217
236
if (str->audio_channel != -1)
218
 
  printf (" audio channel = %d, %d Hz, %d channels, %d bits/sample\n", 
219
 
    str->video_channel,
220
 
    str->channels[str->video_channel].sample_rate,
221
 
    str->channels[str->video_channel].channels,
222
 
    str->channels[str->video_channel].bits);
 
237
   av_log (s, AV_LOG_DEBUG, " audio channel = %d, %d Hz, %d channels, %d bits/sample %d\n",
 
238
    str->audio_channel,
 
239
    str->channels[str->audio_channel].sample_rate,
 
240
    str->channels[str->audio_channel].channels,
 
241
    str->channels[str->audio_channel].bits,str->channels[str->audio_channel].audio_stream_index);
223
242
 
224
243
    /* back to the start */
225
244
    url_fseek(pb, start, SEEK_SET);
228
247
}
229
248
 
230
249
static int str_read_packet(AVFormatContext *s,
231
 
                           AVPacket *pkt)
 
250
                           AVPacket *ret_pkt)
232
251
{
233
252
    ByteIOContext *pb = &s->pb;
234
253
    StrDemuxContext *str = (StrDemuxContext *)s->priv_data;
235
254
    unsigned char sector[RAW_CD_SECTOR_SIZE];
236
255
    int channel;
237
256
    int packet_read = 0;
238
 
    int video_sector_count = 0;
239
 
    int current_video_sector = 0;
240
 
    int video_frame_size = 0;
241
 
    int video_frame_index = 0;
242
 
    int bytes_to_copy;
243
257
    int ret = 0;
 
258
    AVPacket *pkt;
244
259
 
245
260
    while (!packet_read) {
246
261
 
247
262
        if (get_buffer(pb, sector, RAW_CD_SECTOR_SIZE) != RAW_CD_SECTOR_SIZE)
248
 
            return -EIO;
 
263
            return AVERROR_IO;
249
264
 
250
265
        channel = sector[0x11];
251
266
        if (channel >= 32)
258
273
            /* check if this the video channel we care about */
259
274
            if (channel == str->video_channel) {
260
275
 
 
276
                int current_sector = AV_RL16(&sector[0x1C]);
 
277
                int sector_count   = AV_RL16(&sector[0x1E]);
 
278
                int frame_size = AV_RL32(&sector[0x24]);
 
279
                int bytes_to_copy;
 
280
//        printf("%d %d %d\n",current_sector,sector_count,frame_size);
261
281
                /* if this is the first sector of the frame, allocate a pkt */
262
 
                if (current_video_sector == 0) {
263
 
                    video_frame_size = LE_32(&sector[0x24]);
264
 
                    video_sector_count = LE_16(&sector[0x1E]);
265
 
                    if (av_new_packet(pkt, video_frame_size))
266
 
                        return -EIO;
 
282
                pkt = &str->tmp_pkt;
 
283
                if (current_sector == 0) {
 
284
                    if (av_new_packet(pkt, frame_size))
 
285
                        return AVERROR_IO;
267
286
 
268
 
                    pkt->stream_index = 
 
287
                    pkt->pos= url_ftell(pb) - RAW_CD_SECTOR_SIZE;
 
288
                    pkt->stream_index =
269
289
                        str->channels[channel].video_stream_index;
270
 
                    pkt->pts = str->pts;
 
290
               //     pkt->pts = str->pts;
271
291
 
272
292
                    /* if there is no audio, adjust the pts after every video
273
293
                     * frame; assume 15 fps */
276
296
                }
277
297
 
278
298
                /* load all the constituent chunks in the video packet */
279
 
                if (video_frame_size - video_frame_index < VIDEO_DATA_CHUNK_SIZE)
280
 
                    bytes_to_copy = video_frame_size - video_frame_index;
281
 
                else
282
 
                    bytes_to_copy = VIDEO_DATA_CHUNK_SIZE;
283
 
                if (video_frame_index < video_frame_size)
284
 
                    memcpy(&pkt->data[video_frame_index],
285
 
                        &sector[VIDEO_DATA_HEADER_SIZE], bytes_to_copy);
286
 
 
287
 
#ifdef PRINTSTUFF
288
 
printf ("  chunk %d/%d (bytes %d/%d), first 6 bytes = %02X %02X %02X %02X %02X %02X\n",
289
 
  current_video_sector, video_sector_count,
290
 
  video_frame_index, video_frame_size,
291
 
  pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 0],
292
 
  pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 1],
293
 
  pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 2],
294
 
  pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 3],
295
 
  pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 4],
296
 
  pkt->data[current_video_sector * VIDEO_DATA_CHUNK_SIZE + 5]);
297
 
#endif
298
 
 
299
 
                video_frame_index += bytes_to_copy;
300
 
                /* must keep reading sectors until all current video sectors
301
 
                 * are consumed */
302
 
                current_video_sector++;
303
 
                if (current_video_sector >= video_sector_count)
304
 
                    packet_read = 1;
 
299
                bytes_to_copy = frame_size - current_sector*VIDEO_DATA_CHUNK_SIZE;
 
300
                if (bytes_to_copy>0) {
 
301
                    if (bytes_to_copy>VIDEO_DATA_CHUNK_SIZE) bytes_to_copy=VIDEO_DATA_CHUNK_SIZE;
 
302
                    memcpy(pkt->data + current_sector*VIDEO_DATA_CHUNK_SIZE,
 
303
                        sector + VIDEO_DATA_HEADER_SIZE, bytes_to_copy);
 
304
                }
 
305
                if (current_sector == sector_count-1) {
 
306
                    *ret_pkt = *pkt;
 
307
                    return 0;
 
308
                }
305
309
 
306
310
            }
307
311
            break;
310
314
#ifdef PRINTSTUFF
311
315
printf (" dropping audio sector\n");
312
316
#endif
313
 
        break;
 
317
#if 1
 
318
            /* check if this the video channel we care about */
 
319
            if (channel == str->audio_channel) {
 
320
                pkt = ret_pkt;
 
321
                if (av_new_packet(pkt, 2304))
 
322
                    return AVERROR_IO;
 
323
                memcpy(pkt->data,sector+24,2304);
 
324
 
 
325
                pkt->stream_index =
 
326
                    str->channels[channel].audio_stream_index;
 
327
                //pkt->pts = str->pts;
 
328
                return 0;
 
329
            }
 
330
#endif
 
331
            break;
314
332
        default:
315
333
            /* drop the sector and move on */
316
334
#ifdef PRINTSTUFF
320
338
        }
321
339
 
322
340
        if (url_feof(pb))
323
 
            return -EIO;
 
341
            return AVERROR_IO;
324
342
    }
325
343
 
326
344
    return ret;
335
353
    return 0;
336
354
}
337
355
 
338
 
static AVInputFormat str_iformat = {
 
356
AVInputFormat str_demuxer = {
339
357
    "psxstr",
340
358
    "Sony Playstation STR format",
341
359
    sizeof(StrDemuxContext),
344
362
    str_read_packet,
345
363
    str_read_close,
346
364
};
347
 
 
348
 
int str_init(void)
349
 
{
350
 
    av_register_input_format(&str_iformat);
351
 
    return 0;
352
 
}