Merge remote-tracking branch 'libav/master'
[FFMpeg-mirror/mplayer-patches.git] / libavformat / ncdec.c
blob40d8dac3a7796f46e14c3282226318e707243c53
1 /*
2 * NC camera feed demuxer
3 * Copyright (c) 2009 Nicolas Martin (martinic at iro dot umontreal dot ca)
4 * Edouard Auvinet
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "internal.h"
27 #define NC_VIDEO_FLAG 0x1A5
29 static int nc_probe(AVProbeData *probe_packet)
31 int size;
33 if (AV_RB32(probe_packet->buf) != NC_VIDEO_FLAG)
34 return 0;
36 size = AV_RL16(probe_packet->buf + 5);
38 if (size + 20 > probe_packet->buf_size)
39 return AVPROBE_SCORE_MAX/4;
41 if (AV_RB32(probe_packet->buf+16+size) == NC_VIDEO_FLAG)
42 return AVPROBE_SCORE_MAX;
44 return 0;
47 static int nc_read_header(AVFormatContext *s)
49 AVStream *st = avformat_new_stream(s, NULL);
51 if (!st)
52 return AVERROR(ENOMEM);
54 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
55 st->codec->codec_id = AV_CODEC_ID_MPEG4;
56 st->need_parsing = AVSTREAM_PARSE_FULL;
58 avpriv_set_pts_info(st, 64, 1, 100);
60 return 0;
63 static int nc_read_packet(AVFormatContext *s, AVPacket *pkt)
65 int size;
66 int ret;
68 uint32_t state=-1;
69 while (state != NC_VIDEO_FLAG) {
70 if (s->pb->eof_reached)
71 return AVERROR(EIO);
72 state = (state<<8) + avio_r8(s->pb);
75 avio_r8(s->pb);
76 size = avio_rl16(s->pb);
77 avio_skip(s->pb, 9);
79 if (size == 0) {
80 av_log(s, AV_LOG_DEBUG, "Next packet size is zero\n");
81 return AVERROR(EAGAIN);
84 ret = av_get_packet(s->pb, pkt, size);
85 if (ret != size) {
86 if (ret > 0) av_free_packet(pkt);
87 return AVERROR(EIO);
90 pkt->stream_index = 0;
91 return size;
94 AVInputFormat ff_nc_demuxer = {
95 .name = "nc",
96 .long_name = NULL_IF_CONFIG_SMALL("NC camera feed"),
97 .read_probe = nc_probe,
98 .read_header = nc_read_header,
99 .read_packet = nc_read_packet,
100 .extensions = "v",