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.

690 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 "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 = NULL;
  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 = av_mallocz(sizeof(AviSynthLibrary));
  93. if (!avs_library)
  94. return AVERROR_UNKNOWN;
  95. avs_library->library = LoadLibrary(AVISYNTH_LIB);
  96. if (!avs_library->library)
  97. goto init_fail;
  98. #define LOAD_AVS_FUNC(name, continue_on_fail) \
  99. avs_library->name = \
  100. (void *)GetProcAddress(avs_library->library, #name); \
  101. if (!continue_on_fail && !avs_library->name) \
  102. goto fail;
  103. LOAD_AVS_FUNC(avs_bit_blt, 0);
  104. LOAD_AVS_FUNC(avs_clip_get_error, 0);
  105. LOAD_AVS_FUNC(avs_create_script_environment, 0);
  106. LOAD_AVS_FUNC(avs_delete_script_environment, 0);
  107. LOAD_AVS_FUNC(avs_get_audio, 0);
  108. LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
  109. LOAD_AVS_FUNC(avs_get_frame, 0);
  110. LOAD_AVS_FUNC(avs_get_version, 0);
  111. LOAD_AVS_FUNC(avs_get_video_info, 0);
  112. LOAD_AVS_FUNC(avs_invoke, 0);
  113. LOAD_AVS_FUNC(avs_release_clip, 0);
  114. LOAD_AVS_FUNC(avs_release_value, 0);
  115. LOAD_AVS_FUNC(avs_release_video_frame, 0);
  116. LOAD_AVS_FUNC(avs_take_clip, 0);
  117. #undef LOAD_AVS_FUNC
  118. atexit(avisynth_atexit_handler);
  119. return 0;
  120. fail:
  121. FreeLibrary(avs_library->library);
  122. init_fail:
  123. av_freep(&avs_library);
  124. return AVERROR_UNKNOWN;
  125. }
  126. /* Note that avisynth_context_create and avisynth_context_destroy
  127. * do not allocate or free the actual context! That is taken care of
  128. * by libavformat. */
  129. static av_cold int avisynth_context_create(AVFormatContext *s)
  130. {
  131. AviSynthContext *avs = s->priv_data;
  132. int ret;
  133. if (!avs_library)
  134. if (ret = avisynth_load_library())
  135. return ret;
  136. avs->env = avs_library->avs_create_script_environment(3);
  137. if (avs_library->avs_get_error) {
  138. const char *error = avs_library->avs_get_error(avs->env);
  139. if (error) {
  140. av_log(s, AV_LOG_ERROR, "%s\n", error);
  141. return AVERROR_UNKNOWN;
  142. }
  143. }
  144. if (!avs_ctx_list) {
  145. avs_ctx_list = avs;
  146. } else {
  147. avs->next = avs_ctx_list;
  148. avs_ctx_list = avs;
  149. }
  150. return 0;
  151. }
  152. static av_cold void avisynth_context_destroy(AviSynthContext *avs)
  153. {
  154. if (avs_atexit_called)
  155. return;
  156. if (avs == avs_ctx_list) {
  157. avs_ctx_list = avs->next;
  158. } else {
  159. AviSynthContext *prev = avs_ctx_list;
  160. while (prev->next != avs)
  161. prev = prev->next;
  162. prev->next = avs->next;
  163. }
  164. if (avs->clip) {
  165. avs_library->avs_release_clip(avs->clip);
  166. avs->clip = NULL;
  167. }
  168. if (avs->env) {
  169. avs_library->avs_delete_script_environment(avs->env);
  170. avs->env = NULL;
  171. }
  172. }
  173. static av_cold void avisynth_atexit_handler(void)
  174. {
  175. AviSynthContext *avs = avs_ctx_list;
  176. while (avs) {
  177. AviSynthContext *next = avs->next;
  178. avisynth_context_destroy(avs);
  179. avs = next;
  180. }
  181. FreeLibrary(avs_library->library);
  182. av_freep(&avs_library);
  183. avs_atexit_called = 1;
  184. }
  185. /* Create AVStream from audio and video data. */
  186. static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
  187. {
  188. AviSynthContext *avs = s->priv_data;
  189. int planar = 0; // 0: packed, 1: YUV, 2: Y8
  190. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  191. st->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
  192. st->codec->width = avs->vi->width;
  193. st->codec->height = avs->vi->height;
  194. st->time_base = (AVRational) { avs->vi->fps_denominator,
  195. avs->vi->fps_numerator };
  196. st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
  197. avs->vi->fps_denominator };
  198. st->start_time = 0;
  199. st->duration = avs->vi->num_frames;
  200. st->nb_frames = avs->vi->num_frames;
  201. switch (avs->vi->pixel_type) {
  202. #ifdef USING_AVISYNTH
  203. case AVS_CS_YV24:
  204. st->codec->pix_fmt = AV_PIX_FMT_YUV444P;
  205. planar = 1;
  206. break;
  207. case AVS_CS_YV16:
  208. st->codec->pix_fmt = AV_PIX_FMT_YUV422P;
  209. planar = 1;
  210. break;
  211. case AVS_CS_YV411:
  212. st->codec->pix_fmt = AV_PIX_FMT_YUV411P;
  213. planar = 1;
  214. break;
  215. case AVS_CS_Y8:
  216. st->codec->pix_fmt = AV_PIX_FMT_GRAY8;
  217. planar = 2;
  218. break;
  219. #endif
  220. case AVS_CS_BGR24:
  221. st->codec->pix_fmt = AV_PIX_FMT_BGR24;
  222. break;
  223. case AVS_CS_BGR32:
  224. st->codec->pix_fmt = AV_PIX_FMT_RGB32;
  225. break;
  226. case AVS_CS_YUY2:
  227. st->codec->pix_fmt = AV_PIX_FMT_YUYV422;
  228. break;
  229. case AVS_CS_YV12:
  230. st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
  231. planar = 1;
  232. break;
  233. case AVS_CS_I420: // Is this even used anywhere?
  234. st->codec->pix_fmt = AV_PIX_FMT_YUV420P;
  235. planar = 1;
  236. break;
  237. default:
  238. av_log(s, AV_LOG_ERROR,
  239. "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
  240. avs->error = 1;
  241. return AVERROR_UNKNOWN;
  242. }
  243. switch (planar) {
  244. case 2: // Y8
  245. avs->n_planes = 1;
  246. avs->planes = avs_planes_grey;
  247. break;
  248. case 1: // YUV
  249. avs->n_planes = 3;
  250. avs->planes = avs_planes_yuv;
  251. break;
  252. default:
  253. avs->n_planes = 1;
  254. avs->planes = avs_planes_packed;
  255. }
  256. return 0;
  257. }
  258. static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
  259. {
  260. AviSynthContext *avs = s->priv_data;
  261. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  262. st->codec->sample_rate = avs->vi->audio_samples_per_second;
  263. st->codec->channels = avs->vi->nchannels;
  264. st->time_base = (AVRational) { 1,
  265. avs->vi->audio_samples_per_second };
  266. switch (avs->vi->sample_type) {
  267. case AVS_SAMPLE_INT8:
  268. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  269. break;
  270. case AVS_SAMPLE_INT16:
  271. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  272. break;
  273. case AVS_SAMPLE_INT24:
  274. st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
  275. break;
  276. case AVS_SAMPLE_INT32:
  277. st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
  278. break;
  279. case AVS_SAMPLE_FLOAT:
  280. st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
  281. break;
  282. default:
  283. av_log(s, AV_LOG_ERROR,
  284. "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. {
  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. {
  316. AviSynthContext *avs = s->priv_data;
  317. AVS_Value arg, val;
  318. int ret;
  319. #ifdef USING_AVISYNTH
  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 USING_AVISYNTH
  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,
  329. MAX_PATH * 4, NULL, NULL);
  330. arg = avs_new_value_string(filename_ansi);
  331. #else
  332. arg = avs_new_value_string(s->filename);
  333. #endif
  334. val = avs_library->avs_invoke(avs->env, "Import", arg, 0);
  335. if (avs_is_error(val)) {
  336. av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
  337. ret = AVERROR_UNKNOWN;
  338. goto fail;
  339. }
  340. if (!avs_is_clip(val)) {
  341. av_log(s, AV_LOG_ERROR, "AviSynth script did not return a clip\n");
  342. ret = AVERROR_UNKNOWN;
  343. goto fail;
  344. }
  345. avs->clip = avs_library->avs_take_clip(val, avs->env);
  346. avs->vi = avs_library->avs_get_video_info(avs->clip);
  347. /* Release the AVS_Value as it will go out of scope. */
  348. avs_library->avs_release_value(val);
  349. if (ret = avisynth_create_stream(s))
  350. goto fail;
  351. return 0;
  352. fail:
  353. avisynth_context_destroy(avs);
  354. return ret;
  355. }
  356. static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
  357. AVPacket *pkt, int *discard)
  358. {
  359. AviSynthContext *avs = s->priv_data;
  360. pkt->stream_index = avs->curr_stream++;
  361. avs->curr_stream %= s->nb_streams;
  362. *st = s->streams[pkt->stream_index];
  363. if ((*st)->discard == AVDISCARD_ALL)
  364. *discard = 1;
  365. else
  366. *discard = 0;
  367. return;
  368. }
  369. /* Copy AviSynth clip data into an AVPacket. */
  370. static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
  371. int discard)
  372. {
  373. AviSynthContext *avs = s->priv_data;
  374. AVS_VideoFrame *frame;
  375. unsigned char *dst_p;
  376. const unsigned char *src_p;
  377. int n, i, plane, rowsize, planeheight, pitch, bits;
  378. const char *error;
  379. if (avs->curr_frame >= avs->vi->num_frames)
  380. return AVERROR_EOF;
  381. /* This must happen even if the stream is discarded to prevent desync. */
  382. n = avs->curr_frame++;
  383. if (discard)
  384. return 0;
  385. pkt->pts = n;
  386. pkt->dts = n;
  387. pkt->duration = 1;
  388. #ifdef USING_AVISYNTH
  389. /* Define the bpp values for the new AviSynth 2.6 colorspaces.
  390. * Since AvxSynth doesn't have these functions, special-case
  391. * it in order to avoid implicit declaration errors. */
  392. if (avs_is_yv24(avs->vi))
  393. bits = 24;
  394. else if (avs_is_yv16(avs->vi))
  395. bits = 16;
  396. else if (avs_is_yv411(avs->vi))
  397. bits = 12;
  398. else if (avs_is_y8(avs->vi))
  399. bits = 8;
  400. else
  401. #endif
  402. bits = avs_bits_per_pixel(avs->vi);
  403. /* Without the cast to int64_t, calculation overflows at about 9k x 9k
  404. * resolution. */
  405. pkt->size = (((int64_t)avs->vi->width *
  406. (int64_t)avs->vi->height) * bits) / 8;
  407. if (!pkt->size)
  408. return AVERROR_UNKNOWN;
  409. pkt->data = av_malloc(pkt->size);
  410. if (!pkt->data)
  411. return AVERROR(ENOMEM);
  412. frame = avs_library->avs_get_frame(avs->clip, n);
  413. error = avs_library->avs_clip_get_error(avs->clip);
  414. if (error) {
  415. av_log(s, AV_LOG_ERROR, "%s\n", error);
  416. avs->error = 1;
  417. av_freep(&pkt->data);
  418. return AVERROR_UNKNOWN;
  419. }
  420. dst_p = pkt->data;
  421. for (i = 0; i < avs->n_planes; i++) {
  422. plane = avs->planes[i];
  423. src_p = avs_get_read_ptr_p(frame, plane);
  424. pitch = avs_get_pitch_p(frame, plane);
  425. #ifdef USING_AVISYNTH
  426. if (avs_library->avs_get_version(avs->clip) == 3) {
  427. rowsize = avs_get_row_size_p_25(frame, plane);
  428. planeheight = avs_get_height_p_25(frame, plane);
  429. } else {
  430. rowsize = avs_get_row_size_p(frame, plane);
  431. planeheight = avs_get_height_p(frame, plane);
  432. }
  433. #else
  434. rowsize = avs_get_row_size_p(frame, plane);
  435. planeheight = avs_get_height_p(frame, plane);
  436. #endif
  437. /* Flip RGB video. */
  438. if (avs_is_rgb24(avs->vi) || avs_is_rgb(avs->vi)) {
  439. src_p = src_p + (planeheight - 1) * pitch;
  440. pitch = -pitch;
  441. }
  442. avs_library->avs_bit_blt(avs->env, dst_p, rowsize, src_p, pitch,
  443. rowsize, planeheight);
  444. dst_p += rowsize * planeheight;
  445. }
  446. avs_library->avs_release_video_frame(frame);
  447. return 0;
  448. }
  449. static int avisynth_read_packet_audio(AVFormatContext *s, AVPacket *pkt,
  450. int discard)
  451. {
  452. AviSynthContext *avs = s->priv_data;
  453. AVRational fps, samplerate;
  454. int samples;
  455. int64_t n;
  456. const char *error;
  457. if (avs->curr_sample >= avs->vi->num_audio_samples)
  458. return AVERROR_EOF;
  459. fps.num = avs->vi->fps_numerator;
  460. fps.den = avs->vi->fps_denominator;
  461. samplerate.num = avs->vi->audio_samples_per_second;
  462. samplerate.den = 1;
  463. if (avs_has_video(avs->vi)) {
  464. if (avs->curr_frame < avs->vi->num_frames)
  465. samples = av_rescale_q(avs->curr_frame, samplerate, fps) -
  466. avs->curr_sample;
  467. else
  468. samples = av_rescale_q(1, samplerate, fps);
  469. } else {
  470. samples = 1000;
  471. }
  472. /* After seeking, audio may catch up with video. */
  473. if (samples <= 0) {
  474. pkt->size = 0;
  475. pkt->data = NULL;
  476. return 0;
  477. }
  478. if (avs->curr_sample + samples > avs->vi->num_audio_samples)
  479. samples = avs->vi->num_audio_samples - avs->curr_sample;
  480. /* This must happen even if the stream is discarded to prevent desync. */
  481. n = avs->curr_sample;
  482. avs->curr_sample += samples;
  483. if (discard)
  484. return 0;
  485. pkt->pts = n;
  486. pkt->dts = n;
  487. pkt->duration = samples;
  488. pkt->size = avs_bytes_per_channel_sample(avs->vi) *
  489. samples * avs->vi->nchannels;
  490. if (!pkt->size)
  491. return AVERROR_UNKNOWN;
  492. pkt->data = av_malloc(pkt->size);
  493. if (!pkt->data)
  494. return AVERROR(ENOMEM);
  495. avs_library->avs_get_audio(avs->clip, pkt->data, n, samples);
  496. error = avs_library->avs_clip_get_error(avs->clip);
  497. if (error) {
  498. av_log(s, AV_LOG_ERROR, "%s\n", error);
  499. avs->error = 1;
  500. av_freep(&pkt->data);
  501. return AVERROR_UNKNOWN;
  502. }
  503. return 0;
  504. }
  505. static av_cold int avisynth_read_header(AVFormatContext *s)
  506. {
  507. int ret;
  508. // Calling library must implement a lock for thread-safe opens.
  509. if (ret = avpriv_lock_avformat())
  510. return ret;
  511. if (ret = avisynth_open_file(s)) {
  512. avpriv_unlock_avformat();
  513. return ret;
  514. }
  515. avpriv_unlock_avformat();
  516. return 0;
  517. }
  518. static int avisynth_read_packet(AVFormatContext *s, AVPacket *pkt)
  519. {
  520. AviSynthContext *avs = s->priv_data;
  521. AVStream *st;
  522. int discard = 0;
  523. int ret;
  524. if (avs->error)
  525. return AVERROR_UNKNOWN;
  526. pkt->destruct = av_destruct_packet;
  527. /* If either stream reaches EOF, try to read the other one before
  528. * giving up. */
  529. avisynth_next_stream(s, &st, pkt, &discard);
  530. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  531. ret = avisynth_read_packet_video(s, pkt, discard);
  532. if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
  533. avisynth_next_stream(s, &st, pkt, &discard);
  534. return avisynth_read_packet_audio(s, pkt, discard);
  535. }
  536. } else {
  537. ret = avisynth_read_packet_audio(s, pkt, discard);
  538. if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
  539. avisynth_next_stream(s, &st, pkt, &discard);
  540. return avisynth_read_packet_video(s, pkt, discard);
  541. }
  542. }
  543. return ret;
  544. }
  545. static av_cold int avisynth_read_close(AVFormatContext *s)
  546. {
  547. if (avpriv_lock_avformat())
  548. return AVERROR_UNKNOWN;
  549. avisynth_context_destroy(s->priv_data);
  550. avpriv_unlock_avformat();
  551. return 0;
  552. }
  553. static int avisynth_read_seek(AVFormatContext *s, int stream_index,
  554. int64_t timestamp, int flags)
  555. {
  556. AviSynthContext *avs = s->priv_data;
  557. AVStream *st;
  558. AVRational fps, samplerate;
  559. if (avs->error)
  560. return AVERROR_UNKNOWN;
  561. fps = (AVRational) { avs->vi->fps_numerator,
  562. avs->vi->fps_denominator };
  563. samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
  564. st = s->streams[stream_index];
  565. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  566. /* AviSynth frame counts are signed int. */
  567. if ((timestamp >= avs->vi->num_frames) ||
  568. (timestamp > INT_MAX) ||
  569. (timestamp < 0))
  570. return AVERROR_EOF;
  571. avs->curr_frame = timestamp;
  572. if (avs_has_audio(avs->vi))
  573. avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
  574. } else {
  575. if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
  576. return AVERROR_EOF;
  577. /* Force frame granularity for seeking. */
  578. if (avs_has_video(avs->vi)) {
  579. avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
  580. avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
  581. } else {
  582. avs->curr_sample = timestamp;
  583. }
  584. }
  585. return 0;
  586. }
  587. AVInputFormat ff_avisynth_demuxer = {
  588. .name = "avisynth",
  589. .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
  590. .priv_data_size = sizeof(AviSynthContext),
  591. .read_header = avisynth_read_header,
  592. .read_packet = avisynth_read_packet,
  593. .read_close = avisynth_read_close,
  594. .read_seek = avisynth_read_seek,
  595. .extensions = "avs",
  596. };