avisynth.c 7.2 KB

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