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.

698 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. #include "config.h"
  26. /* Enable function pointer definitions for runtime loading. */
  27. #define AVSC_NO_DECLSPEC
  28. /* Platform-specific directives for AviSynth vs AvxSynth. */
  29. #ifdef _WIN32
  30. #include <windows.h>
  31. #undef EXTERN_C
  32. #include <avisynth/avisynth_c.h>
  33. #define AVISYNTH_LIB "avisynth"
  34. #define USING_AVISYNTH
  35. #else
  36. #include <dlfcn.h>
  37. #include <avxsynth/avxsynth_c.h>
  38. #define AVISYNTH_NAME "libavxsynth"
  39. #define AVISYNTH_LIB AVISYNTH_NAME SLIBSUF
  40. #define LoadLibrary(x) dlopen(x, RTLD_NOW | RTLD_LOCAL)
  41. #define GetProcAddress dlsym
  42. #define FreeLibrary dlclose
  43. #endif
  44. typedef struct AviSynthLibrary {
  45. void *library;
  46. #define AVSC_DECLARE_FUNC(name) name ## _func name
  47. AVSC_DECLARE_FUNC(avs_bit_blt);
  48. AVSC_DECLARE_FUNC(avs_clip_get_error);
  49. AVSC_DECLARE_FUNC(avs_create_script_environment);
  50. AVSC_DECLARE_FUNC(avs_delete_script_environment);
  51. AVSC_DECLARE_FUNC(avs_get_audio);
  52. AVSC_DECLARE_FUNC(avs_get_error);
  53. AVSC_DECLARE_FUNC(avs_get_frame);
  54. AVSC_DECLARE_FUNC(avs_get_version);
  55. AVSC_DECLARE_FUNC(avs_get_video_info);
  56. AVSC_DECLARE_FUNC(avs_invoke);
  57. AVSC_DECLARE_FUNC(avs_release_clip);
  58. AVSC_DECLARE_FUNC(avs_release_value);
  59. AVSC_DECLARE_FUNC(avs_release_video_frame);
  60. AVSC_DECLARE_FUNC(avs_take_clip);
  61. #ifdef USING_AVISYNTH
  62. AVSC_DECLARE_FUNC(avs_bits_per_pixel);
  63. AVSC_DECLARE_FUNC(avs_get_height_p);
  64. AVSC_DECLARE_FUNC(avs_get_pitch_p);
  65. AVSC_DECLARE_FUNC(avs_get_read_ptr_p);
  66. AVSC_DECLARE_FUNC(avs_get_row_size_p);
  67. #endif
  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;
  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.library = LoadLibrary(AVISYNTH_LIB);
  98. if (!avs_library.library)
  99. return AVERROR_UNKNOWN;
  100. #define LOAD_AVS_FUNC(name, continue_on_fail) \
  101. avs_library.name = GetProcAddress(avs_library.library, #name); \
  102. if (!continue_on_fail && !avs_library.name) \
  103. goto fail;
  104. LOAD_AVS_FUNC(avs_bit_blt, 0);
  105. LOAD_AVS_FUNC(avs_clip_get_error, 0);
  106. LOAD_AVS_FUNC(avs_create_script_environment, 0);
  107. LOAD_AVS_FUNC(avs_delete_script_environment, 0);
  108. LOAD_AVS_FUNC(avs_get_audio, 0);
  109. LOAD_AVS_FUNC(avs_get_error, 1); // New to AviSynth 2.6
  110. LOAD_AVS_FUNC(avs_get_frame, 0);
  111. LOAD_AVS_FUNC(avs_get_version, 0);
  112. LOAD_AVS_FUNC(avs_get_video_info, 0);
  113. LOAD_AVS_FUNC(avs_invoke, 0);
  114. LOAD_AVS_FUNC(avs_release_clip, 0);
  115. LOAD_AVS_FUNC(avs_release_value, 0);
  116. LOAD_AVS_FUNC(avs_release_video_frame, 0);
  117. LOAD_AVS_FUNC(avs_take_clip, 0);
  118. #ifdef USING_AVISYNTH
  119. LOAD_AVS_FUNC(avs_bits_per_pixel, 1);
  120. LOAD_AVS_FUNC(avs_get_height_p, 1);
  121. LOAD_AVS_FUNC(avs_get_pitch_p, 1);
  122. LOAD_AVS_FUNC(avs_get_read_ptr_p, 1);
  123. LOAD_AVS_FUNC(avs_get_row_size_p, 1);
  124. #endif
  125. #undef LOAD_AVS_FUNC
  126. atexit(avisynth_atexit_handler);
  127. return 0;
  128. fail:
  129. FreeLibrary(avs_library.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 = s->priv_data;
  138. int ret;
  139. if (!avs_library.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. 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->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  196. st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
  197. st->codecpar->width = avs->vi->width;
  198. st->codecpar->height = avs->vi->height;
  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. avpriv_set_pts_info(st, 32, avs->vi->fps_denominator, avs->vi->fps_numerator);
  205. switch (avs->vi->pixel_type) {
  206. #ifdef USING_AVISYNTH
  207. case AVS_CS_YV24:
  208. st->codecpar->format = AV_PIX_FMT_YUV444P;
  209. planar = 1;
  210. break;
  211. case AVS_CS_YV16:
  212. st->codecpar->format = AV_PIX_FMT_YUV422P;
  213. planar = 1;
  214. break;
  215. case AVS_CS_YV411:
  216. st->codecpar->format = AV_PIX_FMT_YUV411P;
  217. planar = 1;
  218. break;
  219. case AVS_CS_Y8:
  220. st->codecpar->format = AV_PIX_FMT_GRAY8;
  221. planar = 2;
  222. break;
  223. #endif
  224. case AVS_CS_BGR24:
  225. st->codecpar->format = AV_PIX_FMT_BGR24;
  226. break;
  227. case AVS_CS_BGR32:
  228. st->codecpar->format = AV_PIX_FMT_RGB32;
  229. break;
  230. case AVS_CS_YUY2:
  231. st->codecpar->format = AV_PIX_FMT_YUYV422;
  232. break;
  233. case AVS_CS_YV12:
  234. st->codecpar->format = AV_PIX_FMT_YUV420P;
  235. planar = 1;
  236. break;
  237. case AVS_CS_I420: // Is this even used anywhere?
  238. st->codecpar->format = AV_PIX_FMT_YUV420P;
  239. planar = 1;
  240. break;
  241. default:
  242. av_log(s, AV_LOG_ERROR,
  243. "unknown AviSynth colorspace %d\n", avs->vi->pixel_type);
  244. avs->error = 1;
  245. return AVERROR_UNKNOWN;
  246. }
  247. switch (planar) {
  248. case 2: // Y8
  249. avs->n_planes = 1;
  250. avs->planes = avs_planes_grey;
  251. break;
  252. case 1: // YUV
  253. avs->n_planes = 3;
  254. avs->planes = avs_planes_yuv;
  255. break;
  256. default:
  257. avs->n_planes = 1;
  258. avs->planes = avs_planes_packed;
  259. }
  260. return 0;
  261. }
  262. static int avisynth_create_stream_audio(AVFormatContext *s, AVStream *st)
  263. {
  264. AviSynthContext *avs = s->priv_data;
  265. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  266. st->codecpar->sample_rate = avs->vi->audio_samples_per_second;
  267. st->codecpar->channels = avs->vi->nchannels;
  268. st->duration = avs->vi->num_audio_samples;
  269. avpriv_set_pts_info(st, 64, 1, avs->vi->audio_samples_per_second);
  270. switch (avs->vi->sample_type) {
  271. case AVS_SAMPLE_INT8:
  272. st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
  273. break;
  274. case AVS_SAMPLE_INT16:
  275. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  276. break;
  277. case AVS_SAMPLE_INT24:
  278. st->codecpar->codec_id = AV_CODEC_ID_PCM_S24LE;
  279. break;
  280. case AVS_SAMPLE_INT32:
  281. st->codecpar->codec_id = AV_CODEC_ID_PCM_S32LE;
  282. break;
  283. case AVS_SAMPLE_FLOAT:
  284. st->codecpar->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. /* On Windows, libav supports AviSynth interface version 6 or higher.
  353. * This includes AviSynth 2.6 RC1 or higher, and AviSynth+ r1718 or higher,
  354. * and excludes 2.5 and the 2.6 alphas. Since AvxSynth identifies itself
  355. * as interface version 3 like 2.5.8, this needs to be special-cased. */
  356. if (avs_library.avs_get_version(avs->clip) < 6) {
  357. av_log(s, AV_LOG_ERROR,
  358. "AviSynth version is too old. Please upgrade to either AviSynth 2.6 >= RC1 or AviSynth+ >= r1718.\n");
  359. ret = AVERROR_UNKNOWN;
  360. goto fail;
  361. }
  362. #endif
  363. /* Release the AVS_Value as it will go out of scope. */
  364. avs_library.avs_release_value(val);
  365. if (ret = avisynth_create_stream(s))
  366. goto fail;
  367. return 0;
  368. fail:
  369. avisynth_context_destroy(avs);
  370. return ret;
  371. }
  372. static void avisynth_next_stream(AVFormatContext *s, AVStream **st,
  373. AVPacket *pkt, int *discard)
  374. {
  375. AviSynthContext *avs = s->priv_data;
  376. avs->curr_stream++;
  377. avs->curr_stream %= s->nb_streams;
  378. *st = s->streams[avs->curr_stream];
  379. if ((*st)->discard == AVDISCARD_ALL)
  380. *discard = 1;
  381. else
  382. *discard = 0;
  383. return;
  384. }
  385. /* Copy AviSynth clip data into an AVPacket. */
  386. static int avisynth_read_packet_video(AVFormatContext *s, AVPacket *pkt,
  387. int discard)
  388. {
  389. AviSynthContext *avs = s->priv_data;
  390. AVS_VideoFrame *frame;
  391. unsigned char *dst_p;
  392. const unsigned char *src_p;
  393. int n, i, plane, rowsize, planeheight, pitch, bits;
  394. const char *error;
  395. if (avs->curr_frame >= avs->vi->num_frames)
  396. return AVERROR_EOF;
  397. /* This must happen even if the stream is discarded to prevent desync. */
  398. n = avs->curr_frame++;
  399. if (discard)
  400. return 0;
  401. #ifdef USING_AVISYNTH
  402. /* avs_bits_per_pixel changed to AVSC_API with AviSynth 2.6, which
  403. * requires going through avs_library, while AvxSynth has it under
  404. * the older AVSC_INLINE type, so special-case this. */
  405. bits = avs_library.avs_bits_per_pixel(avs->vi);
  406. #else
  407. bits = avs_bits_per_pixel(avs->vi);
  408. #endif
  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. if (av_new_packet(pkt, pkt->size) < 0)
  416. return AVERROR(ENOMEM);
  417. pkt->pts = n;
  418. pkt->dts = n;
  419. pkt->duration = 1;
  420. pkt->stream_index = avs->curr_stream;
  421. frame = avs_library.avs_get_frame(avs->clip, n);
  422. error = avs_library.avs_clip_get_error(avs->clip);
  423. if (error) {
  424. av_log(s, AV_LOG_ERROR, "%s\n", error);
  425. avs->error = 1;
  426. av_packet_unref(pkt);
  427. return AVERROR_UNKNOWN;
  428. }
  429. dst_p = pkt->data;
  430. for (i = 0; i < avs->n_planes; i++) {
  431. plane = avs->planes[i];
  432. #ifdef USING_AVISYNTH
  433. src_p = avs_library.avs_get_read_ptr_p(frame, plane);
  434. pitch = avs_library.avs_get_pitch_p(frame, plane);
  435. rowsize = avs_library.avs_get_row_size_p(frame, plane);
  436. planeheight = avs_library.avs_get_height_p(frame, plane);
  437. #else
  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. #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->size = avs_bytes_per_channel_sample(avs->vi) *
  492. samples * avs->vi->nchannels;
  493. if (!pkt->size)
  494. return AVERROR_UNKNOWN;
  495. if (av_new_packet(pkt, pkt->size) < 0)
  496. return AVERROR(ENOMEM);
  497. pkt->pts = n;
  498. pkt->dts = n;
  499. pkt->duration = samples;
  500. pkt->stream_index = avs->curr_stream;
  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_packet_unref(pkt);
  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. /* 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->codecpar->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. } else {
  542. ret = avisynth_read_packet_audio(s, pkt, discard);
  543. if (ret == AVERROR_EOF && avs_has_video(avs->vi)) {
  544. avisynth_next_stream(s, &st, pkt, &discard);
  545. return avisynth_read_packet_video(s, pkt, discard);
  546. }
  547. }
  548. return ret;
  549. }
  550. static av_cold int avisynth_read_close(AVFormatContext *s)
  551. {
  552. if (avpriv_lock_avformat())
  553. return AVERROR_UNKNOWN;
  554. avisynth_context_destroy(s->priv_data);
  555. avpriv_unlock_avformat();
  556. return 0;
  557. }
  558. static int avisynth_read_seek(AVFormatContext *s, int stream_index,
  559. int64_t timestamp, int flags)
  560. {
  561. AviSynthContext *avs = s->priv_data;
  562. AVStream *st;
  563. AVRational fps, samplerate;
  564. if (avs->error)
  565. return AVERROR_UNKNOWN;
  566. fps = (AVRational) { avs->vi->fps_numerator,
  567. avs->vi->fps_denominator };
  568. samplerate = (AVRational) { avs->vi->audio_samples_per_second, 1 };
  569. st = s->streams[stream_index];
  570. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  571. /* AviSynth frame counts are signed int. */
  572. if ((timestamp >= avs->vi->num_frames) ||
  573. (timestamp > INT_MAX) ||
  574. (timestamp < 0))
  575. return AVERROR_EOF;
  576. avs->curr_frame = timestamp;
  577. if (avs_has_audio(avs->vi))
  578. avs->curr_sample = av_rescale_q(timestamp, samplerate, fps);
  579. } else {
  580. if ((timestamp >= avs->vi->num_audio_samples) || (timestamp < 0))
  581. return AVERROR_EOF;
  582. /* Force frame granularity for seeking. */
  583. if (avs_has_video(avs->vi)) {
  584. avs->curr_frame = av_rescale_q(timestamp, fps, samplerate);
  585. avs->curr_sample = av_rescale_q(avs->curr_frame, samplerate, fps);
  586. } else {
  587. avs->curr_sample = timestamp;
  588. }
  589. }
  590. return 0;
  591. }
  592. AVInputFormat ff_avisynth_demuxer = {
  593. .name = "avisynth",
  594. .long_name = NULL_IF_CONFIG_SMALL("AviSynth script"),
  595. .priv_data_size = sizeof(AviSynthContext),
  596. .read_header = avisynth_read_header,
  597. .read_packet = avisynth_read_packet,
  598. .read_close = avisynth_read_close,
  599. .read_seek = avisynth_read_seek,
  600. .extensions = "avs",
  601. };