You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.1KB

  1. /*
  2. * RAW demuxers
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2005 Alex Beregszaszi
  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 "avformat.h"
  23. #include "internal.h"
  24. #include "avio_internal.h"
  25. #include "rawdec.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/parseutils.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/avassert.h"
  30. #include "libavutil/intreadwrite.h"
  31. #define RAW_PACKET_SIZE 1024
  32. int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
  33. {
  34. int ret, size;
  35. size = RAW_PACKET_SIZE;
  36. if (av_new_packet(pkt, size) < 0)
  37. return AVERROR(ENOMEM);
  38. pkt->pos= avio_tell(s->pb);
  39. pkt->stream_index = 0;
  40. ret = ffio_read_partial(s->pb, pkt->data, size);
  41. if (ret < 0) {
  42. av_packet_unref(pkt);
  43. return ret;
  44. }
  45. av_shrink_packet(pkt, ret);
  46. return ret;
  47. }
  48. int ff_raw_audio_read_header(AVFormatContext *s)
  49. {
  50. AVStream *st = avformat_new_stream(s, NULL);
  51. if (!st)
  52. return AVERROR(ENOMEM);
  53. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  54. st->codecpar->codec_id = s->iformat->raw_codec_id;
  55. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  56. st->start_time = 0;
  57. /* the parameters will be extracted from the compressed bitstream */
  58. return 0;
  59. }
  60. /* MPEG-1/H.263 input */
  61. int ff_raw_video_read_header(AVFormatContext *s)
  62. {
  63. AVStream *st;
  64. FFRawVideoDemuxerContext *s1 = s->priv_data;
  65. int ret = 0;
  66. st = avformat_new_stream(s, NULL);
  67. if (!st) {
  68. ret = AVERROR(ENOMEM);
  69. goto fail;
  70. }
  71. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  72. st->codecpar->codec_id = s->iformat->raw_codec_id;
  73. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  74. st->internal->avctx->framerate = s1->framerate;
  75. avpriv_set_pts_info(st, 64, 1, 1200000);
  76. fail:
  77. return ret;
  78. }
  79. int ff_raw_data_read_header(AVFormatContext *s)
  80. {
  81. AVStream *st = avformat_new_stream(s, NULL);
  82. if (!st)
  83. return AVERROR(ENOMEM);
  84. st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
  85. st->codecpar->codec_id = s->iformat->raw_codec_id;
  86. st->start_time = 0;
  87. return 0;
  88. }
  89. /* Note: Do not forget to add new entries to the Makefile as well. */
  90. #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
  91. #define DEC AV_OPT_FLAG_DECODING_PARAM
  92. const AVOption ff_rawvideo_options[] = {
  93. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC},
  94. { NULL },
  95. };
  96. #if CONFIG_DATA_DEMUXER
  97. AVInputFormat ff_data_demuxer = {
  98. .name = "data",
  99. .long_name = NULL_IF_CONFIG_SMALL("raw data"),
  100. .read_header = ff_raw_data_read_header,
  101. .read_packet = ff_raw_read_partial_packet,
  102. .raw_codec_id = AV_CODEC_ID_NONE,
  103. .flags = AVFMT_NOTIMESTAMPS,
  104. };
  105. #endif
  106. #if CONFIG_MJPEG_DEMUXER
  107. static int mjpeg_probe(AVProbeData *p)
  108. {
  109. int i;
  110. int state = -1;
  111. int nb_invalid = 0;
  112. int nb_frames = 0;
  113. for (i=0; i<p->buf_size-2; i++) {
  114. int c;
  115. if (p->buf[i] != 0xFF)
  116. continue;
  117. c = p->buf[i+1];
  118. switch (c) {
  119. case 0xD8:
  120. state = 0xD8;
  121. break;
  122. case 0xC0:
  123. case 0xC1:
  124. case 0xC2:
  125. case 0xC3:
  126. case 0xC5:
  127. case 0xC6:
  128. case 0xC7:
  129. case 0xF7:
  130. if (state == 0xD8) {
  131. state = 0xC0;
  132. } else
  133. nb_invalid++;
  134. break;
  135. case 0xDA:
  136. if (state == 0xC0) {
  137. state = 0xDA;
  138. } else
  139. nb_invalid++;
  140. break;
  141. case 0xD9:
  142. if (state == 0xDA) {
  143. state = 0xD9;
  144. nb_frames++;
  145. } else
  146. nb_invalid++;
  147. break;
  148. default:
  149. if ( (c >= 0x02 && c <= 0xBF)
  150. || c == 0xC8) {
  151. nb_invalid++;
  152. }
  153. }
  154. }
  155. if (nb_invalid*4 + 1 < nb_frames) {
  156. static const char ct_jpeg[] = "\r\nContent-Type: image/jpeg\r\n";
  157. int i;
  158. for (i=0; i<FFMIN(p->buf_size - (int)sizeof(ct_jpeg), 100); i++)
  159. if (!memcmp(p->buf + i, ct_jpeg, sizeof(ct_jpeg) - 1))
  160. return AVPROBE_SCORE_EXTENSION;
  161. if (nb_invalid == 0 && nb_frames > 2)
  162. return AVPROBE_SCORE_EXTENSION / 2;
  163. return AVPROBE_SCORE_EXTENSION / 4;
  164. }
  165. return 0;
  166. }
  167. FF_DEF_RAWVIDEO_DEMUXER2(mjpeg, "raw MJPEG video", mjpeg_probe, "mjpg,mjpeg,mpo", AV_CODEC_ID_MJPEG, AVFMT_GENERIC_INDEX|AVFMT_NOTIMESTAMPS)
  168. #endif