libavformat/ncdec.c
eaf5d011
 /*
  * 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"
c3f9ebf7
 #include "internal.h"
eaf5d011
 
 #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)
0535f919
         return AVPROBE_SCORE_MAX/4;
eaf5d011
 
     if (AV_RB32(probe_packet->buf+16+size) == NC_VIDEO_FLAG)
         return AVPROBE_SCORE_MAX;
 
     return 0;
 }
 
6e9651d1
 static int nc_read_header(AVFormatContext *s)
eaf5d011
 {
3b3bbdd3
     AVStream *st = avformat_new_stream(s, NULL);
eaf5d011
 
     if (!st)
         return AVERROR(ENOMEM);
 
72415b2a
     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
36ef5369
     st->codec->codec_id   = AV_CODEC_ID_MPEG4;
eaf5d011
     st->need_parsing      = AVSTREAM_PARSE_FULL;
 
c3f9ebf7
     avpriv_set_pts_info(st, 64, 1, 100);
eaf5d011
 
     return 0;
 }
 
 static int nc_read_packet(AVFormatContext *s, AVPacket *pkt)
 {
     int size;
f9e5b942
     int ret;
eaf5d011
 
     uint32_t state=-1;
     while (state != NC_VIDEO_FLAG) {
d34ec64a
         if (avio_feof(s->pb))
eaf5d011
             return AVERROR(EIO);
e63a3628
         state = (state<<8) + avio_r8(s->pb);
eaf5d011
     }
 
e63a3628
     avio_r8(s->pb);
     size = avio_rl16(s->pb);
45a8a02a
     avio_skip(s->pb, 9);
eaf5d011
 
     if (size == 0) {
         av_log(s, AV_LOG_DEBUG, "Next packet size is zero\n");
         return AVERROR(EAGAIN);
     }
 
f9e5b942
     ret = av_get_packet(s->pb, pkt, size);
eaf5d011
     if (ret != size) {
ce70f28a
         if (ret > 0) av_packet_unref(pkt);
eaf5d011
         return AVERROR(EIO);
     }
 
     pkt->stream_index = 0;
     return size;
 }
 
66355be3
 AVInputFormat ff_nc_demuxer = {
dfc2c4d9
     .name           = "nc",
6774247a
     .long_name      = NULL_IF_CONFIG_SMALL("NC camera feed"),
dfc2c4d9
     .read_probe     = nc_probe,
     .read_header    = nc_read_header,
     .read_packet    = nc_read_packet,
20234a4b
     .extensions     = "v",
eaf5d011
 };