Statistics
| Branch: | Revision:

ffmpeg / libavformat / mxg.c @ b7effd4e

History | View | Annotate | Download (8.21 KB)

1
/*
2
 * MxPEG clip file demuxer
3
 * Copyright (c) 2010 Anatoly Nenashev
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
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
20
 */
21

    
22
#include "libavutil/intreadwrite.h"
23
#include "libavcodec/mjpeg.h"
24
#include "avformat.h"
25
#include "avio.h"
26

    
27
#define VIDEO_STREAM_INDEX 0
28
#define AUDIO_STREAM_INDEX 1
29
#define DEFAULT_PACKET_SIZE 1024
30
#define OVERREAD_SIZE 3
31

    
32
typedef struct MXGContext {
33
    uint8_t *buffer;
34
    uint8_t *buffer_ptr;
35
    uint8_t *soi_ptr;
36
    unsigned int buffer_size;
37
    int64_t dts;
38
    unsigned int cache_size;
39
} MXGContext;
40

    
41
static int mxg_read_header(AVFormatContext *s, AVFormatParameters *ap)
42
{
43
    AVStream *video_st, *audio_st;
44
    MXGContext *mxg = s->priv_data;
45

    
46
    /* video parameters will be extracted from the compressed bitstream */
47
    video_st = av_new_stream(s, VIDEO_STREAM_INDEX);
48
    if (!video_st)
49
        return AVERROR(ENOMEM);
50
    video_st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
51
    video_st->codec->codec_id = CODEC_ID_MXPEG;
52
    av_set_pts_info(video_st, 64, 1, 1000000);
53

    
54
    audio_st = av_new_stream(s, AUDIO_STREAM_INDEX);
55
    if (!audio_st)
56
        return AVERROR(ENOMEM);
57
    audio_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
58
    audio_st->codec->codec_id = CODEC_ID_PCM_ALAW;
59
    audio_st->codec->channels = 1;
60
    audio_st->codec->sample_rate = 8000;
61
    audio_st->codec->bits_per_coded_sample = 8;
62
    audio_st->codec->block_align = 1;
63
    av_set_pts_info(audio_st, 64, 1, 1000000);
64

    
65
    mxg->soi_ptr = mxg->buffer_ptr = mxg->buffer = 0;
66
    mxg->buffer_size = 0;
67
    mxg->dts = AV_NOPTS_VALUE;
68
    mxg->cache_size = 0;
69

    
70
    return 0;
71
}
72

    
73
static uint8_t* mxg_find_startmarker(uint8_t *p, uint8_t *end)
74
{
75
    for (; p < end - 3; p += 4) {
76
        uint32_t x = *(uint32_t*)p;
77

    
78
        if (x & (~(x+0x01010101)) & 0x80808080) {
79
            if (p[0] == 0xff) {
80
                return p;
81
            } else if (p[1] == 0xff) {
82
                return p+1;
83
            } else if (p[2] == 0xff) {
84
                return p+2;
85
            } else if (p[3] == 0xff) {
86
                return p+3;
87
            }
88
        }
89
    }
90

    
91
    for (; p < end; ++p) {
92
        if (*p == 0xff) return p;
93
    }
94

    
95
    return end;
96
}
97

    
98
static int mxg_update_cache(AVFormatContext *s, unsigned int cache_size)
99
{
100
    MXGContext *mxg = s->priv_data;
101
    unsigned int current_pos = mxg->buffer_ptr - mxg->buffer;
102
    unsigned int soi_pos;
103
    int ret;
104

    
105
    /* reallocate internal buffer */
106
    if (current_pos > current_pos + cache_size)
107
        return AVERROR(ENOMEM);
108
    if (mxg->soi_ptr) soi_pos = mxg->soi_ptr - mxg->buffer;
109
    mxg->buffer = av_fast_realloc(mxg->buffer, &mxg->buffer_size,
110
                                  current_pos + cache_size +
111
                                  FF_INPUT_BUFFER_PADDING_SIZE);
112
    if (!mxg->buffer)
113
        return AVERROR(ENOMEM);
114
    mxg->buffer_ptr = mxg->buffer + current_pos;
115
    if (mxg->soi_ptr) mxg->soi_ptr = mxg->buffer + soi_pos;
116

    
117
    /* get data */
118
    ret = avio_read(s->pb, mxg->buffer_ptr + mxg->cache_size,
119
                     cache_size - mxg->cache_size);
120
    if (ret < 0)
121
        return ret;
122

    
123
    mxg->cache_size += ret;
124

    
125
    return ret;
126
}
127

    
128
static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt)
129
{
130
    int ret;
131
    unsigned int size;
132
    uint8_t *startmarker_ptr, *end, *search_end, marker;
133
    MXGContext *mxg = s->priv_data;
134

    
135
    while (!url_feof(s->pb) && !url_ferror(s->pb)){
136
        if (mxg->cache_size <= OVERREAD_SIZE) {
137
            /* update internal buffer */
138
            ret = mxg_update_cache(s, DEFAULT_PACKET_SIZE + OVERREAD_SIZE);
139
            if (ret < 0)
140
                return ret;
141
        }
142
        end = mxg->buffer_ptr + mxg->cache_size;
143

    
144
        /* find start marker - 0xff */
145
        if (mxg->cache_size > OVERREAD_SIZE) {
146
            search_end = end - OVERREAD_SIZE;
147
            startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
148
        } else {
149
            search_end = end;
150
            startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
151
            if (startmarker_ptr >= search_end - 1 ||
152
                *(startmarker_ptr + 1) != EOI) break;
153
        }
154

    
155
        if (startmarker_ptr != search_end) { /* start marker found */
156
            marker = *(startmarker_ptr + 1);
157
            mxg->buffer_ptr = startmarker_ptr + 2;
158
            mxg->cache_size = end - mxg->buffer_ptr;
159

    
160
            if (marker == SOI) {
161
                mxg->soi_ptr = startmarker_ptr;
162
            } else if (marker == EOI) {
163
                if (!mxg->soi_ptr) {
164
                    av_log(s, AV_LOG_WARNING, "Found EOI before SOI, skipping\n");
165
                    continue;
166
                }
167

    
168
                pkt->pts = pkt->dts = mxg->dts;
169
                pkt->stream_index = VIDEO_STREAM_INDEX;
170
                pkt->destruct = NULL;
171
                pkt->size = mxg->buffer_ptr - mxg->soi_ptr;
172
                pkt->data = mxg->soi_ptr;
173

    
174
                if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
175
                    if (mxg->cache_size > 0) {
176
                        memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
177
                    }
178

    
179
                    mxg->buffer_ptr = mxg->buffer;
180
                }
181
                mxg->soi_ptr = 0;
182

    
183
                return pkt->size;
184
            } else if ( (SOF0 <= marker && marker <= SOF15) ||
185
                        (SOS  <= marker && marker <= COM) ) {
186
                /* all other markers that start marker segment also contain
187
                   length value (see specification for JPEG Annex B.1) */
188
                size = AV_RB16(mxg->buffer_ptr);
189
                if (size < 2)
190
                    return AVERROR(EINVAL);
191

    
192
                if (mxg->cache_size < size) {
193
                    ret = mxg_update_cache(s, size);
194
                    if (ret < 0)
195
                        return ret;
196
                    startmarker_ptr = mxg->buffer_ptr - 2;
197
                    mxg->cache_size = 0;
198
                } else {
199
                    mxg->cache_size -= size;
200
                }
201

    
202
                mxg->buffer_ptr += size;
203

    
204
                if (marker == APP13 && size >= 16) { /* audio data */
205
                    /* time (GMT) of first sample in usec since 1970, little-endian */
206
                    pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8);
207
                    pkt->stream_index = AUDIO_STREAM_INDEX;
208
                    pkt->destruct = NULL;
209
                    pkt->size = size - 14;
210
                    pkt->data = startmarker_ptr + 16;
211

    
212
                    if (startmarker_ptr - mxg->buffer > mxg->cache_size) {
213
                        if (mxg->cache_size > 0) {
214
                            memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
215
                        }
216
                        mxg->buffer_ptr = mxg->buffer;
217
                    }
218

    
219
                    return pkt->size;
220
                } else if (marker == COM && size >= 18 &&
221
                           !strncmp(startmarker_ptr + 4, "MXF", 3)) {
222
                    /* time (GMT) of video frame in usec since 1970, little-endian */
223
                    mxg->dts = AV_RL64(startmarker_ptr + 12);
224
                }
225
            }
226
        } else {
227
            /* start marker not found */
228
            mxg->buffer_ptr = search_end;
229
            mxg->cache_size = OVERREAD_SIZE;
230
        }
231
    }
232

    
233
    return AVERROR_EOF;
234
}
235

    
236
static int mxg_close(struct AVFormatContext *s)
237
{
238
    MXGContext *mxg = s->priv_data;
239
    av_freep(&mxg->buffer);
240
    return 0;
241
}
242

    
243
AVInputFormat ff_mxg_demuxer = {
244
    .name = "mxg",
245
    .long_name = NULL_IF_CONFIG_SMALL("MxPEG clip file format"),
246
    .priv_data_size = sizeof(MXGContext),
247
    .read_header = mxg_read_header,
248
    .read_packet = mxg_read_packet,
249
    .read_close = mxg_close,
250
    .extensions = "mxg"
251
};