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.

684 lines
20KB

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