rawdec.c 6.3 KB

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