mxg.c 8.36 KB
Newer Older
Anatoly Nenashev's avatar
Anatoly Nenashev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * MxPEG clip file demuxer
 * Copyright (c) 2010 Anatoly Nenashev
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

Justin Ruggles's avatar
Justin Ruggles committed
22
#include "libavutil/channel_layout.h"
Anatoly Nenashev's avatar
Anatoly Nenashev committed
23 24 25
#include "libavutil/intreadwrite.h"
#include "libavcodec/mjpeg.h"
#include "avformat.h"
26
#include "internal.h"
Anatoly Nenashev's avatar
Anatoly Nenashev committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "avio.h"

#define DEFAULT_PACKET_SIZE 1024
#define OVERREAD_SIZE 3

typedef struct MXGContext {
    uint8_t *buffer;
    uint8_t *buffer_ptr;
    uint8_t *soi_ptr;
    unsigned int buffer_size;
    int64_t dts;
    unsigned int cache_size;
} MXGContext;

41
static int mxg_read_header(AVFormatContext *s)
Anatoly Nenashev's avatar
Anatoly Nenashev committed
42 43 44 45 46
{
    AVStream *video_st, *audio_st;
    MXGContext *mxg = s->priv_data;

    /* video parameters will be extracted from the compressed bitstream */
47
    video_st = avformat_new_stream(s, NULL);
Anatoly Nenashev's avatar
Anatoly Nenashev committed
48 49 50
    if (!video_st)
        return AVERROR(ENOMEM);
    video_st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
51
    video_st->codec->codec_id = AV_CODEC_ID_MXPEG;
52
    avpriv_set_pts_info(video_st, 64, 1, 1000000);
Anatoly Nenashev's avatar
Anatoly Nenashev committed
53

54
    audio_st = avformat_new_stream(s, NULL);
Anatoly Nenashev's avatar
Anatoly Nenashev committed
55 56 57
    if (!audio_st)
        return AVERROR(ENOMEM);
    audio_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
58
    audio_st->codec->codec_id = AV_CODEC_ID_PCM_ALAW;
Anatoly Nenashev's avatar
Anatoly Nenashev committed
59
    audio_st->codec->channels = 1;
Justin Ruggles's avatar
Justin Ruggles committed
60
    audio_st->codec->channel_layout = AV_CH_LAYOUT_MONO;
Anatoly Nenashev's avatar
Anatoly Nenashev committed
61 62 63
    audio_st->codec->sample_rate = 8000;
    audio_st->codec->bits_per_coded_sample = 8;
    audio_st->codec->block_align = 1;
64
    avpriv_set_pts_info(audio_st, 64, 1, 1000000);
Anatoly Nenashev's avatar
Anatoly Nenashev committed
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

    mxg->soi_ptr = mxg->buffer_ptr = mxg->buffer = 0;
    mxg->buffer_size = 0;
    mxg->dts = AV_NOPTS_VALUE;
    mxg->cache_size = 0;

    return 0;
}

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

        if (x & (~(x+0x01010101)) & 0x80808080) {
            if (p[0] == 0xff) {
                return p;
            } else if (p[1] == 0xff) {
                return p+1;
            } else if (p[2] == 0xff) {
                return p+2;
            } else if (p[3] == 0xff) {
                return p+3;
            }
        }
    }

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

    return end;
}

static int mxg_update_cache(AVFormatContext *s, unsigned int cache_size)
{
    MXGContext *mxg = s->priv_data;
    unsigned int current_pos = mxg->buffer_ptr - mxg->buffer;
    unsigned int soi_pos;
104
    uint8_t *buffer;
Anatoly Nenashev's avatar
Anatoly Nenashev committed
105 106 107 108 109
    int ret;

    /* reallocate internal buffer */
    if (current_pos > current_pos + cache_size)
        return AVERROR(ENOMEM);
110
    soi_pos = mxg->soi_ptr - mxg->buffer;
111 112 113 114
    buffer = av_fast_realloc(mxg->buffer, &mxg->buffer_size,
                             current_pos + cache_size +
                             FF_INPUT_BUFFER_PADDING_SIZE);
    if (!buffer)
Anatoly Nenashev's avatar
Anatoly Nenashev committed
115
        return AVERROR(ENOMEM);
116
    mxg->buffer = buffer;
Anatoly Nenashev's avatar
Anatoly Nenashev committed
117 118 119 120
    mxg->buffer_ptr = mxg->buffer + current_pos;
    if (mxg->soi_ptr) mxg->soi_ptr = mxg->buffer + soi_pos;

    /* get data */
121
    ret = avio_read(s->pb, mxg->buffer_ptr + mxg->cache_size,
Anatoly Nenashev's avatar
Anatoly Nenashev committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                     cache_size - mxg->cache_size);
    if (ret < 0)
        return ret;

    mxg->cache_size += ret;

    return ret;
}

