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.

221 lines
6.6KB

  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_tag = wvfmt.wFormatTag;
  84. st->codec->codec_id = wav_codec_get_id(wvfmt.wFormatTag, st->codec->bits_per_sample);
  85. }
  86. else if (stream->info.fccType == streamtypeVIDEO)
  87. {
  88. BITMAPINFO imgfmt;
  89. LONG struct_size = sizeof(BITMAPINFO);
  90. stream->chunck_size = stream->info.dwSampleSize;
  91. stream->chunck_samples = 1;
  92. if (AVIStreamReadFormat(stream->handle, 0, &imgfmt, &struct_size) != S_OK)
  93. continue;
  94. st = av_new_stream(s, id);
  95. st->codec->codec_type = CODEC_TYPE_VIDEO;
  96. st->r_frame_rate.num = stream->info.dwRate;
  97. st->r_frame_rate.den = stream->info.dwScale;
  98. st->codec->width = imgfmt.bmiHeader.biWidth;
  99. st->codec->height = imgfmt.bmiHeader.biHeight;
  100. st->codec->bits_per_sample = imgfmt.bmiHeader.biBitCount;
  101. st->codec->bit_rate = (uint64_t)stream->info.dwSampleSize * (uint64_t)stream->info.dwRate * 8 / (uint64_t)stream->info.dwScale;
  102. st->codec->codec_tag = imgfmt.bmiHeader.biCompression;
  103. st->codec->codec_id = codec_get_id(codec_bmp_tags, imgfmt.bmiHeader.biCompression);
  104. st->duration = stream->info.dwLength;
  105. }
  106. else
  107. {
  108. AVIStreamRelease(stream->handle);
  109. continue;
  110. }
  111. avs->nb_streams++;
  112. st->codec->stream_codec_tag = stream->info.fccHandler;
  113. av_set_pts_info(st, 64, info.dwScale, info.dwRate);
  114. st->start_time = stream->info.dwStart;
  115. }
  116. }
  117. }
  118. return 0;
  119. }
  120. static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
  121. {
  122. AVISynthContext *avs = s->priv_data;
  123. HRESULT res;
  124. AVISynthStream *stream;
  125. int stream_id = avs->next_stream;
  126. LONG read_size;
  127. // handle interleaving manually...
  128. stream = &avs->streams[stream_id];
  129. if (stream->read >= stream->info.dwLength)
  130. return AVERROR_IO;
  131. if (av_new_packet(pkt, stream->chunck_size))
  132. return AVERROR_IO;
  133. pkt->stream_index = stream_id;
  134. pkt->pts = avs->streams[stream_id].read / avs->streams[stream_id].chunck_samples;
  135. res = AVIStreamRead(stream->handle, stream->read, stream->chunck_samples, pkt->data, stream->chunck_size, &read_size, NULL);
  136. pkt->pts = stream->read;
  137. pkt->size = read_size;
  138. stream->read += stream->chunck_samples;
  139. // prepare for the next stream to read
  140. do {
  141. avs->next_stream = (avs->next_stream+1) % avs->nb_streams;
  142. } while (avs->next_stream != stream_id && s->streams[avs->next_stream]->discard >= AVDISCARD_ALL);
  143. return (res == S_OK) ? pkt->size : -1;
  144. }
  145. static int avisynth_read_close(AVFormatContext *s)
  146. {
  147. AVISynthContext *avs = s->priv_data;
  148. int i;
  149. for (i=0;i<avs->nb_streams;i++)
  150. {
  151. AVIStreamRelease(avs->streams[i].handle);
  152. }
  153. av_free(avs->streams);
  154. AVIFileRelease(avs->file);
  155. AVIFileExit();
  156. return 0;
  157. }
  158. static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t pts, int flags)
  159. {
  160. AVISynthContext *avs = s->priv_data;
  161. int stream_id;
  162. for (stream_id = 0; stream_id < avs->nb_streams; stream_id++)
  163. {
  164. avs->streams[stream_id].read = pts * avs->streams[stream_id].chunck_samples;
  165. }
  166. return 0;
  167. }
  168. AVInputFormat avisynth_demuxer = {
  169. "avs",
  170. "AVISynth",
  171. sizeof(AVISynthContext),
  172. NULL,
  173. avisynth_read_header,
  174. avisynth_read_packet,
  175. avisynth_read_close,
  176. avisynth_read_seek,
  177. NULL,
  178. 0,
  179. "avs",
  180. };