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.

284 lines
7.7KB

  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_free_packet(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->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  54. st->codec->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->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  72. st->codec->codec_id = s->iformat->raw_codec_id;
  73. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  74. st->codec->framerate = s1->framerate;
  75. st->codec->time_base = av_inv_q(s1->framerate);
  76. avpriv_set_pts_info(st, 64, 1, 1200000);
  77. fail:
  78. return ret;
  79. }
  80. int ff_raw_data_read_header(AVFormatContext *s)
  81. {
  82. AVStream *st = avformat_new_stream(s, NULL);
  83. if (!st)
  84. return AVERROR(ENOMEM);
  85. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  86. st->codec->codec_id = s->iformat->raw_codec_id;
  87. st->start_time = 0;
  88. return 0;
  89. }
  90. /* Note: Do not forget to add new entries to the Makefile as well. */
  91. #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
  92. #define DEC AV_OPT_FLAG_DECODING_PARAM
  93. const AVOption ff_rawvideo_options[] = {
  94. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC},
  95. { NULL },
  96. };
  97. #if CONFIG_DATA_DEMUXER
  98. AVInputFormat ff_data_demuxer = {
  99. .name = "data",
  100. .long_name = NULL_IF_CONFIG_SMALL("raw data"),
  101. .read_header = ff_raw_data_read_header,
  102. .read_packet = ff_raw_read_partial_packet,
  103. .raw_codec_id = AV_CODEC_ID_NONE,
  104. .flags = AVFMT_NOTIMESTAMPS,
  105. };
  106. #endif
  107. #if CONFIG_LATM_DEMUXER
  108. AVInputFormat ff_latm_demuxer = {
  109. .name = "latm",
  110. .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
  111. .read_header = ff_raw_audio_read_header,
  112. .read_packet = ff_raw_read_partial_packet,
  113. .flags = AVFMT_GENERIC_INDEX | AVFMT_NOTIMESTAMPS,
  114. .extensions = "latm",
  115. .raw_codec_id = AV_CODEC_ID_AAC_LATM,
  116. };
  117. #endif
  118. #if CONFIG_MJPEG_DEMUXER
  119. static int mjpeg_probe(AVProbeData *p)
  120. {
  121. int i;
  122. int state = -1;
  123. int nb_invalid = 0;
  124. int nb_frames = 0;
  125. for (i=0; i<p->buf_size-2; i++) {
  126. int c;
  127. if (p->buf[i] != 0xFF)
  128. continue;
  129. c = p->buf[i+1];
  130. switch (c) {
  131. case 0xD8:
  132. state = 0xD8;
  133. break;
  134. case 0xC0:
  135. case 0xC1:
  136. case 0xC2:
  137. case 0xC3:
  138. case 0xC5:
  139. case 0xC6:
  140. case 0xC7:
  141. case 0xF7:
  142. if (state == 0xD8) {
  143. state = 0xC0;
  144. } else
  145. nb_invalid++;
  146. break;
  147. case 0xDA:
  148. if (state == 0xC0) {
  149. state = 0xDA;
  150. } else
  151. nb_invalid++;
  152. break;
  153. case 0xD9:
  154. if (state == 0xDA) {
  155. state = 0xD9;
  156. nb_frames++;
  157. } else
  158. nb_invalid++;
  159. break;
  160. default:
  161. if ( (c >= 0x02 && c <= 0xBF)
  162. || c == 0xC8) {
  163. nb_invalid++;
  164. }
  165. }
  166. }
  167. if (nb_invalid*4 + 1 < nb_frames) {
  168. static const char ct_jpeg[] = "\r\nContent-Type: image/jpeg\r\n";
  169. int i;
  170. for (i=0; i<FFMIN(p->buf_size - (int)sizeof(ct_jpeg), 100); i++)
  171. if (!memcmp(p->buf + i, ct_jpeg, sizeof(ct_jpeg) - 1))
  172. return AVPROBE_SCORE_EXTENSION;
  173. if (nb_invalid == 0 && nb_frames > 2)
  174. return AVPROBE_SCORE_EXTENSION / 2;
  175. return AVPROBE_SCORE_EXTENSION / 4;
  176. }
  177. return 0;
  178. }
  179. FF_DEF_RAWVIDEO_DEMUXER2(mjpeg, "raw MJPEG video", mjpeg_probe, "mjpg,mjpeg,mpo", AV_CODEC_ID_MJPEG, AVFMT_GENERIC_INDEX|AVFMT_NOTIMESTAMPS)
  180. #endif
  181. #if CONFIG_MLP_DEMUXER || CONFIG_TRUEHD_DEMUXER
  182. static int av_always_inline mlp_thd_probe(AVProbeData *p, uint32_t sync)
  183. {
  184. const uint8_t *buf, *last_buf = p->buf, *end = p->buf + p->buf_size;
  185. int frames = 0, valid = 0, size = 0;
  186. for (buf = p->buf; buf + 8 <= end; buf++) {
  187. if (AV_RB32(buf + 4) == sync) {
  188. frames++;
  189. if (last_buf + size == buf) {
  190. valid++;
  191. }
  192. last_buf = buf;
  193. size = (AV_RB16(buf) & 0xfff) * 2;
  194. } else if (buf - last_buf == size) {
  195. size += (AV_RB16(buf) & 0xfff) * 2;
  196. }
  197. }
  198. if (valid >= 100)
  199. return AVPROBE_SCORE_MAX;
  200. return 0;
  201. }
  202. #endif
  203. #if CONFIG_MLP_DEMUXER
  204. static int mlp_probe(AVProbeData *p)
  205. {
  206. return mlp_thd_probe(p, 0xf8726fbb);
  207. }
  208. AVInputFormat ff_mlp_demuxer = {
  209. .name = "mlp",
  210. .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
  211. .read_probe = mlp_probe,
  212. .read_header = ff_raw_audio_read_header,
  213. .read_packet = ff_raw_read_partial_packet,
  214. .flags = AVFMT_GENERIC_INDEX | AVFMT_NOTIMESTAMPS,
  215. .extensions = "mlp",
  216. .raw_codec_id = AV_CODEC_ID_MLP,
  217. };
  218. #endif
  219. #if CONFIG_TRUEHD_DEMUXER
  220. static int thd_probe(AVProbeData *p)
  221. {
  222. return mlp_thd_probe(p, 0xf8726fba);
  223. }
  224. AVInputFormat ff_truehd_demuxer = {
  225. .name = "truehd",
  226. .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
  227. .read_probe = thd_probe,
  228. .read_header = ff_raw_audio_read_header,
  229. .read_packet = ff_raw_read_partial_packet,
  230. .flags = AVFMT_GENERIC_INDEX | AVFMT_NOTIMESTAMPS,
  231. .extensions = "thd",
  232. .raw_codec_id = AV_CODEC_ID_TRUEHD,
  233. };
  234. #endif
  235. #if CONFIG_SHORTEN_DEMUXER
  236. AVInputFormat ff_shorten_demuxer = {
  237. .name = "shn",
  238. .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
  239. .read_header = ff_raw_audio_read_header,
  240. .read_packet = ff_raw_read_partial_packet,
  241. .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK|AVFMT_NOTIMESTAMPS,
  242. .extensions = "shn",
  243. .raw_codec_id = AV_CODEC_ID_SHORTEN,
  244. };
  245. #endif
  246. #if CONFIG_VC1_DEMUXER
  247. FF_DEF_RAWVIDEO_DEMUXER2(vc1, "raw VC-1", NULL, "vc1", AV_CODEC_ID_VC1, AVFMT_GENERIC_INDEX|AVFMT_NOTIMESTAMPS)
  248. #endif