avisynth.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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)
  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 can't accept UTF-8 filename */
  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\n", 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\n", 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. #if FF_API_R_FRAME_RATE
  109. st->r_frame_rate = st->avg_frame_rate;
  110. #endif
  111. st->codec->width = imgfmt.bmiHeader.biWidth;
  112. st->codec->height = imgfmt.bmiHeader.biHeight;
  113. st->codec->bits_per_coded_sample = imgfmt.bmiHeader.biBitCount;
  114. st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale;
  115. st->codec->codec_tag = imgfmt.bmiHeader.biCompression;
  116. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags, imgfmt.bmiHeader.biCompression);
  117. if (st->codec->codec_id == AV_CODEC_ID_RAWVIDEO && imgfmt.bmiHeader.biCompression== BI_RGB) {
  118. st->codec->extradata = av_malloc(9 + FF_INPUT_BUFFER_PADDING_SIZE);
  119. if (st->codec->extradata) {
  120. st->codec->extradata_size = 9;
  121. memcpy(st->codec->extradata, "BottomUp", 9);
  122. }
  123. }
  124. st->duration = stream->info.dwLength;
  125. }
  126. else
  127. {
  128. AVIStreamRelease(stream->handle);
  129. continue;
  130. }
  131. avs->nb_streams++;
  132. st->codec->stream_codec_tag = stream->info.fccHandler;
  133. avpriv_set_pts_info(st, 64, info.dwScale, info.dwRate);
  134. st->start_time = stream->info.dwStart;
  135. }
  136. }
  137. }
  138. return 0;
  139. }
  140. static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
  141. {
  142. AVISynthContext *avs = s->priv_data;
  143. HRESULT res;
  144. AVISynthStream *stream;
  145. int stream_id = avs->next_stream;
  146. LONG read_size;
  147. // handle interleaving manually...
  148. stream = &avs->streams[stream_id];
  149. if (stream->read >= stream->info.dwLength)
  150. return AVERROR(EIO);
  151. if (av_new_packet(pkt, stream->chunck_size))
  152. return AVERROR(EIO);
  153. pkt->stream_index = stream_id;
  154. pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
  155. res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
  156. pkt->size = read_size;
  157. stream->read += stream->chunck_samples;
  158. // prepare for the next stream to read
  159. do {
  160. avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
  161. } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
  162. return (res == S_OK) ? pkt->size : -1;
  163. }
  164. static int avisynth_read_close(AVFormatContext *s)
  165. {
  166. AVISynthContext *avs = s->priv_data;
  167. int i;
  168. for (i=0;i<avs->nb_streams;i++)
  169. {
  170. AVIStreamRelease(avs->streams[i].handle);
  171. }
  172. av_free(avs->streams);
  173. AVIFileRelease(avs->file);
  174. AVIFileExit();
  175. return 0;
  176. }
  177. static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  178. {
  179. AVISynthContext *avs = s->priv_data;
  180. int stream_id;
  181. for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
  182. {
  183. avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
  184. }
  185. return 0;
  186. }
  187. AVInputFormat ff_avisynth_demuxer = {
  188. .name = "avs",
  189. .long_name = NULL_IF_CONFIG_SMALL("AVISynth"),
  190. .priv_data_size = sizeof(AVISynthContext),
  191. .read_header = avisynth_read_header,
  192. .read_packet = avisynth_read_packet,
  193. .read_close = avisynth_read_close,
  194. .read_seek = avisynth_read_seek,
  195. .extensions = "avs",
  196. };