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.

669 lines
19KB

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