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. * 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. // Shut up ffmpeg error messages.
  27. // avisynth_c.h contains inline functions that call these functions.
  28. #undef malloc
  29. #undef free
  30. #undef printf
  31. /* Platform-specific directives for AviSynth vs AvxSynth. */
  32. #ifdef _WIN32
  33. #include <windows.h>
  34. #undef EXTERN_C
  35. #include "compat/avisynth/avisynth_c.h"
  36. #include "compat/avisynth/avisynth_c_25.h"
  37. #define AVISYNTH_LIB "avisynth"
  38. #define USING_AVISYNTH
  39. #else
  40. #include <dlfcn.h>
  41. #include "compat/avisynth/avxsynth_c.h"
  42. #if defined (__APPLE__)
  43. #define AVISYNTH_LIB "libavxsynth.dylib"
  44. #else
  45. #define AVISYNTH_LIB "libavxsynth.so"
  46. #endif
  47. #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_GLOBAL)
  48. #define GetProcAddress dlsym
  49. #define FreeLibrary dlclose
  50. #endif
  51. typedef struct AviSynthLibrary {
  52. void *library;
  53. #define AVSC_DECLARE_FUNC(name) name ## _func name
  54. AVSC_DECLARE_FUNC(avs_bit_blt);
  55. AVSC_DECLARE_FUNC(avs_clip_get_error);
  56. AVSC_DECLARE_FUNC(avs_create_script_environment);
  57. AVSC_DECLARE_FUNC(avs_delete_script_environment);
  58. AVSC_DECLARE_FUNC(avs_get_audio);
  59. AVSC_DECLARE_FUNC(avs_get_error);
  60. AVSC_DECLARE_FUNC(avs_get_frame);
  61. AVSC_DECLARE_FUNC(avs_get_version);
  62. AVSC_DECLARE_FUNC(avs_get_video_info);
  63. AVSC_DECLARE_FUNC(avs_invoke);
  64. AVSC_DECLARE_FUNC(avs_release_clip);
  65. AVSC_DECLARE_FUNC(avs_release_value);
  66. AVSC_DECLARE_FUNC(avs_release_video_frame);
  67. AVSC_DECLARE_FUNC(avs_take_clip);
  68. #undef AVSC_DECLARE_FUNC
  69. } AviSynthLibrary;
  70. typedef struct AviSynthContext {
  71. AVS_ScriptEnvironment *env;
  72. AVS_Clip *clip;
  73. const AVS_VideoInfo *vi;
  74. /* avisynth_read_packet_video() iterates over this. */
  75. int n_planes;
  76. const int *planes;
  77. int curr_stream;
  78. int curr_frame;
  79. int64_t curr_sample;
  80. int error;
  81. /* Linked list pointers. */
  82. struct AviSynthContext *next;
  83. } AviSynthContext;
  84. static const int avs_planes_packed[1] = { 0 };
  85. static const int avs_planes_grey[1] = { AVS_PLANAR_Y };
  86. static const int avs_planes_yuv[3] = { AVS_PLANAR_Y, AVS_PLANAR_U,
  87. AVS_PLANAR_V };
  88. /* A conflict between C++ global objects, atexit, and dynamic loading requires
  89. * us to register our own atexit handler to prevent double freeing. */
  90. static AviSynthLibrary *avs_library = NULL;
  91. static int avs_atexit_called = 0;
  92. /* Linked list of AviSynthContexts. An atexit handler destroys this list. */
  93. static AviSynthContext *avs_ctx_list = NULL;
  94. static av_cold void avisynth_atexit_handler(void);
  95. static av_cold int avisynth_load_library(void)
  96. {
  97. avs_library = av_mallocz(sizeof(AviSynthLibrary));
  98. if (!avs_library)
  99. return AVERROR_UNKNOWN;
  100. avs_library->library = LoadLibrary(AVISYNTH_LIB);
  101. if (!avs_library->library)
  102. goto init_fail;
  103. #define LOAD_AVS_FUNC(name, continue_on_fail) \
  104. avs_library->name = \
  105. (void *)GetProcAddress(avs_library->library, #name); \
  106. if (!continue_on_fail && !avs_library->name) \
  107. goto fail;
  108. LOAD_AVS_FUNC(avs_bit_blt, 0);
  109. LOAD_AVS_FUNC(avs_clip_get_error, 0);
  110. LOAD_AVS_FUNC(avs_create_script_environment, 0);
  111. LOAD_AVS_FUNC(avs_delete_script_environment, 0);
  112. LOAD_AVS_FUNC(avs_get_audio, 0);
  113. LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
  114. LOAD_AVS_FUNC(avs_get_frame, 0);
  115. LOAD_AVS_FUNC(avs_get_version, 0);
  116. LOAD_AVS_FUNC(avs_get_video_info, 0);
  117. LOAD_AVS_FUNC(avs_invoke, 0);
  118. LOAD_AVS_FUNC(avs_release_clip, 0);
  119. LOAD_AVS_FUNC(avs_release_value, 0);
  120. LOAD_AVS_FUNC(avs_release_video_frame, 0);
  121. LOAD_AVS_FUNC(avs_take_clip, 0);
  122. #undef LOAD_AVS_FUNC
  123. atexit(avisynth_atexit_handler);
  124. return 0;
  125. fail:
  126. FreeLibrary(avs_library->library);
  127. init_fail:
  128. av_freep(&avs_library);
  129. return AVERROR_UNKNOWN;
  130. }
  131. /* Note that avisynth_context_create and avisynth_context_destroy
  132. * do not allocate or free the actual context! That is taken care of
  133. * by libavformat. */
  134. static av_cold int avisynth_context_create(AVFormatContext *s)
  135. {
  136. AviSynthContext *avs = s->priv_data;
  137. int ret;
  138. if (!avs_library)
  139. if (ret = avisynth_load_library())
  140. return ret;
  141. avs->env = avs_library->avs_create_script_environment(3);
  142. if (avs_library->avs_get_error) {
  143. const char *error = avs_library->avs_get_error(avs->env);
  144. if (error) {
  145. av_log(s, AV_LOG_ERROR, "%s\n", error);
  146. return AVERROR_UNKNOWN;
  147. }
  148. }
  149. if (!avs_ctx_list) {
  150. avs_ctx_list = avs;
  151. } else {
  152. avs->next = avs_ctx_list;
  153. avs_ctx_list = avs;
  154. }
  155. return 0;
  156. }
  157. static av_cold void avisynth_context_destroy(AviSynthContext *avs)
  158. {
  159. if (avs_atexit_called)
  160. return;
  161. if (avs == avs_ctx_list) {
  162. avs_ctx_list = avs->next;
  163. } else {
  164. AviSynthContext *prev = avs_ctx_list;
  165. while (prev->next != avs)
  166. prev = prev->next;
  167. prev->next = avs->next;
  168. }
  169. if (avs->clip) {
  170. avs_library->avs_release_clip(avs->clip);
  171. avs->clip = NULL;
  172. }
  173. if (avs->env) {
  174. avs_library->avs_delete_script_environment(avs->env);
  175. avs->env = NULL;
  176. }
  177. }
  178. static av_cold void avisynth_atexit_handler(void)
  179. {
  180. AviSynthContext *avs = avs_ctx_list;
  181. while (avs) {
  182. AviSynthContext *next = avs->next;
  183. avisynth_context_destroy(avs);
  184. avs = next;
  185. }
  186. FreeLibrary(avs_library->library);
  187. av_freep(&avs_library);
  188. avs_atexit_called = 1;
  189. }
  190. /* Create AVStream from audio and video data. */
  191. static int avisynth_create_stream_video(AVFormatContext *s, AVStream *st)
  192. {
  193. AviSynthContext *avs = s->priv_data;
  194. int planar = 0; // 0: packed, 1: YUV, 2: Y8
  195. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  196. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  197. st->codec->width = avs->vi->width;
  198. st->codec->height = avs->vi->height;
  199. st->time_base = (AVRational) { avs->vi->fps_denominator,
  200. avs->vi->fps_numerator };
  201. st->avg_frame_rate = (AVRational) { avs->vi->fps_numerator,
  202. 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 USING_AVISYNTH
  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,
  244. "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
  245. avs->error = 1;
  246. return AVERROR_UNKNOWN;
  247. }
  248. switch (planar) {
  249. case 2: // Y8
  250. avs->n_planes = 1;
  251. avs->planes = avs_planes_grey;
  252. break;
  253. case 1: // YUV
  254. avs->n_planes = 3;
  255. avs->planes = avs_planes_yuv;
  256. break;
  257. default:
  258. avs->n_planes = 1;
  259. avs->planes = avs_planes_packed;
  260. }
  261. return 0;
  262. }
  263. static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
  264. {
  265. AviSynthContext *avs = s->priv_data;
  266. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  267. st->codec->sample_rate = avs->vi->audio_samples_per_second;
  268. st->codec->channels = avs->vi->nchannels;
  269. st->time_base = (AVRational) { 1,
  270. avs->vi->audio_samples_per_second };
  271. switch (avs->vi->sample_type) {
  272. case AVS_SAMPLE_INT8:
  273. st->codec->codec_id = CODEC_ID_PCM_U8;
  274. break;
  275. case AVS_SAMPLE_INT16:
  276. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  277. break;
  278. case AVS_SAMPLE_INT24:
  279. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  280. break;
  281. case AVS_SAMPLE_INT32:
  282. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  283. break;
  284. case AVS_SAMPLE_FLOAT:
  285. st->codec->codec_id = CODEC_ID_PCM_F32LE;
  286. break;
  287. default:
  288. av_log(s, AV_LOG_ERROR,
  289. "unknown AviSynth sample type %d\n", avs->vi->sample_type);
  290. avs->error = 1;
  291. return AVERROR_UNKNOWN;
  292. }
  293. return 0;
  294. }
  295. static int avisynth_create_stream(AVFormatContext *s)
  296. {
  297. AviSynthContext *avs = s->priv_data;
  298. AVStream *st;
  299. int ret;
  300. int id = 0;
  301. if (avs_has_video(avs->vi)) {
  302. st = avformat_new_stream(s, NULL);
  303. if (!st)
  304. return AVERROR_UNKNOWN;
  305. st->id = id++;
  306. if (ret = avisynth_create_stream_video(s, st))
  307. return ret;
  308. }
  309. if (avs_has_audio(avs->vi)) {
  310. st = avformat_new_stream(s, NULL);
  311. if (!st)
  312. return AVERROR_UNKNOWN;
  313. st->id = id++;
  314. if (ret = avisynth_create_stream_audio(s, st))
  315. return ret;
  316. }
  317. return 0;
  318. }
  319. static int avisynth_open_file(AVFormatContext *s)
  320. {
  321. AviSynthContext *avs = s->priv_data;
  322. AVS_Value arg, val;
  323. int ret;
  324. #ifdef USING_AVISYNTH
  325. char filename_ansi[MAX_PATH * 4];
  326. wchar_t filename_wc[MAX_PATH * 4];
  327. #endif
  328. if (ret = avisynth_context_create(s))
  329. return ret;
  330. #ifdef USING_AVISYNTH
  331. /* Convert UTF-8 to ANSI code page */
  332. MultiByteToWideChar(CP_UTF8, 0, s->filename, -1, filename_wc, MAX_PATH * 4);
  333. WideCharToMultiByte(CP_THREAD_ACP, 0, filename_wc, -1, filename_ansi,
  334. MAX_PATH * 4, NULL, NULL);
  335. arg = avs_new_value_string(filename_ansi);
  336. #else
  337. arg = avs_new_value_string(s->filename);
  338. #endif
  339. val = avs_library->avs_invoke(avs->env, "Import", arg, 0);
  340. if (avs_is_error(val)) {
  341. av_log(s, AV_LOG_ERROR, "%s\n", avs_as_error(val));
  342. ret = AVERROR_UNKNOWN;
  343. goto fail;
  344. }
  345. if (!avs_is_clip(val)) {
  346. av_log(s, AV_LOG_ERROR, "%s\n", "AviSynth script did not return a clip");
  347. ret = AVERROR_UNKNOWN;
  348. goto fail;
  349. }
  350. avs->clip = avs_library->avs_take_clip(val, avs->env);
  351. avs->vi = avs_library->avs_get_video_info(avs->clip);
  352. /* Release the AVS_Value as it will go out of scope. */
  353. avs_library->avs_release_value(val);
  354. if (ret = avisynth_create_stream(s))
  355. goto fail;
  356. return 0;
  357. fail:
  358. avisynth_context_destroy(avs);
  359. return ret;
  360. }
  361. static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
  362. AVPacket *pkt, int *discard)
  363. {
  364. AviSynthContext *avs = s->priv_data;
  365. pkt->stream_index = avs->curr_stream++;
  366. avs->curr_stream %= s->nb_streams;
  367. *st = s->streams[pkt->stream_index];
  368. if ((*st)->discard == AVDISCARD_ALL)
  369. *discard = 1;
  370. else
  371. *discard = 0;
  372. return;
  373. }
  374. /* Copy AviSynth clip data into an AVPacket. */
  375. static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
  376. int discard)
  377. {
  378. AviSynthContext *avs = s->priv_data;
  379. AVS_VideoFrame *frame;
  380. unsigned char *dst_p;
  381. const unsigned char *src_p;
  382. int n, i, plane, rowsize, planeheight, pitch, bits;
  383. const char *error;
  384. if (avs->curr_frame >= avs->vi->num_frames)
  385. return AVERROR_EOF;
  386. /* This must happen even if the stream is discarded to prevent desync. */
  387. n = avs->curr_frame++;
  388. if (discard)
  389. return 0;
  390. pkt->pts = n;
  391. pkt->dts = n;
  392. pkt->duration = 1;
  393. #ifdef USING_AVISYNTH
  394. /* Define the bpp values for the new AviSynth 2.6 colorspaces.
  395. * Since AvxSynth doesn't have these functions, special-case
  396. * it in order to avoid implicit declaration errors. */
  397. if (avs_is_yv24(avs->vi))
  398. bits = 24;
  399. else if (avs_is_yv16(avs->vi))
  400. bits = 16;
  401. else if (avs_is_yv411(avs->vi))
  402. bits = 12;
  403. else if (avs_is_y8(avs->vi))
  404. bits = 8;
  405. else
  406. #endif
  407. bits = avs_bits_per_pixel(avs->vi);
  408. /* Without the cast to int64_t, calculation overflows at about 9k x 9k
  409. * resolution. */
  410. pkt->size = (((int64_t)avs->vi->width *
  411. (int64_t)avs->vi->height) * bits) / 8;
  412. if (!pkt->size)
  413. return AVERROR_UNKNOWN;
  414. pkt->data = av_malloc(pkt->size);
  415. if (!pkt->data)
  416. return AVERROR_UNKNOWN;
  417. frame = avs_library->avs_get_frame(avs->clip, n);
  418. error = avs_library->avs_clip_get_error(avs->clip);
  419. if (error) {
  420. av_log(s, AV_LOG_ERROR, "%s\n", error);
  421. avs->error = 1;
  422. av_freep(&pkt->data);
  423. return AVERROR_UNKNOWN;
  424. }
  425. dst_p = pkt->data;
  426. for (i = 0; i < avs->n_planes; i++) {
  427. plane = avs->planes[i];
  428. src_p = avs_get_read_ptr_p(frame, plane);
  429. pitch = avs_get_pitch_p(frame, plane);
  430. #ifdef USING_AVISYNTH
  431. if (avs_library->avs_get_version(avs->clip) == 3) {
  432. rowsize = avs_get_row_size_p_25(frame, plane);
  433. planeheight = avs_get_height_p_25(frame, plane);
  434. } else {
  435. rowsize = avs_get_row_size_p(frame, plane);
  436. planeheight = avs_get_height_p(frame, plane);
  437. }
  438. #else
  439. rowsize = avs_get_row_size_p(frame, plane);
  440. planeheight = avs_get_height_p(frame, plane);
  441. #endif
  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->pts = n;
  491. pkt->dts = n;
  492. pkt->duration = samples;
  493. pkt->size = avs_bytes_per_channel_sample(avs->vi) *
  494. samples * avs->vi->nchannels;
  495. if (!pkt->size)
  496. return AVERROR_UNKNOWN;
  497. pkt->data = av_malloc(pkt->size);
  498. if (!pkt->data)
  499. return AVERROR_UNKNOWN;
  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_freep(&pkt->data);
  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. pkt->destruct = av_destruct_packet;
  532. /* If either stream reaches EOF, try to read the other one before
  533. * giving up. */
  534. avisynth_next_stream(s, &st, pkt, &discard);
  535. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  536. ret = avisynth_read_packet_video(s, pkt, discard);
  537. if (ret == AVERROR_EOF && avs_has_audio(avs->vi)) {
  538. avisynth_next_stream(s, &st, pkt, &discard);
  539. return avisynth_read_packet_audio(s, pkt, discard);
  540. }
  541. return ret;
  542. } else {
  543. ret = avisynth_read_packet_audio(s, pkt, discard);
  544. if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
  545. avisynth_next_stream(s, &st, pkt, &discard);
  546. return avisynth_read_packet_video(s, pkt, discard);
  547. }
  548. return ret;
  549. }
  550. }
  551. static av_cold int avisynth_read_close(AVFormatContext *s)
  552. {
  553. if (avpriv_lock_avformat())
  554. return AVERROR_UNKNOWN;
  555. avisynth_context_destroy(s->priv_data);
  556. avpriv_unlock_avformat();
  557. return 0;
  558. }
  559. static int avisynth_read_seek(AVFormatContext *s, int stream_index,
  560. int64_t timestamp, int flags)
  561. {
  562. AviSynthContext *avs = s->priv_data;
  563. AVStream *st;
  564. AVRational fps, samplerate;
  565. if (avs->error)
  566. return AVERROR_UNKNOWN;
  567. fps = (AVRational) { avs->vi->fps_numerator,
  568. avs->vi->fps_denominator };
  569. samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
  570. st = s->streams[stream_index];
  571. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  572. /* AviSynth frame counts are signed int. */
  573. if ((timestamp >= avs->vi->num_frames) ||
  574. (timestamp > INT_MAX) ||
  575. (timestamp < 0))
  576. return AVERROR_EOF;
  577. avs->curr_frame = timestamp;
  578. if (avs_has_audio(avs->vi))
  579. avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
  580. } else {
  581. if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
  582. return AVERROR_EOF;
  583. /* Force frame granularity for seeking. */
  584. if (avs_has_video(avs->vi)) {
  585. avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
  586. avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
  587. } else {
  588. avs->curr_sample = timestamp;
  589. }
  590. }
  591. return 0;
  592. }
  593. AVInputFormat ff_avisynth_demuxer = {
  594. .name = "avisynth",
  595. .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
  596. .priv_data_size = sizeof(AviSynthContext),
  597. .read_header = avisynth_read_header,
  598. .read_packet = avisynth_read_packet,
  599. .read_close = avisynth_read_close,
  600. .read_seek = avisynth_read_seek,
  601. .extensions = "avs",
  602. };