static int mxg_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    int ret;
    unsigned int size;
    uint8_t *startmarker_ptr, *end, *search_end, marker;
    MXGContext *mxg = s->priv_data;

138
    while (!url_feof(s->pb) && !s->pb->error){
Anatoly Nenashev's avatar
Anatoly Nenashev committed
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
        if (mxg->cache_size <= OVERREAD_SIZE) {
            /* update internal buffer */
            ret = mxg_update_cache(s, DEFAULT_PACKET_SIZE + OVERREAD_SIZE);
            if (ret < 0)
                return ret;
        }
        end = mxg->buffer_ptr + mxg->cache_size;

        /* find start marker - 0xff */
        if (mxg->cache_size > OVERREAD_SIZE) {
            search_end = end - OVERREAD_SIZE;
            startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
        } else {
            search_end = end;
            startmarker_ptr = mxg_find_startmarker(mxg->buffer_ptr, search_end);
            if (startmarker_ptr >= search_end - 1 ||
                *(startmarker_ptr + 1) != EOI) break;
        }

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

            if (marker == SOI) {
                mxg->soi_ptr = startmarker_ptr;
            } else if (marker == EOI) {
                if (!mxg->soi_ptr) {
                    av_log(s, AV_LOG_WARNING, "Found EOI before SOI, skipping\n");
                    continue;
                }

                pkt->pts = pkt->dts = mxg->dts;
172
                pkt->stream_index = 0;
173
#if FF_API_DESTRUCT_PACKET
Anatoly Nenashev's avatar
Anatoly Nenashev committed
174
                pkt->destruct = NULL;
175 176
#endif
                pkt->buf  = NULL;
Anatoly Nenashev's avatar
Anatoly Nenashev committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
                pkt->size = mxg->buffer_ptr - mxg->soi_ptr;
                pkt->data = mxg->soi_ptr;

                if (mxg->soi_ptr - mxg->buffer > mxg->cache_size) {
                    if (mxg->cache_size > 0) {
                        memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
                    }

                    mxg->buffer_ptr = mxg->buffer;
                }
                mxg->soi_ptr = 0;

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

                if (mxg->cache_size < size) {
                    ret = mxg_update_cache(s, size);
                    if (ret < 0)
                        return ret;
                    startmarker_ptr = mxg->buffer_ptr - 2;
                    mxg->cache_size = 0;
                } else {
                    mxg->cache_size -= size;
                }

                mxg->buffer_ptr += size;

                if (marker == APP13 && size >= 16) { /* audio data */
                    /* time (GMT) of first sample in usec since 1970, little-endian */
                    pkt->pts = pkt->dts = AV_RL64(startmarker_ptr + 8);
213
                    pkt->stream_index = 1;
214
#if FF_API_DESTRUCT_PACKET
Anatoly Nenashev's avatar
Anatoly Nenashev committed
215
                    pkt->destruct = NULL;
216 217
#endif
                    pkt->buf  = NULL;
Anatoly Nenashev's avatar
Anatoly Nenashev committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                    pkt->size = size - 14;
                    pkt->data = startmarker_ptr + 16;

                    if (startmarker_ptr - mxg->buffer > mxg->cache_size) {
                        if (mxg->cache_size > 0) {
                            memcpy(mxg->buffer, mxg->buffer_ptr, mxg->cache_size);
                        }
                        mxg->buffer_ptr = mxg->buffer;
                    }

                    return pkt->size;
                } else if (marker == COM && size >= 18 &&
                           !strncmp(startmarker_ptr + 4, "MXF", 3)) {
                    /* time (GMT) of video frame in usec since 1970, little-endian */
                    mxg->dts = AV_RL64(startmarker_ptr + 12);
                }
            }
        } else {
            /* start marker not found */
            mxg->buffer_ptr = search_end;
            mxg->cache_size = OVERREAD_SIZE;
        }
    }

    return AVERROR_EOF;
}

static int mxg_close(struct AVFormatContext *s)
{
    MXGContext *mxg = s->priv_data;
    av_freep(&mxg->buffer);
    return 0;
}

252
AVInputFormat ff_mxg_demuxer = {
253
    .name           = "mxg",
254
    .long_name      = NULL_IF_CONFIG_SMALL("MxPEG clip"),
Anatoly Nenashev's avatar
Anatoly Nenashev committed
255
    .priv_data_size = sizeof(MXGContext),
256 257 258 259
    .read_header    = mxg_read_header,
    .read_packet    = mxg_read_packet,
    .read_close     = mxg_close,
    .extensions     = "mxg",
Anatoly Nenashev's avatar
Anatoly Nenashev committed
260
};