rawdec.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /* raw input */
  31. int ff_raw_read_header(AVFormatContext *s)
  32. {
  33. AVStream *st;
  34. enum AVCodecID id;
  35. st = avformat_new_stream(s, NULL);
  36. if (!st)
  37. return AVERROR(ENOMEM);
  38. id = s->iformat->raw_codec_id;
  39. if (id == AV_CODEC_ID_RAWVIDEO) {
  40. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  41. } else {
  42. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  43. }
  44. st->codec->codec_id = id;
  45. switch(st->codec->codec_type) {
  46. case AVMEDIA_TYPE_AUDIO: {
  47. RawAudioDemuxerContext *s1 = s->priv_data;
  48. st->codec->channels = 1;
  49. if (id == AV_CODEC_ID_ADPCM_G722)
  50. st->codec->sample_rate = 16000;
  51. if (s1 && s1->sample_rate)
  52. st->codec->sample_rate = s1->sample_rate;
  53. if (st->codec->sample_rate <= 0) {
  54. av_log(s, AV_LOG_WARNING, "Invalid sample rate %d specified using default of 44100\n",
  55. st->codec->sample_rate);
  56. st->codec->sample_rate= 44100;
  57. }
  58. if (s1 && s1->channels)
  59. st->codec->channels = s1->channels;
  60. st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
  61. av_assert0(st->codec->bits_per_coded_sample > 0);
  62. st->codec->block_align = st->codec->bits_per_coded_sample*st->codec->channels/8;
  63. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  64. break;
  65. }
  66. case AVMEDIA_TYPE_VIDEO: {
  67. FFRawVideoDemuxerContext *s1 = s->priv_data;
  68. int width = 0, height = 0, ret = 0;
  69. enum PixelFormat pix_fmt;
  70. AVRational framerate;
  71. if (s1->video_size && (ret = av_parse_video_size(&width, &height, s1->video_size)) < 0) {
  72. av_log(s, AV_LOG_ERROR, "Couldn't parse video size.\n");
  73. goto fail;
  74. }
  75. if ((pix_fmt = av_get_pix_fmt(s1->pixel_format)) == PIX_FMT_NONE) {
  76. av_log(s, AV_LOG_ERROR, "No such pixel format: %s.\n", s1->pixel_format);
  77. ret = AVERROR(EINVAL);
  78. goto fail;
  79. }
  80. if ((ret = av_parse_video_rate(&framerate, s1->framerate)) < 0) {
  81. av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s1->framerate);
  82. goto fail;
  83. }
  84. avpriv_set_pts_info(st, 64, framerate.den, framerate.num);
  85. st->codec->width = width;
  86. st->codec->height = height;
  87. st->codec->pix_fmt = pix_fmt;
  88. fail:
  89. return ret;
  90. }
  91. default:
  92. return -1;
  93. }
  94. return 0;
  95. }
  96. #define RAW_PACKET_SIZE 1024
  97. int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
  98. {
  99. int ret, size;
  100. size = RAW_PACKET_SIZE;
  101. if (av_new_packet(pkt, size) < 0)
  102. return AVERROR(ENOMEM);
  103. pkt->pos= avio_tell(s->pb);
  104. pkt->stream_index = 0;
  105. ret = ffio_read_partial(s->pb, pkt->data, size);
  106. if (ret < 0) {
  107. av_free_packet(pkt);
  108. return ret;
  109. }
  110. av_shrink_packet(pkt, ret);
  111. return ret;
  112. }
  113. int ff_raw_audio_read_header(AVFormatContext *s)
  114. {
  115. AVStream *st = avformat_new_stream(s, NULL);
  116. if (!st)
  117. return AVERROR(ENOMEM);
  118. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  119. st->codec->codec_id = s->iformat->raw_codec_id;
  120. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  121. st->start_time = 0;
  122. /* the parameters will be extracted from the compressed bitstream */
  123. return 0;
  124. }
  125. /* MPEG-1/H.263 input */
  126. int ff_raw_video_read_header(AVFormatContext *s)
  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->raw_codec_id;
  139. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  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 = av_inv_q(framerate);
  145. avpriv_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. .raw_codec_id = AV_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. .raw_codec_id = AV_CODEC_ID_AAC_LATM,
  176. };
  177. #endif
  178. #if CONFIG_MJPEG_DEMUXER
  179. FF_DEF_RAWVIDEO_DEMUXER(mjpeg, "raw MJPEG video", NULL, "mjpg,mjpeg,mpo", AV_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. .raw_codec_id = AV_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. .raw_codec_id = AV_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. .raw_codec_id = AV_CODEC_ID_SHORTEN,
  212. };
  213. #endif
  214. #if CONFIG_VC1_DEMUXER
  215. FF_DEF_RAWVIDEO_DEMUXER(vc1, "raw VC-1", NULL, "vc1", AV_CODEC_ID_VC1)
  216. #endif