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.

263 lines
7.2KB

  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. /* raw input */
  27. int ff_raw_read_header(AVFormatContext *s, AVFormatParameters *ap)
  28. {
  29. AVStream *st;
  30. enum CodecID id;
  31. st = av_new_stream(s, 0);
  32. if (!st)
  33. return AVERROR(ENOMEM);
  34. id = s->iformat->value;
  35. if (id == CODEC_ID_RAWVIDEO) {
  36. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  37. } else {
  38. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  39. }
  40. st->codec->codec_id = id;
  41. switch(st->codec->codec_type) {
  42. case AVMEDIA_TYPE_AUDIO: {
  43. RawAudioDemuxerContext *s1 = s->priv_data;
  44. if (ap->sample_rate)
  45. st->codec->sample_rate = ap->sample_rate;
  46. if (ap->channels)
  47. st->codec->channels = ap->channels;
  48. else st->codec->channels = 1;
  49. if (s1->sample_rate)
  50. st->codec->sample_rate = s1->sample_rate;
  51. if (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. if(ap->time_base.num)
  61. av_set_pts_info(st, 64, ap->time_base.num, ap->time_base.den);
  62. else
  63. av_set_pts_info(st, 64, 1, 25);
  64. st->codec->width = ap->width;
  65. st->codec->height = ap->height;
  66. st->codec->pix_fmt = ap->pix_fmt;
  67. if(st->codec->pix_fmt == PIX_FMT_NONE)
  68. st->codec->pix_fmt= PIX_FMT_YUV420P;
  69. break;
  70. default:
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. #define RAW_PACKET_SIZE 1024
  76. int ff_raw_read_partial_packet(AVFormatContext *s, AVPacket *pkt)
  77. {
  78. int ret, size;
  79. size = RAW_PACKET_SIZE;
  80. if (av_new_packet(pkt, size) < 0)
  81. return AVERROR(ENOMEM);
  82. pkt->pos= avio_tell(s->pb);
  83. pkt->stream_index = 0;
  84. ret = ffio_read_partial(s->pb, pkt->data, size);
  85. if (ret < 0) {
  86. av_free_packet(pkt);
  87. return ret;
  88. }
  89. pkt->size = ret;
  90. return ret;
  91. }
  92. int ff_raw_audio_read_header(AVFormatContext *s,
  93. AVFormatParameters *ap)
  94. {
  95. AVStream *st = av_new_stream(s, 0);
  96. if (!st)
  97. return AVERROR(ENOMEM);
  98. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  99. st->codec->codec_id = s->iformat->value;
  100. st->need_parsing = AVSTREAM_PARSE_FULL;
  101. /* the parameters will be extracted from the compressed bitstream */
  102. return 0;
  103. }
  104. /* MPEG-1/H.263 input */
  105. int ff_raw_video_read_header(AVFormatContext *s,
  106. AVFormatParameters *ap)
  107. {
  108. AVStream *st;
  109. st = av_new_stream(s, 0);
  110. if (!st)
  111. return AVERROR(ENOMEM);
  112. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  113. st->codec->codec_id = s->iformat->value;
  114. st->need_parsing = AVSTREAM_PARSE_FULL;
  115. /* for MJPEG, specify frame rate */
  116. /* for MPEG-4 specify it, too (most MPEG-4 streams do not have the fixed_vop_rate set ...)*/
  117. if (ap->time_base.num) {
  118. st->codec->time_base= ap->time_base;
  119. } else if ( st->codec->codec_id == CODEC_ID_MJPEG ||
  120. st->codec->codec_id == CODEC_ID_MPEG4 ||
  121. st->codec->codec_id == CODEC_ID_DIRAC ||
  122. st->codec->codec_id == CODEC_ID_DNXHD ||
  123. st->codec->codec_id == CODEC_ID_VC1 ||
  124. st->codec->codec_id == CODEC_ID_H264) {
  125. st->codec->time_base= (AVRational){1,25};
  126. }
  127. av_set_pts_info(st, 64, 1, 1200000);
  128. return 0;
  129. }
  130. /* Note: Do not forget to add new entries to the Makefile as well. */
  131. static const AVOption audio_options[] = {
  132. { "sample_rate", "", offsetof(RawAudioDemuxerContext, sample_rate), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  133. { "channels", "", offsetof(RawAudioDemuxerContext, channels), FF_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  134. { NULL },
  135. };
  136. const AVClass ff_rawaudio_demuxer_class = {
  137. .class_name = "rawaudio demuxer",
  138. .item_name = av_default_item_name,
  139. .option = audio_options,
  140. .version = LIBAVUTIL_VERSION_INT,
  141. };
  142. #if CONFIG_G722_DEMUXER
  143. AVInputFormat ff_g722_demuxer = {
  144. "g722",
  145. NULL_IF_CONFIG_SMALL("raw G.722"),
  146. sizeof(RawAudioDemuxerContext),
  147. NULL,
  148. ff_raw_read_header,
  149. ff_raw_read_partial_packet,
  150. .flags= AVFMT_GENERIC_INDEX,
  151. .extensions = "g722,722",
  152. .value = CODEC_ID_ADPCM_G722,
  153. .priv_class = &ff_rawaudio_demuxer_class,
  154. };
  155. #endif
  156. #if CONFIG_GSM_DEMUXER
  157. AVInputFormat ff_gsm_demuxer = {
  158. "gsm",
  159. NULL_IF_CONFIG_SMALL("raw GSM"),
  160. 0,
  161. NULL,
  162. ff_raw_audio_read_header,
  163. ff_raw_read_partial_packet,
  164. .flags= AVFMT_GENERIC_INDEX,
  165. .extensions = "gsm",
  166. .value = CODEC_ID_GSM,
  167. };
  168. #endif
  169. #if CONFIG_MJPEG_DEMUXER
  170. AVInputFormat ff_mjpeg_demuxer = {
  171. "mjpeg",
  172. NULL_IF_CONFIG_SMALL("raw MJPEG video"),
  173. 0,
  174. NULL,
  175. ff_raw_video_read_header,
  176. ff_raw_read_partial_packet,
  177. .flags= AVFMT_GENERIC_INDEX,
  178. .extensions = "mjpg,mjpeg",
  179. .value = CODEC_ID_MJPEG,
  180. };
  181. #endif
  182. #if CONFIG_MLP_DEMUXER
  183. AVInputFormat ff_mlp_demuxer = {
  184. "mlp",
  185. NULL_IF_CONFIG_SMALL("raw MLP"),
  186. 0,
  187. NULL,
  188. ff_raw_audio_read_header,
  189. ff_raw_read_partial_packet,
  190. .flags= AVFMT_GENERIC_INDEX,
  191. .extensions = "mlp",
  192. .value = CODEC_ID_MLP,
  193. };
  194. #endif
  195. #if CONFIG_TRUEHD_DEMUXER
  196. AVInputFormat ff_truehd_demuxer = {
  197. "truehd",
  198. NULL_IF_CONFIG_SMALL("raw TrueHD"),
  199. 0,
  200. NULL,
  201. ff_raw_audio_read_header,
  202. 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. "shn",
  211. NULL_IF_CONFIG_SMALL("raw Shorten"),
  212. 0,
  213. NULL,
  214. ff_raw_audio_read_header,
  215. ff_raw_read_partial_packet,
  216. .flags= AVFMT_GENERIC_INDEX,
  217. .extensions = "shn",
  218. .value = CODEC_ID_SHORTEN,
  219. };
  220. #endif
  221. #if CONFIG_VC1_DEMUXER
  222. AVInputFormat ff_vc1_demuxer = {
  223. "vc1",
  224. NULL_IF_CONFIG_SMALL("raw VC-1"),
  225. 0,
  226. NULL /* vc1_probe */,
  227. ff_raw_video_read_header,
  228. ff_raw_read_partial_packet,
  229. .extensions = "vc1",
  230. .value = CODEC_ID_VC1,
  231. };
  232. #endif