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.

672 lines
20KB

  1. /*
  2. * Avi/AvxSynth support
  3. * Copyright (c) 2012 AvxSynth Team.
  4. *
  5. * This file is part of FFmpeg
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avformat.h"
  21. #include "internal.h"
  22. #include "libavcodec/internal.h"
  23. // Enable function pointer definitions for runtime loading.
  24. #define AVSC_NO_DECLSPEC
  25. // Shut up ffmpeg error messages.
  26. // avisynth_c.h contains inline functions that call these functions.
  27. #undef malloc
  28. #undef free
  29. #undef printf
  30. // Platform-specific directives for AviSynth vs AvxSynth.
  31. #ifdef _WIN32
  32. #include <windows.h>
  33. #undef EXTERN_C
  34. #include "compat/avisynth/avisynth_c.h"
  35. #define AVISYNTH_LIB "avisynth"
  36. #else
  37. #include <dlfcn.h>
  38. #include "compat/avisynth/avxsynth_c.h"
  39. #if defined (__APPLE__)
  40. #define AVISYNTH_LIB "libavxsynth.dylib"
  41. #else
  42. #define AVISYNTH_LIB "libavxsynth.so"
  43. #endif
  44. #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_GLOBAL)
  45. #define GetProcAddress dlsym
  46. #define FreeLibrary dlclose
  47. #endif
  48. // AvxSynth doesn't have these colorspaces, so disable them
  49. #ifndef _WIN32
  50. #define avs_is_yv24(vi) 0
  51. #define avs_is_yv16(vi) 0
  52. #define avs_is_yv411(vi) 0
  53. #define avs_is_y8(vi) 0
  54. #endif
  55. typedef struct {
  56. void *library;
  57. #define AVSC_DECLARE_FUNC(name) name##_func name
  58. AVSC_DECLARE_FUNC(avs_bit_blt);
  59. AVSC_DECLARE_FUNC(avs_clip_get_error);
  60. AVSC_DECLARE_FUNC(avs_create_script_environment);
  61. AVSC_DECLARE_FUNC(avs_delete_script_environment);
  62. AVSC_DECLARE_FUNC(avs_get_audio);
  63. AVSC_DECLARE_FUNC(avs_get_error);
  64. AVSC_DECLARE_FUNC(avs_get_frame);
  65. AVSC_DECLARE_FUNC(avs_get_version);
  66. AVSC_DECLARE_FUNC(avs_get_video_info);
  67. AVSC_DECLARE_FUNC(avs_invoke);
  68. AVSC_DECLARE_FUNC(avs_release_clip);
  69. AVSC_DECLARE_FUNC(avs_release_value);
  70. AVSC_DECLARE_FUNC(avs_release_video_frame);
  71. AVSC_DECLARE_FUNC(avs_take_clip);
  72. #undef AVSC_DECLARE_FUNC
  73. } AviSynthLibrary;
  74. struct AviSynthContext {
  75. AVS_ScriptEnvironment *env;
  76. AVS_Clip *clip;
  77. const AVS_VideoInfo *vi;
  78. // avisynth_read_packet_video() iterates over this.
  79. int n_planes;
  80. const int *planes;
  81. int curr_stream;
  82. int curr_frame;
  83. int64_t curr_sample;
  84. int error;
  85. // Linked list pointers.
  86. struct AviSynthContext *next;
  87. };
  88. typedef struct AviSynthContext AviSynthContext;
  89. static const int avs_planes_packed[1] = {0};
  90. static const int avs_planes_grey[1] = {AVS_PLANAR_Y};
  91. static const int avs_planes_yuv[3] = {AVS_PLANAR_Y, AVS_PLANAR_U, AVS_PLANAR_V};
  92. // A conflict between C++ global objects, atexit, and dynamic loading requires
  93. // us to register our own atexit handler to prevent double freeing.
  94. static AviSynthLibrary *avs_library = NULL;
  95. static int avs_atexit_called = 0;
  96. // Linked list of AviSynthContexts. An atexit handler destroys this list.
  97. static AviSynthContext *avs_ctx_list = NULL;
  98. static av_cold void avisynth_atexit_handler(void);
  99. static av_cold int avisynth_load_library(void) {
  100. avs_library = av_mallocz(sizeof(AviSynthLibrary));
  101. if (!avs_library)
  102. return AVERROR_UNKNOWN;
  103. avs_library->library = LoadLibrary(AVISYNTH_LIB);
  104. if (!avs_library->library)
  105. goto init_fail;
  106. #define LOAD_AVS_FUNC(name, continue_on_fail) \
  107. { \
  108. avs_library->name = (void*)GetProcAddress(avs_library->library, #name); \
  109. if(!continue_on_fail && !avs_library->name) \
  110. goto fail; \
  111. }
  112. LOAD_AVS_FUNC(avs_bit_blt, 0);
  113. LOAD_AVS_FUNC(avs_clip_get_error, 0);
  114. LOAD_AVS_FUNC(avs_create_script_environment, 0);
  115. LOAD_AVS_FUNC(avs_delete_script_environment, 0);
  116. LOAD_AVS_FUNC(avs_get_audio, 0);
  117. LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
  118. LOAD_AVS_FUNC(avs_get_frame, 0);
  119. LOAD_AVS_FUNC(avs_get_version, 0);
  120. LOAD_AVS_FUNC(avs_get_video_info, 0);
  121. LOAD_AVS_FUNC(avs_invoke, 0);
  122. LOAD_AVS_FUNC(avs_release_clip, 0);
  123. LOAD_AVS_FUNC(avs_release_value, 0);
  124. LOAD_AVS_FUNC(avs_release_video_frame, 0);
  125. LOAD_AVS_FUNC(avs_take_clip, 0);
  126. #undef LOAD_AVS_FUNC
  127. atexit(avisynth_atexit_handler);
  128. return 0;
  129. fail:
  130. FreeLibrary(avs_library->library);
  131. init_fail:
  132. av_freep(&avs_library);
  133. return AVERROR_UNKNOWN;
  134. }
  135. // Note that avisynth_context_create and avisynth_context_destroy
  136. // do not allocate or free the actual context! That is taken care of
  137. // by libavformat.
  138. static av_cold int avisynth_context_create(AVFormatContext *s) {
  139. AviSynthContext *avs = (AviSynthContext *)s->priv_data;
  140. int ret;
  141. if (!avs_library) {
  142. if (ret = avisynth_load_library())
  143. return ret;
  144. }
  145. avs->env = avs_library->avs_create_script_environment(3);
  146. if (avs_library->avs_get_error) {
  147. const char *error = avs_library->avs_get_error(avs->env);
  148. if (error) {
  149. av_log(s, AV_LOG_ERROR, "%s\n", error);
  150. return AVERROR_UNKNOWN;
  151. }
  152. }
  153. if (!avs_ctx_list) {
  154. avs_ctx_list = avs;
  155. } else {
  156. avs->next = avs_ctx_list;
  157. avs_ctx_list = avs;
  158. }
  159. return 0;
  160. }
  161. static av_cold void avisynth_context_destroy(AviSynthContext *avs) {
  162. if (avs_atexit_called)
  163. return;
  164. if (avs == avs_ctx_list) {
  165. avs_ctx_list = avs->next;
  166. } else {
  167. AviSynthContext *prev = avs_ctx_list;
  168. while (prev->next != avs)
  169. prev = prev->next;
  170. prev->next = avs->next;
  171. }
  172. if (avs->clip) {
  173. avs_library->avs_release_clip(avs->clip);
  174. avs->clip = NULL;
  175. }
  176. if (avs->env) {
  177. avs_library->avs_delete_script_environment(avs->env);
  178. avs->env = NULL;
  179. }
  180. }
  181. static av_cold void avisynth_atexit_handler(void) {
  182. AviSynthContext *avs = avs_ctx_list;
  183. while (avs) {
  184. AviSynthContext *next = avs->next;
  185. avisynth_context_destroy(avs);
  186. avs = next;
  187. }
  188. FreeLibrary(avs_library->library);
  189. av_freep(&avs_library);
  190. avs_atexit_called = 1;
  191. }
  192. // Create AVStream from audio and video data.
  193. static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st) {
  194. AviSynthContext *avs = s->priv_data;
  195. int planar = 0; // 0: packed, 1: YUV, 2: Y8
  196. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  197. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  198. st->codec->width = avs->vi->width;
  199. st->codec->height = avs->vi->height;
  200. st->time_base = (AVRational) {avs->vi->fps_denominator, avs->vi->fps_numerator};
  201. st->avg_frame_rate = (AVRational) {avs->vi->fps_numerator, avs->vi->fps_denominator};
  202. st->start_time = 0;
  203. st->duration = avs->vi->num_frames;
  204. st->nb_frames = avs->vi->num_frames;
  205. switch (avs->vi->pixel_type) {
  206. #ifdef _WIN32
  207. case AVS_CS_YV24:
  208. st->codec->pix_fmt = AV_PIX_FMT_YUV444P;
  209. planar = 1;
  210. break;
  211. case AVS_CS_YV16:
  212. st->codec->pix_fmt = AV_PIX_FMT_YUV422P;
  213. planar = 1;
  214. break;
  215. case AVS_CS_YV411:
  216. st->codec->pix_fmt = AV_PIX_FMT_YUV411P;
  217. planar = 1;
  218. break;
  219. case AVS_CS_Y8:
  220. st->codec->pix_fmt = AV_PIX_FMT_GRAY8;
  221. planar = 2;
  222. break;
  223. #endif
  224. case AVS_CS_BGR24:
  225. st->codec->pix_fmt = AV_PIX_FMT_BGR24;
  226. break;
  227. case AVS_CS_BGR32:
  228. st->codec->pix_fmt = AV_PIX_FMT_RGB32;
  229. break;
  230. case AVS_CS_YUY2:
  231. st->codec->pix_fmt = AV_PIX_FMT_YUYV422;
  232. break;
  233. case AVS_CS_YV12:
  234. st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
  235. planar = 1;
  236. break;
  237. case AVS_CS_I420: // Is this even used anywhere?
  238. st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
  239. planar = 1;
  240. break;
  241. default:
  242. av_log(s, AV_LOG_ERROR, "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
  243. avs->error = 1;
  244. return AVERROR_UNKNOWN;
  245. }
  246. switch (planar) {
  247. case 2: // Y8
  248. avs->n_planes = 1;
  249. avs->planes = avs_planes_grey;
  250. break;
  251. case 1: // YUV
  252. avs->n_planes = 3;
  253. avs->planes = avs_planes_yuv;
  254. break;
  255. default:
  256. avs->n_planes = 1;
  257. avs->planes = avs_planes_packed;
  258. }
  259. return 0;
  260. }
  261. static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st) {
  262. AviSynthContext *avs = s->priv_data;
  263. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  264. st->codec->sample_rate = avs->vi->audio_samples_per_second;
  265. st->codec->channels = avs->vi->nchannels;
  266. st->time_base = (AVRational) {1, avs->vi->audio_samples_per_second};
  267. switch (avs->vi->sample_type) {
  268. case AVS_SAMPLE_INT8:
  269. st->codec->codec_id = CODEC_ID_PCM_U8;
  270. break;
  271. case AVS_SAMPLE_INT16:
  272. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  273. break;
  274. case AVS_SAMPLE_INT24:
  275. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  276. break;
  277. case AVS_SAMPLE_INT32:
  278. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  279. break;
  280. case AVS_SAMPLE_FLOAT:
  281. st->codec->codec_id = CODEC_ID_PCM_F32LE;
  282. break;
  283. default:
  284. av_log(s, AV_LOG_ERROR, "unknown AviSynth sample type %d\n", avs->vi->sample_type);
  285. avs->error = 1;
  286. return AVERROR_UNKNOWN;
  287. }
  288. return 0;
  289. }
  290. static int avisynth_create_stream(AVFormatContext *s) {
  291. AviSynthContext *avs = s->priv_data;
  292. AVStream *st;
  293. int ret;
  294. int id = 0;
  295. if (avs_has_video(avs->vi)) {
  296. st = avformat_new_stream(s, NULL);
  297. if (!st)
  298. return AVERROR_UNKNOWN;
  299. st->id = id++;
  300. if (ret = avisynth_create_stream_video(s, st))
  301. return ret;
  302. }
  303. if (avs_has_audio(avs->vi)) {
  304. st = avformat_new_stream(s, NULL);
  305. if (!st)
  306. return AVERROR_UNKNOWN;
  307. st->id = id++;
  308. if (ret = avisynth_create_stream_audio(s, st))
  309. return ret;
  310. }
  311. return 0;
  312. }
  313. static int avisynth_open_file(AVFormatContext *s) {
  314. AviSynthContext *avs = (AviSynthContext *)s->priv_data;
  315. AVS_Value arg, val;
  316. int ret;
  317. #ifdef _WIN32
  318. char filename_ansi[MAX_PATH * 4];
  319. wchar_t filename_wc[MAX_PATH * 4];
  320. #endif
  321. if (ret = avisynth_context_create(s))
  322. return ret;
  323. #ifdef _WIN32
  324. // Convert UTF-8 to ANSI code page
  325. MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
  326. WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi, MAX_PATH * 4, NULL, NULL);
  327. arg = avs_new_value_string(filename_ansi);
  328. #else
  329. arg = avs_new_value_string(s->filename);
  330. #endif
  331. val = avs_library->avs_invoke(avs->env, "Import", arg, 0);
  332. if (avs_is_error(val)) {
  333. av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
  334. ret = AVERROR_UNKNOWN;
  335. goto fail;
  336. }
  337. if (!avs_is_clip(val)) {
  338. av_log(s, AV_LOG_ERROR, "%s\n", "AviSynth script did not return a clip");
  339. ret = AVERROR_UNKNOWN;
  340. goto fail;
  341. }
  342. avs->clip = avs_library->avs_take_clip(val, avs->env);
  343. avs->vi = avs_library->avs_get_video_info(avs->clip);
  344. // Release the AVS_Value as it will go out of scope.
  345. avs_library->avs_release_value(val);
  346. if (ret = avisynth_create_stream(s))
  347. goto fail;
  348. return 0;
  349. fail:
  350. avisynth_context_destroy(avs);
  351. return ret;
  352. }
  353. static void avisynth_next_stream(AVFormatContext *s, AVStream **st, AVPacket *pkt, int *discard) {
  354. AviSynthContext *avs = s->priv_data;
  355. pkt->stream_index = avs->curr_stream++;
  356. avs->curr_stream %= s->nb_streams;
  357. *st = s->streams[pkt->stream_index];
  358. if ((*st)->discard == AVDISCARD_ALL)
  359. *discard = 1;
  360. else
  361. *discard = 0;
  362. return;
  363. }
  364. // Copy AviSynth clip data into an AVPacket.
  365. static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt, int discard) {
  366. AviSynthContext *avs = s->priv_data;
  367. AVS_VideoFrame *frame;
  368. unsigned char *dst_p;
  369. const unsigned char *src_p;
  370. int n, i, plane, rowsize, planeheight, pitch, bits;
  371. const char *error;
  372. if (avs->curr_frame >= avs->vi->num_frames)
  373. return AVERROR_EOF;
  374. // This must happen even if the stream is discarded to prevent desync.
  375. n = avs->curr_frame++;
  376. if (discard)
  377. return 0;
  378. pkt->pts = n;
  379. pkt->dts = n;
  380. pkt->duration = 1;
  381. // Define the bpp values for the new AviSynth 2.6 colorspaces
  382. if (avs_is_yv24(avs->vi)) {
  383. bits = 24;
  384. } else if (avs_is_yv16(avs->vi)) {
  385. bits = 16;
  386. } else if (avs_is_yv411(avs->vi)) {
  387. bits = 12;
  388. } else if (avs_is_y8(avs->vi)) {
  389. bits = 8;
  390. } else {
  391. bits = avs_bits_per_pixel(avs->vi);
  392. }
  393. // Without cast to int64_t, calculation overflows at about 9k x 9k resolution.
  394. pkt->size = (((int64_t)avs->vi->width * (int64_t)avs->vi->height) * bits) / 8;
  395. if (!pkt->size)
  396. return AVERROR_UNKNOWN;
  397. pkt->data = av_malloc(pkt->size);
  398. if (!pkt->data)
  399. return AVERROR_UNKNOWN;
  400. frame = avs_library->avs_get_frame(avs->clip, n);
  401. error = avs_library->avs_clip_get_error(avs->clip);
  402. if (error) {
  403. av_log(s, AV_LOG_ERROR, "%s\n", error);
  404. avs->error = 1;
  405. av_freep(&pkt->data);
  406. return AVERROR_UNKNOWN;
  407. }
  408. dst_p = pkt->data;
  409. for (i = 0; i < avs->n_planes; i++) {
  410. plane = avs->planes[i];
  411. src_p = avs_get_read_ptr_p(frame, plane);
  412. rowsize = avs_get_row_size_p(frame, plane);
  413. planeheight = avs_get_height_p(frame, plane);
  414. pitch = avs_get_pitch_p(frame, plane);
  415. // Flip RGB video.
  416. if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
  417. src_p = src_p + (planeheight - 1) * pitch;
  418. pitch = -pitch;
  419. }
  420. // An issue with avs_bit_blt on 2.5.8 prevents video from working correctly.
  421. // This problem doesn't exist for 2.6 and AvxSynth, so enable the workaround
  422. // for 2.5.8 only. This only displays the warning and exits if the script has
  423. // video. 2.5.8's internal interface version is 3, so avs_get_version allows
  424. // it to work only in the circumstance that the interface is 5 or higher (4 is
  425. // unused). There's a strong chance that AvxSynth, having been based on 2.5.8,
  426. // would also be identified as interface version 3, but since AvxSynth doesn't
  427. // suffer from this problem, special-case it.
  428. #ifdef _WIN32
  429. if (avs_library->avs_get_version(avs->clip) > 3) {
  430. avs_library->avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch, rowsize, planeheight);
  431. } else {
  432. av_log(s, AV_LOG_ERROR, "Video input from AviSynth 2.5.8 is not supported. Please upgrade to 2.6.\n");
  433. avs->error = 1;
  434. av_freep(&pkt->data);
  435. return AVERROR_UNKNOWN;
  436. }
  437. #else
  438. avs_library->avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch, rowsize, planeheight);
  439. #endif
  440. dst_p += rowsize * planeheight;
  441. }
  442. avs_library->avs_release_video_frame(frame);
  443. return 0;
  444. }
  445. static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt, int discard) {
  446. AviSynthContext *avs = s->priv_data;
  447. AVRational fps, samplerate;
  448. int samples;
  449. int64_t n;
  450. const char *error;
  451. if (avs->curr_sample >= avs->vi->num_audio_samples)
  452. return AVERROR_EOF;
  453. fps.num = avs->vi->fps_numerator;
  454. fps.den = avs->vi->fps_denominator;
  455. samplerate.num = avs->vi->audio_samples_per_second;
  456. samplerate.den = 1;
  457. if (avs_has_video(avs->vi)) {
  458. if (avs->curr_frame < avs->vi->num_frames)
  459. samples = av_rescale_q(avs->curr_frame, samplerate, fps) - avs->curr_sample;
  460. else
  461. samples = av_rescale_q(1, samplerate, fps);
  462. } else {
  463. samples = 1000;
  464. }
  465. // After seeking, audio may catch up with video.
  466. if (samples <= 0) {
  467. pkt->size = 0;
  468. pkt->data = NULL;
  469. return 0;
  470. }
  471. if (avs->curr_sample + samples > avs->vi->num_audio_samples)
  472. samples = avs->vi->num_audio_samples - avs->curr_sample;
  473. // This must happen even if the stream is discarded to prevent desync.
  474. n = avs->curr_sample;
  475. avs->curr_sample += samples;
  476. if (discard)
  477. return 0;
  478. pkt->pts = n;
  479. pkt->dts = n;
  480. pkt->duration = samples;
  481. pkt->size = avs_bytes_per_channel_sample(avs->vi) * samples * avs->vi->nchannels;
  482. if (!pkt->size)
  483. return AVERROR_UNKNOWN;
  484. pkt->data = av_malloc(pkt->size);
  485. if (!pkt->data)
  486. return AVERROR_UNKNOWN;
  487. avs_library->avs_get_audio(avs->clip, pkt->data, n, samples);
  488. error = avs_library->avs_clip_get_error(avs->clip);
  489. if (error) {
  490. av_log(s, AV_LOG_ERROR, "%s\n", error);
  491. avs->error = 1;
  492. av_freep(&pkt->data);
  493. return AVERROR_UNKNOWN;
  494. }
  495. return 0;
  496. }
  497. static av_cold int avisynth_read_header(AVFormatContext *s) {
  498. int ret;
  499. // Calling library must implement a lock for thread-safe opens.
  500. if (ret = avpriv_lock_avformat())
  501. return ret;
  502. if (ret = avisynth_open_file(s)) {
  503. avpriv_unlock_avformat();
  504. return ret;
  505. }
  506. avpriv_unlock_avformat();
  507. return 0;
  508. }
  509. static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt) {
  510. AviSynthContext *avs = s->priv_data;
  511. AVStream *st;
  512. int discard = 0;
  513. int ret;
  514. if (avs->error)
  515. return AVERROR_UNKNOWN;
  516. pkt->destruct = av_destruct_packet;
  517. // If either stream reaches EOF, try to read the other one before giving up.
  518. avisynth_next_stream(s, &st, pkt, &discard);
  519. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  520. ret = avisynth_read_packet_video(s, pkt, discard);
  521. if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
  522. avisynth_next_stream(s, &st, pkt, &discard);
  523. return avisynth_read_packet_audio(s, pkt, discard);
  524. }
  525. return ret;
  526. } else {
  527. ret = avisynth_read_packet_audio(s, pkt, discard);
  528. if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
  529. avisynth_next_stream(s, &st, pkt, &discard);
  530. return avisynth_read_packet_video(s, pkt, discard);
  531. }
  532. return ret;
  533. }
  534. }
  535. static av_cold int avisynth_read_close(AVFormatContext *s) {
  536. if (avpriv_lock_avformat())
  537. return AVERROR_UNKNOWN;
  538. avisynth_context_destroy(s->priv_data);
  539. avpriv_unlock_avformat();
  540. return 0;
  541. }
  542. static int avisynth_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
  543. AviSynthContext *avs = s->priv_data;
  544. AVStream *st;
  545. AVRational fps, samplerate;
  546. if (avs->error)
  547. return AVERROR_UNKNOWN;
  548. fps = (AVRational) {avs->vi->fps_numerator, avs->vi->fps_denominator};
  549. samplerate = (AVRational) {avs->vi->audio_samples_per_second, 1};
  550. st = s->streams[stream_index];
  551. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  552. // AviSynth frame counts are signed int.
  553. if ((timestamp >= avs->vi->num_frames) || (timestamp > INT_MAX) || (timestamp < 0))
  554. return AVERROR_EOF;
  555. avs->curr_frame = timestamp;
  556. if (avs_has_audio(avs->vi))
  557. avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
  558. } else {
  559. if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
  560. return AVERROR_EOF;
  561. // Force frame granularity for seeking.
  562. if (avs_has_video(avs->vi)) {
  563. avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
  564. avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
  565. } else {
  566. avs->curr_sample = timestamp;
  567. }
  568. }
  569. return 0;
  570. }
  571. AVInputFormat ff_avisynth_demuxer = {
  572. .name = "avisynth",
  573. .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
  574. .priv_data_size = sizeof(AviSynthContext),
  575. .read_header = avisynth_read_header,
  576. .read_packet = avisynth_read_packet,
  577. .read_close = avisynth_read_close,
  578. .read_seek = avisynth_read_seek,
  579. .extensions = "avs",
  580. };