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.

666 lines
19KB

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