avisynth.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * AVISynth support
  3. * Copyright (c) 2006 DivX, Inc.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "internal.h"
  23. #include "riff.h"
  24. #include <windows.h>
  25. #include <vfw.h>
  26. typedef struct {
  27. PAVISTREAM handle;
  28. AVISTREAMINFO info;
  29. DWORD read;
  30. LONG chunck_size;
  31. LONG chunck_samples;
  32. } AVISynthStream;
  33. typedef struct {
  34. PAVIFILE file;
  35. AVISynthStream *streams;
  36. int nb_streams;
  37. int next_stream;
  38. } AVISynthContext;
  39. static int avisynth_read_header(AVFormatContext *s, AVFormatParameters *ap)
  40. {
  41. AVISynthContext *avs = s->priv_data;
  42. HRESULT res;
  43. AVIFILEINFO info;
  44. DWORD id;
  45. AVStream *st;
  46. AVISynthStream *stream;
  47. AVIFileInit();
  48. res = AVIFileOpen(&avs->file, s->filename, OF_READ|OF_SHARE_DENY_WRITE, NULL);
  49. if (res != S_OK)
  50. {
  51. av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld", res);
  52. AVIFileExit();
  53. return -1;
  54. }
  55. res = AVIFileInfo(avs->file, &info, sizeof(info));
  56. if (res != S_OK)
  57. {
  58. av_log(s, AV_LOG_ERROR, "AVIFileInfo failed with error %ld", res);
  59. AVIFileExit();
  60. return -1;
  61. }
  62. avs->streams = av_mallocz(info.dwStreams * sizeof(AVISynthStream));
  63. for (id=0; id<info.dwStreams; id++)
  64. {
  65. stream = &avs->streams[id];
  66. stream->read = 0;
  67. if (AVIFileGetStream(avs->file, &stream->handle, 0, id) == S_OK)
  68. {
  69. if (AVIStreamInfo(stream->handle, &stream->info, sizeof(stream->info)) == S_OK)
  70. {
  71. if (stream->info.fccType == streamtypeAUDIO)
  72. {
  73. WAVEFORMATEX wvfmt;
  74. LONG struct_size = sizeof(WAVEFORMATEX);
  75. if (AVIStreamReadFormat(stream->handle, 0, &wvfmt, &struct_size) != S_OK)
  76. continue;
  77. st = avformat_new_stream(s, NULL);
  78. st->id = id;
  79. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  80. st->codec->block_align = wvfmt.nBlockAlign;
  81. st->codec->channels = wvfmt.nChannels;
  82. st->codec->sample_rate = wvfmt.nSamplesPerSec;
  83. st->codec->bit_rate = wvfmt.nAvgBytesPerSec * 8;
  84. st->codec->bits_per_coded_sample = wvfmt.wBitsPerSample;
  85. stream->chunck_samples = wvfmt.nSamplesPerSec * (uint64_t)info.dwScale / (uint64_t)info.dwRate;
  86. stream->chunck_size = stream->chunck_samples * wvfmt.nChannels * wvfmt.wBitsPerSample / 8;
  87. st->codec->codec_tag = wvfmt.wFormatTag;
  88. st->codec->codec_id = ff_wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_coded_sample);
  89. }
  90. else if (stream->info.fccType == streamtypeVIDEO)
  91. {
  92. BITMAPINFO imgfmt;
  93. LONG struct_size = sizeof(BITMAPINFO);
  94. stream->chunck_size = stream->info.dwSampleSize;
  95. stream->chunck_samples = 1;
  96. if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK)
  97. continue;
  98. st = avformat_new_stream(s, NULL);
  99. st->id = id;
  100. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  101. st->r_frame_rate.num = stream->info.dwRate;
  102. st->r_frame_rate.den = stream->info.dwScale;
  103. st->codec->width = imgfmt.bmiHeader.biWidth;
  104. st->codec->height = imgfmt.bmiHeader.biHeight;
  105. st->codec->bits_per_coded_sample = imgfmt.bmiHeader.biBitCount;
  106. st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale;
  107. st->codec->codec_tag = imgfmt.bmiHeader.biCompression;
  108. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, imgfmt.bmiHeader.biCompression);
  109. if (st->codec->codec_id == CODEC_ID_RAWVIDEO && imgfmt.bmiHeader.biCompression== BI_RGB) {
  110. st->codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
  111. if (st->codec->extradata) {
  112. st->codec->extradata_size = 9;
  113. memcpy(st->codec->extradata, "BottomUp", 9);
  114. }
  115. }
  116. st->duration = stream->info.dwLength;
  117. }
  118. else
  119. {
  120. AVIStreamRelease(stream->handle);
  121. continue;
  122. }
  123. avs->nb_streams++;
  124. st->codec->stream_codec_tag = stream->info.fccHandler;
  125. avpriv_set_pts_info(st, 64, info.dwScale, info.dwRate);
  126. st->start_time = stream->info.dwStart;
  127. }
  128. }
  129. }
  130. return 0;
  131. }
  132. static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
  133. {
  134. AVISynthContext *avs = s->priv_data;
  135. HRESULT res;
  136. AVISynthStream *stream;
  137. int stream_id = avs->next_stream;
  138. LONG read_size;
  139. // handle interleaving manually...
  140. stream = &avs->streams[stream_id];
  141. if (stream->read >= stream->info.dwLength)
  142. return AVERROR(EIO);
  143. if (av_new_packet(pkt, stream->chunck_size))
  144. return AVERROR(EIO);
  145. pkt->stream_index = stream_id;
  146. pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
  147. res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
  148. pkt->size = read_size;
  149. stream->read += stream->chunck_samples;
  150. // prepare for the next stream to read
  151. do {
  152. avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
  153. } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
  154. return (res == S_OK) ? pkt->size : -1;
  155. }
  156. static int avisynth_read_close(AVFormatContext *s)
  157. {
  158. AVISynthContext *avs = s->priv_data;
  159. int i;
  160. for (i=0;i<avs->nb_streams;i++)
  161. {
  162. AVIStreamRelease(avs->streams[i].handle);
  163. }
  164. av_free(avs->streams);
  165. AVIFileRelease(avs->file);
  166. AVIFileExit();
  167. return 0;
  168. }
  169. static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  170. {
  171. AVISynthContext *avs = s->priv_data;
  172. int stream_id;
  173. for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
  174. {
  175. avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
  176. }
  177. return 0;
  178. }
  179. AVInputFormat ff_avisynth_demuxer = {
  180. .name = "avs",
  181. .long_name = NULL_IF_CONFIG_SMALL("AVISynth"),
  182. .priv_data_size = sizeof(AVISynthContext),
  183. .read_header = avisynth_read_header,
  184. .read_packet = avisynth_read_packet,
  185. .read_close = avisynth_read_close,
  186. .read_seek = avisynth_read_seek,
  187. .extensions = "avs",
  188. };