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.

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