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.

251 lines
7.5KB

  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 "avio_internal.h"
  24. #include "rawdec.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/parseutils.h"
  27. #include "libavutil/pixdesc.h"
  28. /* raw input */
  29. int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
  30. {
  31. AVStream *st;
  32. enum CodecID id;
  33. st = avformat_new_stream(s, NULL);
  34. if (!st)
  35. return AVERROR(ENOMEM);
  36. id = s->iformat->value;
  37. if (id == CODEC_ID_RAWVIDEO) {
  38. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  39. } else {
  40. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  41. }
  42. st->codec->codec_id = id;
  43. switch(st->codec->codec_type) {
  44. case AVMEDIA_TYPE_AUDIO: {
  45. RawAudioDemuxerContext *s1 = s->priv_data;
  46. st->codec->channels = 1;
  47. if (id == CODEC_ID_ADPCM_G722)
  48. st->codec->sample_rate = 16000;
  49. if (s1 && s1->sample_rate)
  50. st->codec->sample_rate = s1->sample_rate;
  51. if (st->codec->sample_rate <= 0) {
  52. av_log(s, AV_LOG_ERROR, "Invalid sample rate %d specified\n",
  53. st->codec->sample_rate);
  54. return AVERROR(EINVAL);
  55. }
  56. if (s1 && s1->channels)
  57. st->codec->channels = s1->channels;
  58. st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
  59. assert(st->codec->bits_per_coded_sample > 0);
  60. st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
  61. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  62. break;
  63. }
  64. case AVMEDIA_TYPE_VIDEO: {
  65. FFRawVideoDemuxerContext *s1 = s->priv_data;
  66. int width = 0, height = 0, ret = 0;
  67. enum PixelFormat pix_fmt;
  68. AVRational framerate;
  69. if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
  70. av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
  71. goto fail;
  72. }
  73. if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
  74. av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
  75. ret = AVERROR(EINVAL);
  76. goto fail;
  77. }
  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. av_set_pts_info(st, 64, framerate.den, framerate.num);
  83. st->codec->width = width;
  84. st->codec->height = height;
  85. st->codec->pix_fmt = pix_fmt;
  86. fail:
  87. return ret;
  88. }
  89. default:
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. #define RAW_PACKET_SIZE 1024
  95. int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
  96. {
  97. int ret, size;
  98. size = RAW_PACKET_SIZE;
  99. if (av_new_packet(pkt, size) < 0)
  100. return AVERROR(ENOMEM);
  101. pkt->pos= avio_tell(s->pb);
  102. pkt->stream_index = 0;
  103. ret = ffio_read_partial(s->pb, pkt->data, size);
  104. if (ret < 0) {
  105. av_free_packet(pkt);
  106. return ret;
  107. }
  108. pkt->size = ret;
  109. return ret;
  110. }
  111. int ff_raw_audio_read_header(AVFormatContext *s,
  112. AVFormatParameters *ap)
  113. {
  114. AVStream *st = avformat_new_stream(s, NULL);
  115. if (!st)
  116. return AVERROR(ENOMEM);
  117. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  118. st->codec->codec_id = s->iformat->value;
  119. st->need_parsing = AVSTREAM_PARSE_FULL;
  120. st->start_time = 0;
  121. /* the parameters will be extracted from the compressed bitstream */
  122. return 0;
  123. }
  124. /* MPEG-1/H.263 input */
  125. int ff_raw_video_read_header(AVFormatContext *s,
  126. AVFormatParameters *ap)
  127. {
  128. AVStream *st;
  129. FFRawVideoDemuxerContext *s1 = s->priv_data;
  130. AVRational framerate;
  131. int ret = 0;
  132. st = avformat_new_stream(s, NULL);
  133. if (!st) {
  134. ret = AVERROR(ENOMEM);
  135. goto fail;
  136. }
  137. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  138. st->codec->codec_id = s->iformat->value;
  139. st->need_parsing = AVSTREAM_PARSE_FULL;
  140. if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
  141. av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
  142. goto fail;
  143. }
  144. st->codec->time_base = (AVRational){framerate.den, framerate.num};
  145. av_set_pts_info(st, 64, 1, 1200000);
  146. fail:
  147. return ret;
  148. }
  149. /* Note: Do not forget to add new entries to the Makefile as well. */
  150. #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
  151. #define DEC AV_OPT_FLAG_DECODING_PARAM
  152. const AVOption ff_rawvideo_options[] = {
  153. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
  154. { NULL },
  155. };
  156. #if CONFIG_G722_DEMUXER
  157. AVInputFormat ff_g722_demuxer = {
  158. .name = "g722",
  159. .long_name = NULL_IF_CONFIG_SMALL("raw G.722"),
  160. .read_header = ff_raw_read_header,
  161. .read_packet = ff_raw_read_partial_packet,
  162. .flags= AVFMT_GENERIC_INDEX,
  163. .extensions = "g722,722",
  164. .value = CODEC_ID_ADPCM_G722,
  165. };
  166. #endif
  167. #if CONFIG_LATM_DEMUXER
  168. AVInputFormat ff_latm_demuxer = {
  169. .name = "latm",
  170. .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
  171. .read_header = ff_raw_audio_read_header,
  172. .read_packet = ff_raw_read_partial_packet,
  173. .flags= AVFMT_GENERIC_INDEX,
  174. .extensions = "latm",
  175. .value = CODEC_ID_AAC_LATM,
  176. };
  177. #endif
  178. #if CONFIG_MJPEG_DEMUXER
  179. FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
  180. #endif
  181. #if CONFIG_MLP_DEMUXER
  182. AVInputFormat ff_mlp_demuxer = {
  183. .name = "mlp",
  184. .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
  185. .read_header = ff_raw_audio_read_header,
  186. .read_packet = ff_raw_read_partial_packet,
  187. .flags= AVFMT_GENERIC_INDEX,
  188. .extensions = "mlp",
  189. .value = CODEC_ID_MLP,
  190. };
  191. #endif
  192. #if CONFIG_TRUEHD_DEMUXER
  193. AVInputFormat ff_truehd_demuxer = {
  194. .name = "truehd",
  195. .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
  196. .read_header = ff_raw_audio_read_header,
  197. .read_packet = ff_raw_read_partial_packet,
  198. .flags= AVFMT_GENERIC_INDEX,
  199. .extensions = "thd",
  200. .value = CODEC_ID_TRUEHD,
  201. };
  202. #endif
  203. #if CONFIG_SHORTEN_DEMUXER
  204. AVInputFormat ff_shorten_demuxer = {
  205. .name = "shn",
  206. .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
  207. .read_header = ff_raw_audio_read_header,
  208. .read_packet = ff_raw_read_partial_packet,
  209. .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
  210. .extensions = "shn",
  211. .value = CODEC_ID_SHORTEN,
  212. };
  213. #endif
  214. #if CONFIG_VC1_DEMUXER
  215. FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
  216. #endif