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.

683 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. switch (avs->vi->sample_type) {
  261. case AVS_SAMPLE_INT8:
  262. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  263. break;
  264. case AVS_SAMPLE_INT16:
  265. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  266. break;
  267. case AVS_SAMPLE_INT24:
  268. st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
  269. break;
  270. case AVS_SAMPLE_INT32:
  271. st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
  272. break;
  273. case AVS_SAMPLE_FLOAT:
  274. st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
  275. break;
  276. default:
  277. av_log(s, AV_LOG_ERROR,
  278. "unknown AviSynth sample type %d\n", avs->vi->sample_type);
  279. avs->error = 1;
  280. return AVERROR_UNKNOWN;
  281. }
  282. return 0;
  283. }
  284. static int avisynth_create_stream(AVFormatContext *s)
  285. {
  286. AviSynthContext *avs = s->priv_data;
  287. AVStream *st;
  288. int ret;
  289. int id = 0;
  290. if (avs_has_video(avs->vi)) {
  291. st = avformat_new_stream(s, NULL);
  292. if (!st)
  293. return AVERROR_UNKNOWN;
  294. st->id = id++;
  295. if (ret = avisynth_create_stream_video(s, st))
  296. return ret;
  297. }
  298. if (avs_has_audio(avs->vi)) {
  299. st = avformat_new_stream(s, NULL);
  300. if (!st)
  301. return AVERROR_UNKNOWN;
  302. st->id = id++;
  303. if (ret = avisynth_create_stream_audio(s, st))
  304. return ret;
  305. }
  306. return 0;
  307. }
  308. static int avisynth_open_file(AVFormatContext *s)
  309. {
  310. AviSynthContext *avs = s->priv_data;
  311. AVS_Value arg, val;
  312. int ret;
  313. #ifdef USING_AVISYNTH
  314. char filename_ansi[MAX_PATH * 4];
  315. wchar_t filename_wc[MAX_PATH * 4];
  316. #endif
  317. if (ret = avisynth_context_create(s))
  318. return ret;
  319. #ifdef USING_AVISYNTH
  320. /* Convert UTF-8 to ANSI code page */
  321. MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
  322. WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
  323. MAX_PATH * 4, NULL, NULL);
  324. arg = avs_new_value_string(filename_ansi);
  325. #else
  326. arg = avs_new_value_string(s->filename);
  327. #endif
  328. val = avs_library.avs_invoke(avs->env, "Import", arg, 0);
  329. if (avs_is_error(val)) {
  330. av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
  331. ret = AVERROR_UNKNOWN;
  332. goto fail;
  333. }
  334. if (!avs_is_clip(val)) {
  335. av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
  336. ret = AVERROR_UNKNOWN;
  337. goto fail;
  338. }
  339. avs->clip = avs_library.avs_take_clip(val, avs->env);
  340. avs->vi = avs_library.avs_get_video_info(avs->clip);
  341. /* Release the AVS_Value as it will go out of scope. */
  342. avs_library.avs_release_value(val);
  343. if (ret = avisynth_create_stream(s))
  344. goto fail;
  345. return 0;
  346. fail:
  347. avisynth_context_destroy(avs);
  348. return ret;
  349. }
  350. static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
  351. AVPacket *pkt, int *discard)
  352. {
  353. AviSynthContext *avs = s->priv_data;
  354. avs->curr_stream++;
  355. avs->curr_stream %= s->nb_streams;
  356. *st = s->streams[avs->curr_stream];
  357. if ((*st)->discard == AVDISCARD_ALL)
  358. *discard = 1;
  359. else
  360. *discard = 0;
  361. return;
  362. }
  363. /* Copy AviSynth clip data into an AVPacket. */
  364. static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
  365. int discard)
  366. {
  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. #ifdef USING_AVISYNTH
  380. /* Define the bpp values for the new AviSynth 2.6 colorspaces.
  381. * Since AvxSynth doesn't have these functions, special-case
  382. * it in order to avoid implicit declaration errors. */
  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. #endif
  393. bits = avs_bits_per_pixel(avs->vi);
  394. /* Without the cast to int64_t, calculation overflows at about 9k x 9k
  395. * resolution. */
  396. pkt->size = (((int64_t)avs->vi->width *
  397. (int64_t)avs->vi->height) * bits) / 8;
  398. if (!pkt->size)
  399. return AVERROR_UNKNOWN;
  400. if (av_new_packet(pkt, pkt->size) < 0)
  401. return AVERROR(ENOMEM);
  402. pkt->pts = n;
  403. pkt->dts = n;
  404. pkt->duration = 1;
  405. pkt->stream_index = avs->curr_stream;
  406. frame = avs_library.avs_get_frame(avs->clip, n);
  407. error = avs_library.avs_clip_get_error(avs->clip);
  408. if (error) {
  409. av_log(s, AV_LOG_ERROR, "%s\n", error);
  410. avs->error = 1;
  411. av_packet_unref(pkt);
  412. return AVERROR_UNKNOWN;
  413. }
  414. dst_p = pkt->data;
  415. for (i = 0; i < avs->n_planes; i++) {
  416. plane = avs->planes[i];
  417. src_p = avs_get_read_ptr_p(frame, plane);
  418. pitch = avs_get_pitch_p(frame, plane);
  419. #ifdef USING_AVISYNTH
  420. if (avs_library.avs_get_version(avs->clip) == 3) {
  421. rowsize = avs_get_row_size_p_25(frame, plane);
  422. planeheight = avs_get_height_p_25(frame, plane);
  423. } else {
  424. rowsize = avs_get_row_size_p(frame, plane);
  425. planeheight = avs_get_height_p(frame, plane);
  426. }
  427. #else
  428. rowsize = avs_get_row_size_p(frame, plane);
  429. planeheight = avs_get_height_p(frame, plane);
  430. #endif
  431. /* Flip RGB video. */
  432. if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
  433. src_p = src_p + (planeheight - 1) * pitch;
  434. pitch = -pitch;
  435. }
  436. avs_library.avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
  437. rowsize, planeheight);
  438. dst_p += rowsize * planeheight;
  439. }
  440. avs_library.avs_release_video_frame(frame);
  441. return 0;
  442. }
  443. static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
  444. int discard)
  445. {
  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) -
  460. avs->curr_sample;
  461. else
  462. samples = av_rescale_q(1, samplerate, fps);
  463. } else {
  464. samples = 1000;
  465. }
  466. /* After seeking, audio may catch up with video. */
  467. if (samples <= 0) {
  468. pkt->size = 0;
  469. pkt->data = NULL;
  470. return 0;
  471. }
  472. if (avs->curr_sample + samples > avs->vi->num_audio_samples)
  473. samples = avs->vi->num_audio_samples - avs->curr_sample;
  474. /* This must happen even if the stream is discarded to prevent desync. */
  475. n = avs->curr_sample;
  476. avs->curr_sample += samples;
  477. if (discard)
  478. return 0;
  479. pkt->size = avs_bytes_per_channel_sample(avs->vi) *
  480. samples * avs->vi->nchannels;
  481. if (!pkt->size)
  482. return AVERROR_UNKNOWN;
  483. if (av_new_packet(pkt, pkt->size) < 0)
  484. return AVERROR(ENOMEM);
  485. pkt->pts = n;
  486. pkt->dts = n;
  487. pkt->duration = samples;
  488. pkt->stream_index = avs->curr_stream;
  489. avs_library.avs_get_audio(avs->clip, pkt->data, n, samples);
  490. error = avs_library.avs_clip_get_error(avs->clip);
  491. if (error) {
  492. av_log(s, AV_LOG_ERROR, "%s\n", error);
  493. avs->error = 1;
  494. av_packet_unref(pkt);
  495. return AVERROR_UNKNOWN;
  496. }
  497. return 0;
  498. }
  499. static av_cold int avisynth_read_header(AVFormatContext *s)
  500. {
  501. int ret;
  502. // Calling library must implement a lock for thread-safe opens.
  503. if (ret = avpriv_lock_avformat())
  504. return ret;
  505. if (ret = avisynth_open_file(s)) {
  506. avpriv_unlock_avformat();
  507. return ret;
  508. }
  509. avpriv_unlock_avformat();
  510. return 0;
  511. }
  512. static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
  513. {
  514. AviSynthContext *avs = s->priv_data;
  515. AVStream *st;
  516. int discard = 0;
  517. int ret;
  518. if (avs->error)
  519. return AVERROR_UNKNOWN;
  520. /* If either stream reaches EOF, try to read the other one before
  521. * giving up. */
  522. avisynth_next_stream(s, &st, pkt, &discard);
  523. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  524. ret = avisynth_read_packet_video(s, pkt, discard);
  525. if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
  526. avisynth_next_stream(s, &st, pkt, &discard);
  527. return avisynth_read_packet_audio(s, pkt, discard);
  528. }
  529. } else {
  530. ret = avisynth_read_packet_audio(s, pkt, discard);
  531. if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
  532. avisynth_next_stream(s, &st, pkt, &discard);
  533. return avisynth_read_packet_video(s, pkt, discard);
  534. }
  535. }
  536. return ret;
  537. }
  538. static av_cold int avisynth_read_close(AVFormatContext *s)
  539. {
  540. if (avpriv_lock_avformat())
  541. return AVERROR_UNKNOWN;
  542. avisynth_context_destroy(s->priv_data);
  543. avpriv_unlock_avformat();
  544. return 0;
  545. }
  546. static int avisynth_read_seek(AVFormatContext *s, int stream_index,
  547. int64_t timestamp, int flags)
  548. {
  549. AviSynthContext *avs = s->priv_data;
  550. AVStream *st;
  551. AVRational fps, samplerate;
  552. if (avs->error)
  553. return AVERROR_UNKNOWN;
  554. fps = (AVRational) { avs->vi->fps_numerator,
  555. avs->vi->fps_denominator };
  556. samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
  557. st = s->streams[stream_index];
  558. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  559. /* AviSynth frame counts are signed int. */
  560. if ((timestamp >= avs->vi->num_frames) ||
  561. (timestamp > INT_MAX) ||
  562. (timestamp < 0))
  563. return AVERROR_EOF;
  564. avs->curr_frame = timestamp;
  565. if (avs_has_audio(avs->vi))
  566. avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
  567. } else {
  568. if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
  569. return AVERROR_EOF;
  570. /* Force frame granularity for seeking. */
  571. if (avs_has_video(avs->vi)) {
  572. avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
  573. avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
  574. } else {
  575. avs->curr_sample = timestamp;
  576. }
  577. }
  578. return 0;
  579. }
  580. AVInputFormat ff_avisynth_demuxer = {
  581. .name = "avisynth",
  582. .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
  583. .priv_data_size = sizeof(AviSynthContext),
  584. .read_header = avisynth_read_header,
  585. .read_packet = avisynth_read_packet,
  586. .read_close = avisynth_read_close,
  587. .read_seek = avisynth_read_seek,
  588. .extensions = "avs",
  589. };