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.

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