ncdec.c 2.5 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. #define NC_VIDEO_FLAG 0x1A5
  25. static int nc_probe(AVProbeData *probe_packet)
  26. {
  27. int size;
  28. if (AV_RB32(probe_packet->buf) != NC_VIDEO_FLAG)
  29. return 0;
  30. size = AV_RL16(probe_packet->buf + 5);
  31. if (size + 20 > probe_packet->buf_size)
  32. return 3*AVPROBE_SCORE_MAX/2;
  33. if (AV_RB32(probe_packet->buf+16+size) == NC_VIDEO_FLAG)
  34. return AVPROBE_SCORE_MAX;
  35. return 0;
  36. }
  37. static int nc_read_header(AVFormatContext *s, AVFormatParameters *ap)
  38. {
  39. AVStream *st = av_new_stream(s, 0);
  40. if (!st)
  41. return AVERROR(ENOMEM);
  42. st->codec->codec_type = CODEC_TYPE_VIDEO;
  43. st->codec->codec_id = CODEC_ID_MPEG4;
  44. st->need_parsing = AVSTREAM_PARSE_FULL;
  45. av_set_pts_info(st, 64, 1, 100);
  46. return 0;
  47. }
  48. static int nc_read_packet(AVFormatContext *s, AVPacket *pkt)
  49. {
  50. int size;
  51. int ret;
  52. uint32_t state=-1;
  53. while (state != NC_VIDEO_FLAG) {
  54. if (url_feof(s->pb))
  55. return AVERROR(EIO);
  56. state = (state<<8) + get_byte(s->pb);
  57. }
  58. get_byte(s->pb);
  59. size = get_le16(s->pb);
  60. url_fskip(s->pb, 9);
  61. if (size == 0) {
  62. av_log(s, AV_LOG_DEBUG, "Next packet size is zero\n");
  63. return AVERROR(EAGAIN);
  64. }
  65. ret = av_get_packet(s->pb, pkt, size);
  66. if (ret != size) {
  67. if (ret > 0) av_free_packet(pkt);
  68. return AVERROR(EIO);
  69. }
  70. pkt->stream_index = 0;
  71. return size;
  72. }
  73. AVInputFormat nc_demuxer = {
  74. "nc",
  75. NULL_IF_CONFIG_SMALL("NC camera feed format"),
  76. 0,
  77. nc_probe,
  78. nc_read_header,
  79. nc_read_packet,
  80. .extensions = "v",
  81. };