ncdec.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include "libavutil/intreadwrite.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #define NC_VIDEO_FLAG 0x1A5
  26. static int nc_probe(AVProbeData *probe_packet)
  27. {
  28. int size;
  29. if (AV_RB32(probe_packet->buf) != NC_VIDEO_FLAG)
  30. return 0;
  31. size = AV_RL16(probe_packet->buf + 5);
  32. if (size + 20 > probe_packet->buf_size)
  33. return AVPROBE_SCORE_MAX/4;
  34. if (AV_RB32(probe_packet->buf+16+size) == NC_VIDEO_FLAG)
  35. return AVPROBE_SCORE_MAX;
  36. return 0;
  37. }
  38. static int nc_read_header(AVFormatContext *s)
  39. {
  40. AVStream *st = avformat_new_stream(s, NULL);
  41. if (!st)
  42. return AVERROR(ENOMEM);
  43. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  44. st->codec->codec_id = AV_CODEC_ID_MPEG4;
  45. st->need_parsing = AVSTREAM_PARSE_FULL;
  46. avpriv_set_pts_info(st, 64, 1, 100);
  47. return 0;
  48. }
  49. static int nc_read_packet(AVFormatContext *s, AVPacket *pkt)
  50. {
  51. int size;
  52. int ret;
  53. uint32_t state=-1;
  54. while (state != NC_VIDEO_FLAG) {
  55. if (url_feof(s->pb))
  56. return AVERROR(EIO);
  57. state = (state<<8) + avio_r8(s->pb);
  58. }
  59. avio_r8(s->pb);
  60. size = avio_rl16(s->pb);
  61. avio_skip(s->pb, 9);
  62. if (size == 0) {
  63. av_log(s, AV_LOG_DEBUG, "Next packet size is zero\n");
  64. return AVERROR(EAGAIN);
  65. }
  66. ret = av_get_packet(s->pb, pkt, size);
  67. if (ret != size) {
  68. if (ret > 0) av_free_packet(pkt);
  69. return AVERROR(EIO);
  70. }
  71. pkt->stream_index = 0;
  72. return size;
  73. }
  74. AVInputFormat ff_nc_demuxer = {
  75. .name = "nc",
  76. .long_name = NULL_IF_CONFIG_SMALL("NC camera feed"),
  77. .read_probe = nc_probe,
  78. .read_header = nc_read_header,
  79. .read_packet = nc_read_packet,
  80. .extensions = "v",
  81. };