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.

696 lines
20KB

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