00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #include "libavutil/intreadwrite.h"
00024 #include "avformat.h"
00025 
00026 #define NC_VIDEO_FLAG 0x1A5
00027 
00028 static int nc_probe(AVProbeData *probe_packet)
00029 {
00030     int size;
00031 
00032     if (AV_RB32(probe_packet->buf) != NC_VIDEO_FLAG)
00033         return 0;
00034 
00035     size = AV_RL16(probe_packet->buf + 5);
00036 
00037     if (size + 20 > probe_packet->buf_size)
00038         return AVPROBE_SCORE_MAX/4;
00039 
00040     if (AV_RB32(probe_packet->buf+16+size) == NC_VIDEO_FLAG)
00041         return AVPROBE_SCORE_MAX;
00042 
00043     return 0;
00044 }
00045 
00046 static int nc_read_header(AVFormatContext *s, AVFormatParameters *ap)
00047 {
00048     AVStream *st = av_new_stream(s, 0);
00049 
00050     if (!st)
00051         return AVERROR(ENOMEM);
00052 
00053     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00054     st->codec->codec_id   = CODEC_ID_MPEG4;
00055     st->need_parsing      = AVSTREAM_PARSE_FULL;
00056 
00057     av_set_pts_info(st, 64, 1, 100);
00058 
00059     return 0;
00060 }
00061 
00062 static int nc_read_packet(AVFormatContext *s, AVPacket *pkt)
00063 {
00064     int size;
00065     int ret;
00066 
00067     uint32_t state=-1;
00068     while (state != NC_VIDEO_FLAG) {
00069         if (url_feof(s->pb))
00070             return AVERROR(EIO);
00071         state = (state<<8) + get_byte(s->pb);
00072     }
00073 
00074     get_byte(s->pb);
00075     size = get_le16(s->pb);
00076     url_fskip(s->pb, 9);
00077 
00078     if (size == 0) {
00079         av_log(s, AV_LOG_DEBUG, "Next packet size is zero\n");
00080         return AVERROR(EAGAIN);
00081     }
00082 
00083     ret = av_get_packet(s->pb, pkt, size);
00084     if (ret != size) {
00085         if (ret > 0) av_free_packet(pkt);
00086         return AVERROR(EIO);
00087     }
00088 
00089     pkt->stream_index = 0;
00090     return size;
00091 }
00092 
00093 AVInputFormat nc_demuxer = {
00094     "nc",
00095     NULL_IF_CONFIG_SMALL("NC camera feed format"),
00096     0,
00097     nc_probe,
00098     nc_read_header,
00099     nc_read_packet,
00100     .extensions = "v",
00101 };