/libavformat/rawdec.c

http://github.com/FFmpeg/FFmpeg · C · 214 lines · 163 code · 27 blank · 24 comment · 26 complexity · 940d3c7c69bc86661c0b045e692f6566 MD5 · raw file

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