ncdec.c 2.59 KB
Newer Older
Nicolas Martin's avatar
Nicolas Martin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * NC camera feed demuxer
 * Copyright (c) 2009  Nicolas Martin (martinic at iro dot umontreal dot ca)
 *                     Edouard Auvinet
 *
 * 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
 */

#include "libavutil/intreadwrite.h"
#include "avformat.h"
25
#include "internal.h"
Nicolas Martin's avatar
Nicolas Martin committed
26 27 28 29 30 31 32 33 34 35 36 37 38

#define NC_VIDEO_FLAG 0x1A5

static int nc_probe(AVProbeData *probe_packet)
{
    int size;

    if (AV_RB32(probe_packet->buf) != NC_VIDEO_FLAG)
        return 0;

    size = AV_RL16(probe_packet->buf + 5);

    if (size + 20 > probe_packet->buf_size)
39
        return AVPROBE_SCORE_MAX/4;
Nicolas Martin's avatar
Nicolas Martin committed
40 41 42 43 44 45 46

    if (AV_RB32(probe_packet->buf+16+size) == NC_VIDEO_FLAG)
        return AVPROBE_SCORE_MAX;

    return 0;
}

47
static int nc_read_header(AVFormatContext *s)
Nicolas Martin's avatar
Nicolas Martin committed
48
{
49
    AVStream *st = avformat_new_stream(s, NULL);
Nicolas Martin's avatar
Nicolas Martin committed
50 51 52 53

    if (!st)
        return AVERROR(ENOMEM);

54
    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
55
    st->codec->codec_id   = AV_CODEC_ID_MPEG4;
Nicolas Martin's avatar
Nicolas Martin committed
56 57
    st->need_parsing      = AVSTREAM_PARSE_FULL;

58
    avpriv_set_pts_info(st, 64, 1, 100);
Nicolas Martin's avatar
Nicolas Martin committed
59 60 61 62 63 64 65

    return 0;
}

static int nc_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    int size;
66
    int ret;
Nicolas Martin's avatar
Nicolas Martin committed
67 68 69

    uint32_t state=-1;
    while (state != NC_VIDEO_FLAG) {
70
        if (avio_feof(s->pb))
Nicolas Martin's avatar
Nicolas Martin committed
71
            return AVERROR(EIO);
72
        state = (state<<8) + avio_r8(s->pb);
Nicolas Martin's avatar
Nicolas Martin committed
73 74
    }

75 76
    avio_r8(s->pb);
    size = avio_rl16(s->pb);
77
    avio_skip(s->pb, 9);
Nicolas Martin's avatar
Nicolas Martin committed
78 79 80 81 82 83

    if (size == 0) {
        av_log(s, AV_LOG_DEBUG, "Next packet size is zero\n");
        return AVERROR(EAGAIN);
    }

84
    ret = av_get_packet(s->pb, pkt, size);
Nicolas Martin's avatar
Nicolas Martin committed
85
    if (ret != size) {
86
        if (ret > 0) av_packet_unref(pkt);
Nicolas Martin's avatar
Nicolas Martin committed
87 88 89 90 91 92 93
        return AVERROR(EIO);
    }

    pkt->stream_index = 0;
    return size;
}

94
AVInputFormat ff_nc_demuxer = {
95
    .name           = "nc",
96
    .long_name      = NULL_IF_CONFIG_SMALL("NC camera feed"),
97 98 99
    .read_probe     = nc_probe,
    .read_header    = nc_read_header,
    .read_packet    = nc_read_packet,
100
    .extensions     = "v",
Nicolas Martin's avatar
Nicolas Martin committed
101
};