avisynth.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * AviSynth support
  3. * Copyright (c) 2006 DivX, Inc.
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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)
  40. {
  41. AviSynthContext *avs = s->priv_data;
  42. HRESULT res;
  43. AVIFILEINFO info;
  44. DWORD id;
  45. AVStream *st;
  46. AviSynthStream *stream;
  47. wchar_t filename_wchar[1024] = { 0 };
  48. char filename_char[1024] = { 0 };
  49. AVIFileInit();
  50. /* AviSynth cannot accept UTF-8 file names. */
  51. MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wchar, 1024);
  52. WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wchar, -1, filename_char, 1024, NULL, NULL);
  53. res = AVIFileOpen(&avs->file, filename_char, OF_READ|OF_SHARE_DENY_WRITE, NULL);
  54. if (res != S_OK)
  55. {
  56. av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld", res);
  57. AVIFileExit();
  58. return -1;
  59. }
  60. res = AVIFileInfo(avs->file, &info, sizeof(info));
  61. if (res != S_OK)
  62. {
  63. av_log(s, AV_LOG_ERROR, "AVIFileInfo failed with error %ld", res);
  64. AVIFileExit();
  65. return -1;
  66. }
  67. avs->streams = av_mallocz(info.dwStreams * sizeof(AviSynthStream));
  68. for (id=0; id<info.dwStreams; id++)
  69. {
  70. stream = &avs->streams[id];
  71. stream->read = 0;
  72. if (AVIFileGetStream(avs->file, &stream->handle, 0, id) == S_OK)
  73. {
  74. if (AVIStreamInfo(stream->handle, &stream->info, sizeof(stream->info)) == S_OK)
  75. {
  76. if (stream->info.fccType == streamtypeAUDIO)
  77. {
  78. WAVEFORMATEX wvfmt;
  79. LONG struct_size = sizeof(WAVEFORMATEX);
  80. if (AVIStreamReadFormat(stream->handle, 0, &wvfmt, &struct_size) != S_OK)
  81. continue;
  82. st = avformat_new_stream(s, NULL);
  83. st->id = id;
  84. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  85. st->codec->block_align = wvfmt.nBlockAlign;
  86. st->codec->channels = wvfmt.nChannels;
  87. st->codec->sample_rate = wvfmt.nSamplesPerSec;
  88. st->codec->bit_rate = wvfmt.nAvgBytesPerSec * 8;
  89. st->codec->bits_per_coded_sample = wvfmt.wBitsPerSample;
  90. stream->chunck_samples = wvfmt.nSamplesPerSec * (uint64_t)info.dwScale / (uint64_t)info.dwRate;
  91. stream->chunck_size = stream->chunck_samples * wvfmt.nChannels * wvfmt.wBitsPerSample / 8;
  92. st->codec->codec_tag = wvfmt.wFormatTag;
  93. st->codec->codec_id = ff_wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_coded_sample);
  94. }
  95. else if (stream->info.fccType == streamtypeVIDEO)
  96. {
  97. BITMAPINFO imgfmt;
  98. LONG struct_size = sizeof(BITMAPINFO);
  99. stream->chunck_size = stream->info.dwSampleSize;
  100. stream->chunck_samples = 1;
  101. if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK)
  102. continue;
  103. st = avformat_new_stream(s, NULL);
  104. st->id = id;
  105. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  106. st->avg_frame_rate.num = stream->info.dwRate;
  107. st->avg_frame_rate.den = stream->info.dwScale;
  108. st->codec->width = imgfmt.bmiHeader.biWidth;
  109. st->codec->height = imgfmt.bmiHeader.biHeight;
  110. st->codec->bits_per_coded_sample = imgfmt.bmiHeader.biBitCount;
  111. st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale;
  112. st->codec->codec_tag = imgfmt.bmiHeader.biCompression;
  113. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, imgfmt.bmiHeader.biCompression);
  114. st->duration = stream->info.dwLength;
  115. }
  116. else
  117. {
  118. AVIStreamRelease(stream->handle);
  119. continue;
  120. }
  121. avs->nb_streams++;
  122. st->codec->stream_codec_tag = stream->info.fccHandler;
  123. avpriv_set_pts_info(st, 64, info.dwScale, info.dwRate);
  124. st->start_time = stream->info.dwStart;
  125. }
  126. }
  127. }
  128. return 0;
  129. }
  130. static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
  131. {
  132. AviSynthContext *avs = s->priv_data;
  133. HRESULT res;
  134. AviSynthStream *stream;
  135. int stream_id = avs->next_stream;
  136. LONG read_size;
  137. // handle interleaving manually...
  138. stream = &avs->streams[stream_id];
  139. if (stream->read >= stream->info.dwLength)
  140. return AVERROR(EIO);
  141. if (av_new_packet(pkt, stream->chunck_size))
  142. return AVERROR(EIO);
  143. pkt->stream_index = stream_id;
  144. pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
  145. res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
  146. pkt->pts = stream->read;
  147. pkt->size = read_size;
  148. stream->read += stream->chunck_samples;
  149. // prepare for the next stream to read
  150. do {
  151. avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
  152. } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
  153. return (res == S_OK) ? pkt->size : -1;
  154. }
  155. static int avisynth_read_close(AVFormatContext *s)
  156. {
  157. AviSynthContext *avs = s->priv_data;
  158. int i;
  159. for (i=0;i<avs->nb_streams;i++)
  160. {
  161. AVIStreamRelease(avs->streams[i].handle);
  162. }
  163. av_free(avs->streams);
  164. AVIFileRelease(avs->file);
  165. AVIFileExit();
  166. return 0;
  167. }
  168. static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  169. {
  170. AviSynthContext *avs = s->priv_data;
  171. int stream_id;
  172. for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
  173. {
  174. avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
  175. }
  176. return 0;
  177. }
  178. AVInputFormat ff_avisynth_demuxer = {
  179. .name = "avisynth",
  180. .long_name = NULL_IF_CONFIG_SMALL("AviSynth"),
  181. .priv_data_size = sizeof(AviSynthContext),
  182. .read_header = avisynth_read_header,
  183. .read_packet = avisynth_read_packet,
  184. .read_close = avisynth_read_close,
  185. .read_seek = avisynth_read_seek,
  186. .extensions = "avs",
  187. };