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.

219 lines
6.3KB

  1. /*
  2. * RAW demuxers
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2005 Alex Beregszaszi
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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/intreadwrite.h"
  30. #define RAW_PACKET_SIZE 1024
  31. int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
  32. {
  33. int ret, size;
  34. size = RAW_PACKET_SIZE;
  35. if (av_new_packet(pkt, size) < 0)
  36. return AVERROR(ENOMEM);
  37. pkt->pos= avio_tell(s->pb);
  38. pkt->stream_index = 0;
  39. ret = ffio_read_partial(s->pb, pkt->data, size);
  40. if (ret < 0) {
  41. av_packet_unref(pkt);
  42. return ret;
  43. } else if (ret < size) {
  44. /* initialize end of packet for partial reads to avoid reading
  45. * uninitialized data on allowed overreads */
  46. memset(pkt->data + ret, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  47. }
  48. pkt->size = ret;
  49. return ret;
  50. }
  51. int ff_raw_audio_read_header(AVFormatContext *s)
  52. {
  53. AVStream *st = avformat_new_stream(s, NULL);
  54. if (!st)
  55. return AVERROR(ENOMEM);
  56. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  57. st->codecpar->codec_id = s->iformat->raw_codec_id;
  58. st->need_parsing = AVSTREAM_PARSE_FULL;
  59. st->start_time = 0;
  60. /* the parameters will be extracted from the compressed bitstream */
  61. return 0;
  62. }
  63. /* MPEG-1/H.263 input */
  64. int ff_raw_video_read_header(AVFormatContext *s)
  65. {
  66. AVStream *st;
  67. FFRawVideoDemuxerContext *s1 = s->priv_data;
  68. AVRational framerate;
  69. int ret = 0;
  70. st = avformat_new_stream(s, NULL);
  71. if (!st) {
  72. ret = AVERROR(ENOMEM);
  73. goto fail;
  74. }
  75. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  76. st->codecpar->codec_id = s->iformat->raw_codec_id;
  77. st->need_parsing = AVSTREAM_PARSE_FULL;
  78. if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
  79. av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
  80. goto fail;
  81. }
  82. st->avg_frame_rate = framerate;
  83. avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
  84. fail:
  85. return ret;
  86. }
  87. /* Note: Do not forget to add new entries to the Makefile as well. */
  88. #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
  89. #define DEC AV_OPT_FLAG_DECODING_PARAM
  90. const AVOption ff_rawvideo_options[] = {
  91. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
  92. { NULL },
  93. };
  94. #if CONFIG_LATM_DEMUXER
  95. #define LOAS_SYNC_WORD 0x2b7
  96. static int latm_read_probe(AVProbeData *p)
  97. {
  98. int max_frames = 0, first_frames = 0;
  99. int fsize, frames;
  100. uint8_t *buf0 = p->buf;
  101. uint8_t *buf2;
  102. uint8_t *buf;
  103. uint8_t *end = buf0 + p->buf_size - 3;
  104. buf = buf0;
  105. for (; buf < end; buf = buf2 + 1) {
  106. buf2 = buf;
  107. for (frames = 0; buf2 < end; frames++) {
  108. uint32_t header = AV_RB24(buf2);
  109. if ((header >> 13) != LOAS_SYNC_WORD) {
  110. if (buf != buf0) {
  111. // Found something that isn't a LOAS header, starting
  112. // from a position other than the start of the buffer.
  113. // Discard the count we've accumulated so far since it
  114. // probably was a false positive.
  115. frames = 0;
  116. }
  117. break;
  118. }
  119. fsize = (header & 0x1FFF) + 3;
  120. if (fsize < 7)
  121. break;
  122. buf2 += fsize;
  123. }
  124. max_frames = FFMAX(max_frames, frames);
  125. if (buf == buf0)
  126. first_frames = frames;
  127. }
  128. if (first_frames >= 3)
  129. return AVPROBE_SCORE_EXTENSION + 1;
  130. else if (max_frames > 100)
  131. return AVPROBE_SCORE_EXTENSION;
  132. else if (max_frames >= 3)
  133. return AVPROBE_SCORE_EXTENSION / 2;
  134. else if (max_frames > 1)
  135. return 1;
  136. else
  137. return 0;
  138. }
  139. AVInputFormat ff_latm_demuxer = {
  140. .name = "latm",
  141. .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
  142. .read_probe = latm_read_probe,
  143. .read_header = ff_raw_audio_read_header,
  144. .read_packet = ff_raw_read_partial_packet,
  145. .flags = AVFMT_GENERIC_INDEX,
  146. .extensions = "latm",
  147. .raw_codec_id = AV_CODEC_ID_AAC_LATM,
  148. };
  149. #endif
  150. #if CONFIG_MJPEG_DEMUXER
  151. FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", AV_CODEC_ID_MJPEG)
  152. #endif
  153. #if CONFIG_MLP_DEMUXER
  154. AVInputFormat ff_mlp_demuxer = {
  155. .name = "mlp",
  156. .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
  157. .read_header = ff_raw_audio_read_header,
  158. .read_packet = ff_raw_read_partial_packet,
  159. .flags = AVFMT_GENERIC_INDEX,
  160. .extensions = "mlp",
  161. .raw_codec_id = AV_CODEC_ID_MLP,
  162. };
  163. #endif
  164. #if CONFIG_TRUEHD_DEMUXER
  165. AVInputFormat ff_truehd_demuxer = {
  166. .name = "truehd",
  167. .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
  168. .read_header = ff_raw_audio_read_header,
  169. .read_packet = ff_raw_read_partial_packet,
  170. .flags = AVFMT_GENERIC_INDEX,
  171. .extensions = "thd",
  172. .raw_codec_id = AV_CODEC_ID_TRUEHD,
  173. };
  174. #endif
  175. #if CONFIG_SHORTEN_DEMUXER
  176. AVInputFormat ff_shorten_demuxer = {
  177. .name = "shn",
  178. .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
  179. .read_header = ff_raw_audio_read_header,
  180. .read_packet = ff_raw_read_partial_packet,
  181. .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
  182. .extensions = "shn",
  183. .raw_codec_id = AV_CODEC_ID_SHORTEN,
  184. };
  185. #endif
  186. #if CONFIG_VC1_DEMUXER
  187. FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", AV_CODEC_ID_VC1)
  188. #endif