avisynth.c 6.7 KB

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