/src/ffmpeg/libavformat/ncdec.c
Line | Count | Source |
1 | | /* |
2 | | * NC camera feed demuxer |
3 | | * Copyright (c) 2009 Nicolas Martin (martinic at iro dot umontreal dot ca) |
4 | | * Edouard Auvinet |
5 | | * |
6 | | * This file is part of FFmpeg. |
7 | | * |
8 | | * FFmpeg 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. |
12 | | * |
13 | | * FFmpeg 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. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public |
19 | | * License along with FFmpeg; if not, write to the Free Software |
20 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | | */ |
22 | | |
23 | | #include "libavutil/intreadwrite.h" |
24 | | #include "avformat.h" |
25 | | #include "demux.h" |
26 | | #include "internal.h" |
27 | | |
28 | 19.3M | #define NC_VIDEO_FLAG 0x1A5 |
29 | | |
30 | | static int nc_probe(const AVProbeData *probe_packet) |
31 | 936k | { |
32 | 936k | int size; |
33 | | |
34 | 936k | if (AV_RB32(probe_packet->buf) != NC_VIDEO_FLAG) |
35 | 935k | return 0; |
36 | | |
37 | 829 | size = AV_RL16(probe_packet->buf + 5); |
38 | | |
39 | 829 | if (size + 20 > probe_packet->buf_size) |
40 | 230 | return AVPROBE_SCORE_MAX/4; |
41 | | |
42 | 599 | if (AV_RB32(probe_packet->buf+16+size) == NC_VIDEO_FLAG) |
43 | 283 | return AVPROBE_SCORE_MAX; |
44 | | |
45 | 316 | return 0; |
46 | 599 | } |
47 | | |
48 | | static int nc_read_header(AVFormatContext *s) |
49 | 851 | { |
50 | 851 | AVStream *st = avformat_new_stream(s, NULL); |
51 | | |
52 | 851 | if (!st) |
53 | 0 | return AVERROR(ENOMEM); |
54 | | |
55 | 851 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; |
56 | 851 | st->codecpar->codec_id = AV_CODEC_ID_MPEG4; |
57 | 851 | ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL; |
58 | | |
59 | 851 | avpriv_set_pts_info(st, 64, 1, 100); |
60 | | |
61 | 851 | return 0; |
62 | 851 | } |
63 | | |
64 | | static int nc_read_packet(AVFormatContext *s, AVPacket *pkt) |
65 | 171k | { |
66 | 171k | int size; |
67 | 171k | int ret; |
68 | | |
69 | 171k | uint32_t state=-1; |
70 | 18.4M | while (state != NC_VIDEO_FLAG) { |
71 | 18.2M | if (avio_feof(s->pb)) |
72 | 1.23k | return AVERROR_INVALIDDATA; |
73 | 18.2M | state = (state<<8) + avio_r8(s->pb); |
74 | 18.2M | } |
75 | | |
76 | 170k | avio_r8(s->pb); |
77 | 170k | size = avio_rl16(s->pb); |
78 | 170k | avio_skip(s->pb, 9); |
79 | | |
80 | 170k | if (size == 0) { |
81 | 6.74k | av_log(s, AV_LOG_DEBUG, "Next packet size is zero\n"); |
82 | 6.74k | return AVERROR(EAGAIN); |
83 | 6.74k | } |
84 | | |
85 | 163k | ret = av_get_packet(s->pb, pkt, size); |
86 | 163k | if (ret != size) { |
87 | 266 | return AVERROR_INVALIDDATA; |
88 | 266 | } |
89 | | |
90 | 163k | pkt->stream_index = 0; |
91 | 163k | return size; |
92 | 163k | } |
93 | | |
94 | | const FFInputFormat ff_nc_demuxer = { |
95 | | .p.name = "nc", |
96 | | .p.long_name = NULL_IF_CONFIG_SMALL("NC camera feed"), |
97 | | .p.extensions = "v", |
98 | | .read_probe = nc_probe, |
99 | | .read_header = nc_read_header, |
100 | | .read_packet = nc_read_packet, |
101 | | }; |