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.

234 lines
7.9KB

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