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.

220 lines
6.5KB

  1. /*
  2. * AVISynth support for ffmpeg system
  3. * Copyright (c) 2006 DivX, Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avformat.h"
  20. #include "riff.h"
  21. #include <windows.h>
  22. #include <vfw.h>
  23. typedef struct {
  24. PAVISTREAM handle;
  25. AVISTREAMINFO info;
  26. DWORD read;
  27. LONG chunck_size;
  28. LONG chunck_samples;
  29. } AVISynthStream;
  30. typedef struct {
  31. PAVIFILE file;
  32. AVISynthStream *streams;
  33. int nb_streams;
  34. int next_stream;
  35. } AVISynthContext;
  36. static int avisynth_read_header(AVFormatContext *s, AVFormatParameters *ap)
  37. {
  38. AVISynthContext *avs = s->priv_data;
  39. HRESULT res;
  40. AVIFILEINFO info;
  41. DWORD id;
  42. AVStream *st;
  43. AVISynthStream *stream;
  44. AVIFileInit();
  45. res = AVIFileOpen(&avs->file, s->filename, OF_READ|OF_SHARE_DENY_WRITE, NULL);
  46. if (res != S_OK)
  47. {
  48. av_log(s, AV_LOG_ERROR, "AVIFileOpen failed with error %ld", res);
  49. AVIFileExit();
  50. return -1;
  51. }
  52. res = AVIFileInfo(avs->file, &info, sizeof(info));
  53. if (res != S_OK)
  54. {
  55. av_log(s, AV_LOG_ERROR, "AVIFileInfo failed with error %ld", res);
  56. AVIFileExit();
  57. return -1;
  58. }
  59. avs->streams = av_mallocz(info.dwStreams * sizeof(AVISynthStream));
  60. for (id=0; id<info.dwStreams; id++)
  61. {
  62. stream = &avs->streams[id];
  63. stream->read = 0;
  64. if (AVIFileGetStream(avs->file, &stream->handle, 0, id) == S_OK)
  65. {
  66. if (AVIStreamInfo(stream->handle, &stream->info, sizeof(stream->info)) == S_OK)
  67. {
  68. if (stream->info.fccType == streamtypeAUDIO)
  69. {
  70. WAVEFORMATEX wvfmt;
  71. LONG struct_size = sizeof(WAVEFORMATEX);
  72. if (AVIStreamReadFormat(stream->handle, 0, &wvfmt, &struct_size) != S_OK)
  73. continue;
  74. st = av_new_stream(s, id);
  75. st->codec->codec_type = CODEC_TYPE_AUDIO;
  76. st->codec->block_align = wvfmt.nBlockAlign;
  77. st->codec->channels = wvfmt.nChannels;
  78. st->codec->sample_rate = wvfmt.nSamplesPerSec;
  79. st->codec->bit_rate = wvfmt.nAvgBytesPerSec * 8;
  80. st->codec->bits_per_sample = wvfmt.wBitsPerSample;
  81. stream->chunck_samples = wvfmt.nSamplesPerSec * (uint64_t)info.dwScale / (uint64_t)info.dwRate;
  82. stream->chunck_size = stream->chunck_samples * wvfmt.nChannels * wvfmt.wBitsPerSample / 8;
  83. st->codec->codec_id = wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_sample);
  84. }
  85. else if (stream->info.fccType == streamtypeVIDEO)
  86. {
  87. BITMAPINFO imgfmt;
  88. LONG struct_size = sizeof(BITMAPINFO);
  89. stream->chunck_size = stream->info.dwSampleSize;
  90. stream->chunck_samples = 1;
  91. if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK)
  92. continue;
  93. st = av_new_stream(s, id);
  94. st->codec->codec_type = CODEC_TYPE_VIDEO;
  95. st->r_frame_rate.num = stream->info.dwRate;
  96. st->r_frame_rate.den = stream->info.dwScale;
  97. st->codec->width = imgfmt.bmiHeader.biWidth;
  98. st->codec->height = imgfmt.bmiHeader.biHeight;
  99. st->codec->bits_per_sample = stream->info.dwSampleSize * 8;
  100. st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale;
  101. st->codec->codec_id = codec_get_id(codec_bmp_tags, stream->info.fccHandler);
  102. st->duration = stream->info.dwLength;
  103. }
  104. else
  105. {
  106. AVIStreamRelease(stream->handle);
  107. continue;
  108. }
  109. avs->nb_streams++;
  110. st->codec->codec_tag = stream->info.fccHandler;
  111. st->codec->stream_codec_tag = stream->info.fccHandler;
  112. av_set_pts_info(st, 64, info.dwScale, info.dwRate);
  113. st->start_time = stream->info.dwStart;
  114. }
  115. }
  116. }
  117. return 0;
  118. }
  119. static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
  120. {
  121. AVISynthContext *avs = s->priv_data;
  122. HRESULT res;
  123. AVISynthStream *stream;
  124. int stream_id = avs->next_stream;
  125. LONG read_size;
  126. // handle interleaving manually...
  127. stream = &avs->streams[stream_id];
  128. if (stream->read >= stream->info.dwLength)
  129. return AVERROR_IO;
  130. if (av_new_packet(pkt, stream->chunck_size))
  131. return AVERROR_IO;
  132. pkt->stream_index = stream_id;
  133. pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
  134. res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
  135. pkt->pts = stream->read;
  136. pkt->size = read_size;
  137. stream->read += stream->chunck_samples;
  138. // prepare for the next stream to read
  139. do {
  140. avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
  141. } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
  142. return (res == S_OK) ? pkt->size : -1;
  143. }
  144. static int avisynth_read_close(AVFormatContext *s)
  145. {
  146. AVISynthContext *avs = s->priv_data;
  147. int i;
  148. for (i=0;i<avs->nb_streams;i++)
  149. {
  150. AVIStreamRelease(avs->streams[i].handle);
  151. }
  152. av_free(avs->streams);
  153. AVIFileRelease(avs->file);
  154. AVIFileExit();
  155. return 0;
  156. }
  157. static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  158. {
  159. AVISynthContext *avs = s->priv_data;
  160. int stream_id;
  161. for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
  162. {
  163. avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
  164. }
  165. return 0;
  166. }
  167. AVInputFormat avisynth_demuxer = {
  168. "avs",
  169. "AVISynth",
  170. sizeof(AVISynthContext),
  171. NULL,
  172. avisynth_read_header,
  173. avisynth_read_packet,
  174. avisynth_read_close,
  175. avisynth_read_seek,
  176. NULL,
  177. 0,
  178. "avs",
  179. };