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.

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