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.

256 lines
7.6KB

  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 "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 (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 = avformat_new_stream(s, NULL);
  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. /* the parameters will be extracted from the compressed bitstream */
  116. return 0;
  117. }
  118. /* MPEG-1/H.263 input */
  119. int ff_raw_video_read_header(AVFormatContext *s,
  120. AVFormatParameters *ap)
  121. {
  122. AVStream *st;
  123. FFRawVideoDemuxerContext *s1 = s->priv_data;
  124. AVRational framerate;
  125. int ret = 0;
  126. st = avformat_new_stream(s, NULL);
  127. if (!st) {
  128. ret = AVERROR(ENOMEM);
  129. goto fail;
  130. }
  131. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  132. st->codec->codec_id = s->iformat->value;
  133. st->need_parsing = AVSTREAM_PARSE_FULL;
  134. if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
  135. av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
  136. goto fail;
  137. }
  138. st->codec->time_base = (AVRational){framerate.den, framerate.num};
  139. av_set_pts_info(st, 64, 1, 1200000);
  140. fail:
  141. return ret;
  142. }
  143. /* Note: Do not forget to add new entries to the Makefile as well. */
  144. #define OFFSET(x) offsetof(FFRawVideoDemuxerContext, x)
  145. #define DEC AV_OPT_FLAG_DECODING_PARAM
  146. const AVOption ff_rawvideo_options[] = {
  147. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC},
  148. { NULL },
  149. };
  150. #if CONFIG_G722_DEMUXER
  151. AVInputFormat ff_g722_demuxer = {
  152. .name = "g722",
  153. .long_name = NULL_IF_CONFIG_SMALL("raw G.722"),
  154. .read_header = ff_raw_read_header,
  155. .read_packet = ff_raw_read_partial_packet,
  156. .flags= AVFMT_GENERIC_INDEX,
  157. .extensions = "g722,722",
  158. .value = CODEC_ID_ADPCM_G722,
  159. };
  160. #endif
  161. #if CONFIG_GSM_DEMUXER
  162. AVInputFormat ff_gsm_demuxer = {
  163. .name = "gsm",
  164. .long_name = NULL_IF_CONFIG_SMALL("raw GSM"),
  165. .read_header = ff_raw_audio_read_header,
  166. .read_packet = ff_raw_read_partial_packet,
  167. .flags= AVFMT_GENERIC_INDEX,
  168. .extensions = "gsm",
  169. .value = CODEC_ID_GSM,
  170. };
  171. #endif
  172. #if CONFIG_LATM_DEMUXER
  173. AVInputFormat ff_latm_demuxer = {
  174. .name = "latm",
  175. .long_name = NULL_IF_CONFIG_SMALL("raw LOAS/LATM"),
  176. .read_header = ff_raw_audio_read_header,
  177. .read_packet = ff_raw_read_partial_packet,
  178. .flags= AVFMT_GENERIC_INDEX,
  179. .extensions = "latm",
  180. .value = CODEC_ID_AAC_LATM,
  181. };
  182. #endif
  183. #if CONFIG_MJPEG_DEMUXER
  184. FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg", CODEC_ID_MJPEG)
  185. #endif
  186. #if CONFIG_MLP_DEMUXER
  187. AVInputFormat ff_mlp_demuxer = {
  188. .name = "mlp",
  189. .long_name = NULL_IF_CONFIG_SMALL("raw MLP"),
  190. .read_header = ff_raw_audio_read_header,
  191. .read_packet = ff_raw_read_partial_packet,
  192. .flags= AVFMT_GENERIC_INDEX,
  193. .extensions = "mlp",
  194. .value = CODEC_ID_MLP,
  195. };
  196. #endif
  197. #if CONFIG_TRUEHD_DEMUXER
  198. AVInputFormat ff_truehd_demuxer = {
  199. .name = "truehd",
  200. .long_name = NULL_IF_CONFIG_SMALL("raw TrueHD"),
  201. .read_header = ff_raw_audio_read_header,
  202. .read_packet = ff_raw_read_partial_packet,
  203. .flags= AVFMT_GENERIC_INDEX,
  204. .extensions = "thd",
  205. .value = CODEC_ID_TRUEHD,
  206. };
  207. #endif
  208. #if CONFIG_SHORTEN_DEMUXER
  209. AVInputFormat ff_shorten_demuxer = {
  210. .name = "shn",
  211. .long_name = NULL_IF_CONFIG_SMALL("raw Shorten"),
  212. .read_header = ff_raw_audio_read_header,
  213. .read_packet = ff_raw_read_partial_packet,
  214. .flags = AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK,
  215. .extensions = "shn",
  216. .value = CODEC_ID_SHORTEN,
  217. };
  218. #endif
  219. #if CONFIG_VC1_DEMUXER
  220. FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", CODEC_ID_VC1)
  221. #endif