Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 71.39 KB | None | 0 0
  1. diff --git a/ffmpeg.c b/ffmpeg.c
  2. index e0463a9..6f400cc 100644
  3. --- a/ffmpeg.c
  4. +++ b/ffmpeg.c
  5. @@ -768,11 +768,6 @@ static int read_ffserver_streams(AVFormatContext *s, const char *filename)
  6.                  choose_pixel_fmt(st, codec);
  7.          }
  8.  
  9. -        if(!st->codec->thread_count)
  10. -            st->codec->thread_count = 1;
  11. -        if(st->codec->thread_count>1)
  12. -            avcodec_thread_init(st->codec, st->codec->thread_count);
  13. -
  14.          if(st->codec->flags & CODEC_FLAG_BITEXACT)
  15.              nopts = 1;
  16.      }
  17. @@ -3433,7 +3428,7 @@ static void new_video_stream(AVFormatContext *oc, int file_idx)
  18.      bitstream_filters[file_idx][oc->nb_streams - 1]= video_bitstream_filters;
  19.      video_bitstream_filters= NULL;
  20.  
  21. -    avcodec_thread_init(st->codec, thread_count);
  22. +    st->codec->thread_count= thread_count;
  23.  
  24.      video_enc = st->codec;
  25.  
  26. @@ -3578,7 +3573,7 @@ static void new_audio_stream(AVFormatContext *oc, int file_idx)
  27.      bitstream_filters[file_idx][oc->nb_streams - 1]= audio_bitstream_filters;
  28.      audio_bitstream_filters= NULL;
  29.  
  30. -    avcodec_thread_init(st->codec, thread_count);
  31. +    st->codec->thread_count= thread_count;
  32.  
  33.      audio_enc = st->codec;
  34.      audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
  35. diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
  36. index 4bddbaa..c99e63a 100644
  37. --- a/libavcodec/avcodec.h
  38. +++ b/libavcodec/avcodec.h
  39. @@ -31,7 +31,7 @@
  40.  #include "libavutil/cpu.h"
  41.  
  42.  #define LIBAVCODEC_VERSION_MAJOR 52
  43. -#define LIBAVCODEC_VERSION_MINOR 92
  44. +#define LIBAVCODEC_VERSION_MINOR 93
  45.  #define LIBAVCODEC_VERSION_MICRO  0
  46.  
  47.  #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
  48. @@ -707,7 +707,10 @@ typedef struct RcOverride{
  49.   * Codec should fill in channel configuration and samplerate instead of container
  50.   */
  51.  #define CODEC_CAP_CHANNEL_CONF     0x0400
  52. -
  53. +/**
  54. +* Codec supports frame-level multithreading.
  55. +*/
  56. +#define CODEC_CAP_FRAME_THREADS    0x0800
  57.  
  58.  //The following defines may change, don't expect compatibility if you use them.
  59.  #define MB_TYPE_INTRA4x4   0x0001
  60. @@ -988,7 +991,20 @@ typedef struct AVPanScan{
  61.       * - decoding: Set by libavcodec\
  62.       */\
  63.      void *hwaccel_picture_private;\
  64. -
  65. +\
  66. +    /**\
  67. +     * the AVCodecContext which ff_thread_get_buffer() was last called on\
  68. +     * - encoding: Set by libavcodec.\
  69. +     * - decoding: Set by libavcodec.\
  70. +     */\
  71. +    struct AVCodecContext *owner;\
  72. +\
  73. +    /**\
  74. +     * used by multithreading to store frame-specific info\
  75. +     * - encoding: Set by libavcodec.\
  76. +     * - decoding: Set by libavcodec.\
  77. +     */\
  78. +    void *thread_opaque;
  79.  
  80.  #define FF_QSCALE_TYPE_MPEG1 0
  81.  #define FF_QSCALE_TYPE_MPEG2 1
  82. @@ -1199,7 +1215,7 @@ typedef struct AVCodecContext {
  83.       * If non NULL, 'draw_horiz_band' is called by the libavcodec
  84.       * decoder to draw a horizontal band. It improves cache usage. Not
  85.       * all codecs can do that. You must check the codec capabilities
  86. -     * beforehand.
  87. +     * beforehand. May be called by different threads at the same time.
  88.       * The function is also used by hardware acceleration APIs.
  89.       * It is called at least once during frame decoding to pass
  90.       * the data needed for hardware render.
  91. @@ -1452,7 +1468,8 @@ typedef struct AVCodecContext {
  92.       * height, as they normally need to be rounded up to the next multiple of 16.
  93.       * if CODEC_CAP_DR1 is not set then get_buffer() must call
  94.       * avcodec_default_get_buffer() instead of providing buffers allocated by
  95. -     * some other means.
  96. +     * some other means. May be called from a different thread if FF_THREAD_FRAME
  97. +     * is set, but does not need to be reentrant.
  98.       * - encoding: unused
  99.       * - decoding: Set by libavcodec, user can override.
  100.       */
  101. @@ -1461,7 +1478,9 @@ typedef struct AVCodecContext {
  102.      /**
  103.       * Called to release buffers which were allocated with get_buffer.
  104.       * A released buffer can be reused in get_buffer().
  105. -     * pic.data[*] must be set to NULL.
  106. +     * pic.data[*] must be set to NULL. May be called by different threads
  107. +     * if frame threading is enabled, but not more than one at the same time.
  108. +     *
  109.       * - encoding: unused
  110.       * - decoding: Set by libavcodec, user can override.
  111.       */
  112. @@ -1765,6 +1784,7 @@ typedef struct AVCodecContext {
  113.  #define FF_DEBUG_VIS_QP      0x00002000
  114.  #define FF_DEBUG_VIS_MB_TYPE 0x00004000
  115.  #define FF_DEBUG_BUFFERS     0x00008000
  116. +#define FF_DEBUG_THREADS     0x00010000
  117.  
  118.      /**
  119.       * debug
  120. @@ -2744,6 +2764,34 @@ typedef struct AVCodecContext {
  121.       * - decoding: unused
  122.       */
  123.      int lpc_passes;
  124. +
  125. +    /**
  126. +     * Whether this is a copy of the context which had init() called on it.
  127. +     * This is used by multithreading - shared tables and picture pointers
  128. +     * should be freed from the original context only.
  129. +     * - encoding: Set by libavcodec.
  130. +     * - decoding: Set by libavcodec.
  131. +     */
  132. +    int is_copy;
  133. +
  134. +    /**
  135. +     * Which multithreading methods to use.
  136. +     * Use of FF_THREAD_FRAME will increase decoding delay by one frame per thread,
  137. +     * so clients which require strictly conforming DTS must not use it.
  138. +     *
  139. +     * - encoding: Set by user, otherwise the default is used.
  140. +     * - decoding: Set by user, otherwise the default is used.
  141. +     */
  142. +    int thread_type;
  143. +#define FF_THREAD_FRAME   1 //< Decode more than one frame at once
  144. +#define FF_THREAD_SLICE   2 //< Decode more than one part of a single frame at once
  145. +
  146. +    /**
  147. +     * Which multithreading methods are actually active at the moment.
  148. +     * - encoding: Set by libavcodec.
  149. +     * - decoding: Set by libavcodec.
  150. +     */
  151. +    int active_thread_type;
  152.  } AVCodecContext;
  153.  
  154.  /**
  155. @@ -2786,7 +2834,28 @@ typedef struct AVCodec {
  156.      const enum SampleFormat *sample_fmts;   ///< array of supported sample formats, or NULL if unknown, array is terminated by -1
  157.      const int64_t *channel_layouts;         ///< array of support channel layouts, or NULL if unknown. array is terminated by 0
  158.      uint8_t max_lowres;                     ///< maximum value for lowres supported by the decoder
  159. +
  160.      AVClass *priv_class;                    ///< AVClass for the private context
  161. +
  162. +    /**
  163. +     * @defgroup framethreading Frame-level threading support functions.
  164. +     * @{
  165. +     */
  166. +    /**
  167. +     * If defined, called on thread contexts when they are created.
  168. +     * If the codec allocates writable tables in init(), re-allocate them here.
  169. +     * priv_data will be set to a copy of the original.
  170. +     */
  171. +    int (*init_thread_copy)(AVCodecContext *);
  172. +    /**
  173. +     * Copy necessary context variables from a previous thread context to the current one.
  174. +     * If not defined, the next thread will start automatically; otherwise, the codec
  175. +     * must call ff_thread_finish_setup().
  176. +     *
  177. +     * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
  178. +     */
  179. +    int (*update_thread_context)(AVCodecContext *dst, AVCodecContext *src);
  180. +    /** @} */
  181.  } AVCodec;
  182.  
  183.  /**
  184. diff --git a/libavcodec/w32thread.c b/libavcodec/beosthread.c
  185. similarity index 51%
  186. copy from libavcodec/w32thread.c
  187. copy to libavcodec/beosthread.c
  188. index f7a1430..8045a48 100644
  189. --- a/libavcodec/w32thread.c
  190. +++ b/libavcodec/beosthread.c
  191. @@ -1,5 +1,5 @@
  192.  /*
  193. - * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  194. + * Copyright (c) 2004 François Revol <revol@free.fr>
  195.   *
  196.   * This file is part of FFmpeg.
  197.   *
  198. @@ -21,51 +21,54 @@
  199.  
  200.  #include "avcodec.h"
  201.  
  202. -#define WIN32_LEAN_AND_MEAN
  203. -#include <windows.h>
  204. -#include <process.h>
  205. +#include <OS.h>
  206.  
  207.  typedef struct ThreadContext{
  208.      AVCodecContext *avctx;
  209. -    HANDLE thread;
  210. -    HANDLE work_sem;
  211. -    HANDLE job_sem;
  212. -    HANDLE done_sem;
  213. +    thread_id thread;
  214. +    sem_id work_sem;
  215. +    sem_id done_sem;
  216.      int (*func)(AVCodecContext *c, void *arg);
  217. -    int (*func2)(AVCodecContext *c, void *arg, int, int);
  218.      void *arg;
  219. -    int argsize;
  220. -    int *jobnr;
  221. -    int *ret;
  222. -    int threadnr;
  223. +    int ret;
  224.  }ThreadContext;
  225.  
  226. +// it's odd Be never patented that :D
  227. +struct benaphore {
  228. +        vint32 atom;
  229. +        sem_id sem;
  230. +};
  231. +static inline int lock_ben(struct benaphore *ben)
  232. +{
  233. +        if (atomic_add(&ben->atom, 1) > 0)
  234. +                return acquire_sem(ben->sem);
  235. +        return B_OK;
  236. +}
  237. +static inline int unlock_ben(struct benaphore *ben)
  238. +{
  239. +        if (atomic_add(&ben->atom, -1) > 1)
  240. +                return release_sem(ben->sem);
  241. +        return B_OK;
  242. +}
  243.  
  244. -static unsigned WINAPI attribute_align_arg thread_func(void *v){
  245. +static struct benaphore av_thread_lib_ben;
  246. +
  247. +static int32 ff_thread_func(void *v){
  248.      ThreadContext *c= v;
  249.  
  250.      for(;;){
  251. -        int ret, jobnr;
  252.  //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
  253. -        WaitForSingleObject(c->work_sem, INFINITE);
  254. -        // avoid trying to access jobnr if we should quit
  255. -        if (!c->func && !c->func2)
  256. -            break;
  257. -        WaitForSingleObject(c->job_sem, INFINITE);
  258. -        jobnr = (*c->jobnr)++;
  259. -        ReleaseSemaphore(c->job_sem, 1, 0);
  260. +        acquire_sem(c->work_sem);
  261.  //printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
  262.          if(c->func)
  263. -            ret= c->func(c->avctx, (uint8_t *)c->arg + jobnr*c->argsize);
  264. +            c->ret= c->func(c->avctx, c->arg);
  265.          else
  266. -            ret= c->func2(c->avctx, c->arg, jobnr, c->threadnr);
  267. -        if (c->ret)
  268. -            c->ret[jobnr] = ret;
  269. +            return 0;
  270.  //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
  271. -        ReleaseSemaphore(c->done_sem, 1, 0);
  272. +        release_sem(c->done_sem);
  273.      }
  274.  
  275. -    return 0;
  276. +    return B_OK;
  277.  }
  278.  
  279.  /**
  280. @@ -75,20 +78,16 @@ static unsigned WINAPI attribute_align_arg thread_func(void *v){
  281.  void avcodec_thread_free(AVCodecContext *s){
  282.      ThreadContext *c= s->thread_opaque;
  283.      int i;
  284. +    int32 ret;
  285.  
  286.      for(i=0; i<s->thread_count; i++){
  287.  
  288.          c[i].func= NULL;
  289. -        c[i].func2= NULL;
  290. +        release_sem(c[i].work_sem);
  291. +        wait_for_thread(c[i].thread, &ret);
  292. +        if(c[i].work_sem > B_OK) delete_sem(c[i].work_sem);
  293. +        if(c[i].done_sem > B_OK) delete_sem(c[i].done_sem);
  294.      }
  295. -    ReleaseSemaphore(c[0].work_sem, s->thread_count, 0);
  296. -    for(i=0; i<s->thread_count; i++){
  297. -        WaitForSingleObject(c[i].thread, INFINITE);
  298. -        if(c[i].thread)   CloseHandle(c[i].thread);
  299. -    }
  300. -    if(c[0].work_sem) CloseHandle(c[0].work_sem);
  301. -    if(c[0].job_sem)  CloseHandle(c[0].job_sem);
  302. -    if(c[0].done_sem) CloseHandle(c[0].done_sem);
  303.  
  304.      av_freep(&s->thread_opaque);
  305.  }
  306. @@ -96,40 +95,39 @@ void avcodec_thread_free(AVCodecContext *s){
  307.  static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  308.      ThreadContext *c= s->thread_opaque;
  309.      int i;
  310. -    int jobnr = 0;
  311.  
  312.      assert(s == c->avctx);
  313. +    assert(count <= s->thread_count);
  314.  
  315.      /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
  316.  
  317. -    for(i=0; i<s->thread_count; i++){
  318. -        c[i].arg= arg;
  319. -        c[i].argsize= size;
  320. +    for(i=0; i<count; i++){
  321. +        c[i].arg= (char*)arg + i*size;
  322.          c[i].func= func;
  323. -        c[i].ret= ret;
  324. -        c[i].jobnr = &jobnr;
  325. +        c[i].ret= 12345;
  326. +
  327. +        release_sem(c[i].work_sem);
  328.      }
  329. -    ReleaseSemaphore(c[0].work_sem, count, 0);
  330. -    for(i=0; i<count; i++)
  331. -        WaitForSingleObject(c[0].done_sem, INFINITE);
  332. +    for(i=0; i<count; i++){
  333. +        acquire_sem(c[i].done_sem);
  334.  
  335. +        c[i].func= NULL;
  336. +        if(ret) ret[i]= c[i].ret;
  337. +    }
  338.      return 0;
  339.  }
  340.  
  341. -static int avcodec_thread_execute2(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count){
  342. -    ThreadContext *c= s->thread_opaque;
  343. -    int i;
  344. -    for(i=0; i<s->thread_count; i++)
  345. -        c[i].func2 = func;
  346. -    avcodec_thread_execute(s, NULL, arg, ret, count, 0);
  347. -}
  348. -
  349.  int avcodec_thread_init(AVCodecContext *s, int thread_count){
  350.      int i;
  351.      ThreadContext *c;
  352. -    uint32_t threadid;
  353. +
  354. +    if(!(s->thread_type & FF_THREAD_SLICE)){
  355. +        av_log(s, AV_LOG_WARNING, "The requested thread algorithm is not supported with this thread library.\n");
  356. +        return 0;
  357. +    }
  358.  
  359.      s->thread_count= thread_count;
  360. +    s->active_thread_type= FF_THREAD_SLICE;
  361.  
  362.      if (thread_count <= 1)
  363.          return 0;
  364. @@ -137,32 +135,55 @@ int avcodec_thread_init(AVCodecContext *s, int thread_count){
  365.      assert(!s->thread_opaque);
  366.      c= av_mallocz(sizeof(ThreadContext)*thread_count);
  367.      s->thread_opaque= c;
  368. -    if(!(c[0].work_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
  369. -        goto fail;
  370. -    if(!(c[0].job_sem  = CreateSemaphore(NULL, 1, 1, NULL)))
  371. -        goto fail;
  372. -    if(!(c[0].done_sem = CreateSemaphore(NULL, 0, INT_MAX, NULL)))
  373. -        goto fail;
  374.  
  375.      for(i=0; i<thread_count; i++){
  376.  //printf("init semaphors %d\n", i); fflush(stdout);
  377.          c[i].avctx= s;
  378. -        c[i].work_sem = c[0].work_sem;
  379. -        c[i].job_sem  = c[0].job_sem;
  380. -        c[i].done_sem = c[0].done_sem;
  381. -        c[i].threadnr = i;
  382. +
  383. +        if((c[i].work_sem = create_sem(0, "ff work sem")) < B_OK)
  384. +            goto fail;
  385. +        if((c[i].done_sem = create_sem(0, "ff done sem")) < B_OK)
  386. +            goto fail;
  387.  
  388.  //printf("create thread %d\n", i); fflush(stdout);
  389. -        c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
  390. -        if( !c[i].thread ) goto fail;
  391. +        c[i].thread = spawn_thread(ff_thread_func, "libavcodec thread", B_LOW_PRIORITY, &c[i] );
  392. +        if( c[i].thread < B_OK ) goto fail;
  393. +        resume_thread(c[i].thread );
  394.      }
  395.  //printf("init done\n"); fflush(stdout);
  396.  
  397.      s->execute= avcodec_thread_execute;
  398. -    s->execute2= avcodec_thread_execute2;
  399.  
  400.      return 0;
  401.  fail:
  402.      avcodec_thread_free(s);
  403.      return -1;
  404.  }
  405. +
  406. +/* provide a mean to serialize calls to avcodec_*() for thread safety. */
  407. +
  408. +int avcodec_thread_lock_lib(void)
  409. +{
  410. +        return lock_ben(&av_thread_lib_ben);
  411. +}
  412. +
  413. +int avcodec_thread_unlock_lib(void)
  414. +{
  415. +        return unlock_ben(&av_thread_lib_ben);
  416. +}
  417. +
  418. +/* our versions of _init and _fini (which are called by those actually from crt.o) */
  419. +
  420. +void initialize_after(void)
  421. +{
  422. +        av_thread_lib_ben.atom = 0;
  423. +        av_thread_lib_ben.sem = create_sem(0, "libavcodec benaphore");
  424. +}
  425. +
  426. +void uninitialize_before(void)
  427. +{
  428. +        delete_sem(av_thread_lib_ben.sem);
  429. +}
  430. +
  431. +
  432. +
  433. diff --git a/libavcodec/dsputil.c b/libavcodec/dsputil.c
  434. index 29ddb4d..dd14eb4 100644
  435. --- a/libavcodec/dsputil.c
  436. +++ b/libavcodec/dsputil.c
  437. @@ -294,7 +294,7 @@ static int sse16_c(void *v, uint8_t *pix1, uint8_t *pix2, int line_size, int h)
  438.  
  439.  /* draw the edges of width 'w' of an image of size width, height */
  440.  //FIXME check that this is ok for mpeg4 interlaced
  441. -static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
  442. +static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w, int sides)
  443.  {
  444.      uint8_t *ptr, *last_line;
  445.      int i;
  446. @@ -302,8 +302,8 @@ static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
  447.      last_line = buf + (height - 1) * wrap;
  448.      for(i=0;i<w;i++) {
  449.          /* top and bottom */
  450. -        memcpy(buf - (i + 1) * wrap, buf, width);
  451. -        memcpy(last_line + (i + 1) * wrap, last_line, width);
  452. +        if (sides&EDGE_TOP)    memcpy(buf - (i + 1) * wrap, buf, width);
  453. +        if (sides&EDGE_BOTTOM) memcpy(last_line + (i + 1) * wrap, last_line, width);
  454.      }
  455.      /* left and right */
  456.      ptr = buf;
  457. @@ -314,10 +314,15 @@ static void draw_edges_c(uint8_t *buf, int wrap, int width, int height, int w)
  458.      }
  459.      /* corners */
  460.      for(i=0;i<w;i++) {
  461. -        memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  462. -        memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  463. -        memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  464. -        memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  465. +        if (sides&EDGE_TOP) {
  466. +            memset(buf - (i + 1) * wrap - w, buf[0], w); /* top left */
  467. +            memset(buf - (i + 1) * wrap + width, buf[width-1], w); /* top right */
  468. +        }
  469. +
  470. +        if (sides&EDGE_BOTTOM) {
  471. +            memset(last_line + (i + 1) * wrap - w, last_line[0], w); /* top left */
  472. +            memset(last_line + (i + 1) * wrap + width, last_line[width-1], w); /* top right */
  473. +        }
  474.      }
  475.  }
  476.  
  477. diff --git a/libavcodec/dsputil.h b/libavcodec/dsputil.h
  478. index 6c56a65..55d7c2d 100644
  479. --- a/libavcodec/dsputil.h
  480. +++ b/libavcodec/dsputil.h
  481. @@ -484,8 +484,10 @@ typedef struct DSPContext {
  482.  #define BASIS_SHIFT 16
  483.  #define RECON_SHIFT 6
  484.  
  485. -    void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w);
  486. +    void (*draw_edges)(uint8_t *buf, int wrap, int width, int height, int w, int sides);
  487.  #define EDGE_WIDTH 16
  488. +#define EDGE_TOP    1
  489. +#define EDGE_BOTTOM 2
  490.  
  491.      void (*prefetch)(void *mem, int stride, int h);
  492.  
  493. diff --git a/libavcodec/options.c b/libavcodec/options.c
  494. index 6969d42..d41d8ce 100644
  495. --- a/libavcodec/options.c
  496. +++ b/libavcodec/options.c
  497. @@ -250,6 +250,7 @@ static const AVOption options[]={
  498.  {"vis_qp", "visualize quantization parameter (QP), lower QP are tinted greener", 0, FF_OPT_TYPE_CONST, FF_DEBUG_VIS_QP, INT_MIN, INT_MAX, V|D, "debug"},
  499.  {"vis_mb_type", "visualize block types", 0, FF_OPT_TYPE_CONST, FF_DEBUG_VIS_MB_TYPE, INT_MIN, INT_MAX, V|D, "debug"},
  500.  {"buffers", "picture buffer allocations", 0, FF_OPT_TYPE_CONST, FF_DEBUG_BUFFERS, INT_MIN, INT_MAX, V|D, "debug"},
  501. +{"thread_ops", "threading operations", 0, FF_OPT_TYPE_CONST, FF_DEBUG_THREADS, INT_MIN, INT_MAX, V|D, "debug"},
  502.  {"vismv", "visualize motion vectors (MVs)", OFFSET(debug_mv), FF_OPT_TYPE_INT, DEFAULT, 0, INT_MAX, V|D, "debug_mv"},
  503.  {"pf", "forward predicted MVs of P-frames", 0, FF_OPT_TYPE_CONST, FF_DEBUG_VIS_MV_P_FOR, INT_MIN, INT_MAX, V|D, "debug_mv"},
  504.  {"bf", "forward predicted MVs of B-frames", 0, FF_OPT_TYPE_CONST, FF_DEBUG_VIS_MV_B_FOR, INT_MIN, INT_MAX, V|D, "debug_mv"},
  505. @@ -425,6 +426,9 @@ static const AVOption options[]={
  506.  {"levinson", NULL, 0, FF_OPT_TYPE_CONST, AV_LPC_TYPE_LEVINSON, INT_MIN, INT_MAX, A|E, "lpc_type"},
  507.  {"cholesky", NULL, 0, FF_OPT_TYPE_CONST, AV_LPC_TYPE_CHOLESKY, INT_MIN, INT_MAX, A|E, "lpc_type"},
  508.  {"lpc_passes", "number of passes to use for Cholesky factorization during LPC analysis", OFFSET(lpc_passes), FF_OPT_TYPE_INT, -1, INT_MIN, INT_MAX, A|E},
  509. +{"thread_type", "select multithreading type", OFFSET(thread_type), FF_OPT_TYPE_INT, FF_THREAD_SLICE|FF_THREAD_FRAME, 0, INT_MAX, V|E|D, "thread_type"},
  510. +{"slice", NULL, 0, FF_OPT_TYPE_CONST, FF_THREAD_SLICE, INT_MIN, INT_MAX, V|E|D, "thread_type"},
  511. +{"frame", NULL, 0, FF_OPT_TYPE_CONST, FF_THREAD_FRAME, INT_MIN, INT_MAX, V|E|D, "thread_type"},
  512.  {NULL},
  513.  };
  514.  
  515. diff --git a/libavcodec/os2thread.c b/libavcodec/os2thread.c
  516. new file mode 100644
  517. index 0000000..9f0cd41
  518. --- /dev/null
  519. +++ b/libavcodec/os2thread.c
  520. @@ -0,0 +1,154 @@
  521. +/*
  522. + * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
  523. + *
  524. + * This file is part of FFmpeg.
  525. + *
  526. + * FFmpeg is free software; you can redistribute it and/or
  527. + * modify it under the terms of the GNU Lesser General Public
  528. + * License as published by the Free Software Foundation; either
  529. + * version 2.1 of the License, or (at your option) any later version.
  530. + *
  531. + * FFmpeg is distributed in the hope that it will be useful,
  532. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  533. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  534. + * Lesser General Public License for more details.
  535. + *
  536. + * You should have received a copy of the GNU Lesser General Public
  537. + * License along with FFmpeg; if not, write to the Free Software
  538. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  539. + */
  540. +//#define DEBUG
  541. +
  542. +// Ported by Vlad Stelmahovsky
  543. +
  544. +#include "avcodec.h"
  545. +
  546. +#define INCL_DOS
  547. +#define INCL_DOSERRORS
  548. +#define INCL_DOSDEVIOCTL
  549. +#include <os2.h>
  550. +
  551. +typedef struct ThreadContext{
  552. +    AVCodecContext *avctx;
  553. +    int thread;
  554. +    HEV work_sem;
  555. +    HEV done_sem;
  556. +    int (*func)(AVCodecContext *c, void *arg);
  557. +    void *arg;
  558. +    int ret;
  559. +}ThreadContext;
  560. +
  561. +
  562. +static void attribute_align_arg thread_func(void *v){
  563. +    ThreadContext *c= v;
  564. +
  565. +    for(;;){
  566. +        //printf("thread_func %X enter wait\n", (int)v); fflush(stdout);
  567. +        DosWaitEventSem(c->work_sem, SEM_INDEFINITE_WAIT);
  568. +//        WaitForSingleObject(c->work_sem, INFINITE);
  569. +//printf("thread_func %X after wait (func=%X)\n", (int)v, (int)c->func); fflush(stdout);
  570. +        if(c->func)
  571. +            c->ret= c->func(c->avctx, c->arg);
  572. +        else
  573. +            return;
  574. +        //printf("thread_func %X signal complete\n", (int)v); fflush(stdout);
  575. +        DosPostEventSem(c->done_sem);
  576. +//        ReleaseSemaphore(c->done_sem, 1, 0);
  577. +    }
  578. +
  579. +    return;
  580. +}
  581. +
  582. +/**
  583. + * free what has been allocated by avcodec_thread_init().
  584. + * must be called after decoding has finished, especially do not call while avcodec_thread_execute() is running
  585. + */
  586. +void avcodec_thread_free(AVCodecContext *s){
  587. +    ThreadContext *c= s->thread_opaque;
  588. +    int i;
  589. +
  590. +    for(i=0; i<s->thread_count; i++){
  591. +
  592. +        c[i].func= NULL;
  593. +        DosPostEventSem(c[i].work_sem);
  594. +        //        ReleaseSemaphore(c[i].work_sem, 1, 0);
  595. +        DosWaitThread((PTID)&c[i].thread,DCWW_WAIT);
  596. +//        WaitForSingleObject(c[i].thread, INFINITE);
  597. +        if(c[i].work_sem) DosCloseEventSem(c[i].work_sem);//CloseHandle(c[i].work_sem);
  598. +        if(c[i].done_sem) DosCloseEventSem(c[i].done_sem);//CloseHandle(c[i].done_sem);
  599. +    }
  600. +
  601. +    av_freep(&s->thread_opaque);
  602. +}
  603. +
  604. +static int avcodec_thread_execute(AVCodecContext *s, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  605. +    ThreadContext *c= s->thread_opaque;
  606. +    int i;
  607. +
  608. +    assert(s == c->avctx);
  609. +    assert(count <= s->thread_count);
  610. +
  611. +    /* note, we can be certain that this is not called with the same AVCodecContext by different threads at the same time */
  612. +
  613. +    for(i=0; i<count; i++){
  614. +
  615. +        c[i].arg= (char*)arg + i*size;
  616. +        c[i].func= func;
  617. +        c[i].ret= 12345;
  618. +
  619. +        DosPostEventSem(c[i].work_sem);
  620. +//        ReleaseSemaphore(c[i].work_sem, 1, 0);
  621. +    }
  622. +    for(i=0; i<count; i++){
  623. +        DosWaitEventSem(c[i].done_sem,SEM_INDEFINITE_WAIT);
  624. +//        WaitForSingleObject(c[i].done_sem, INFINITE);
  625. +
  626. +        c[i].func= NULL;
  627. +        if(ret) ret[i]= c[i].ret;
  628. +    }
  629. +    return 0;
  630. +}
  631. +
  632. +int avcodec_thread_init(AVCodecContext *s, int thread_count){
  633. +    int i;
  634. +    ThreadContext *c;
  635. +    uint32_t threadid;
  636. +
  637. +    if(!(s->thread_type & FF_THREAD_SLICE)){
  638. +        av_log(s, AV_LOG_WARNING, "The requested thread algorithm is not supported with this thread library.\n");
  639. +        return 0;
  640. +    }
  641. +
  642. +    s->thread_count= thread_count;
  643. +    s->active_thread_type= FF_THREAD_SLICE;
  644. +
  645. +    if (thread_count <= 1)
  646. +        return 0;
  647. +
  648. +    assert(!s->thread_opaque);
  649. +    c= av_mallocz(sizeof(ThreadContext)*thread_count);
  650. +    s->thread_opaque= c;
  651. +
  652. +    for(i=0; i<thread_count; i++){
  653. +//printf("init semaphors %d\n", i); fflush(stdout);
  654. +        c[i].avctx= s;
  655. +
  656. +        if (DosCreateEventSem(NULL,&c[i].work_sem,DC_SEM_SHARED,0))
  657. +            goto fail;
  658. +        if (DosCreateEventSem(NULL,&c[i].done_sem,DC_SEM_SHARED,0))
  659. +            goto fail;
  660. +
  661. +//printf("create thread %d\n", i); fflush(stdout);
  662. +//        c[i].thread = (HANDLE)_beginthreadex(NULL, 0, thread_func, &c[i], 0, &threadid );
  663. +        c[i].thread = _beginthread(thread_func, NULL, 0x10000, &c[i]);
  664. +        if( c[i].thread <= 0 ) goto fail;
  665. +    }
  666. +//printf("init done\n"); fflush(stdout);
  667. +
  668. +    s->execute= avcodec_thread_execute;
  669. +
  670. +    return 0;
  671. +fail:
  672. +    avcodec_thread_free(s);
  673. +    return -1;
  674. +}
  675. diff --git a/libavcodec/pthread.c b/libavcodec/pthread.c
  676. index 1628b21..d2b0c3f 100644
  677. --- a/libavcodec/pthread.c
  678. +++ b/libavcodec/pthread.c
  679. @@ -1,5 +1,6 @@
  680.  /*
  681.   * Copyright (c) 2004 Roman Shaposhnik
  682. + * Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com)
  683.   *
  684.   * Many thanks to Steven M. Schultz for providing clever ideas and
  685.   * to Michael Niedermayer <michaelni@gmx.at> for writing initial
  686. @@ -24,6 +25,10 @@
  687.  #include <pthread.h>
  688.  
  689.  #include "avcodec.h"
  690. +#include "thread.h"
  691. +
  692. +/// Max number of frame buffers that can be allocated when using frame threads.
  693. +#define MAX_BUFFERS 32
  694.  
  695.  typedef int (action_func)(AVCodecContext *c, void *arg);
  696.  typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
  697. @@ -45,6 +50,66 @@ typedef struct ThreadContext {
  698.      int done;
  699.  } ThreadContext;
  700.  
  701. +typedef struct PerThreadContext {
  702. +    struct FrameThreadContext *parent;
  703. +
  704. +    pthread_t      thread;
  705. +    pthread_cond_t input_cond;      ///< Used to wait for a new frame from the main thread.
  706. +    pthread_cond_t progress_cond;   ///< Used by child threads to wait for decoding/encoding progress.
  707. +    pthread_cond_t output_cond;     ///< Used by the main thread to wait for frames to finish.
  708. +
  709. +    pthread_mutex_t mutex;          ///< Mutex used to protect the contents of the PerThreadContext.
  710. +    pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
  711. +
  712. +    AVCodecContext *avctx;          ///< Context used to decode frames passed to this thread.
  713. +
  714. +    AVPacket       avpkt;           ///< Input frame (for decoding) or output (for encoding).
  715. +    int            allocated_buf_size;
  716. +
  717. +    AVFrame picture;                ///< Output frame (for decoding) or input (for encoding).
  718. +    int     got_picture;            ///< The output of got_picture_ptr from the last avcodec_decode_video() call (for decoding).
  719. +    int     result;                 ///< The result of the last codec decode/encode() call.
  720. +
  721. +    enum {
  722. +        STATE_INPUT_READY,          ///< Set when the thread is sleeping.
  723. +        STATE_SETTING_UP,           ///< Set before the codec has called ff_thread_finish_setup().
  724. +        STATE_SETUP_FINISHED        /**<
  725. +                                     * Set after the codec has called ff_thread_finish_setup().
  726. +                                     * At this point it is safe to start the next thread.
  727. +                                     */
  728. +    } state;
  729. +
  730. +    /**
  731. +     * Array of frames passed to ff_thread_release_buffer(),
  732. +     * to be released later.
  733. +     */
  734. +    AVFrame released_buffers[MAX_BUFFERS];
  735. +    int     num_released_buffers;
  736. +
  737. +    /**
  738. +     * Array of progress values for ff_thread_get_buffer().
  739. +     */
  740. +    int     progress[MAX_BUFFERS][2];
  741. +    uint8_t used_progress[MAX_BUFFERS];
  742. +} PerThreadContext;
  743. +
  744. +typedef struct FrameThreadContext {
  745. +    PerThreadContext *threads;     ///< The contexts for frame decoding threads.
  746. +    PerThreadContext *prev_thread; ///< The last thread submit_frame() was called on.
  747. +
  748. +    pthread_mutex_t buffer_mutex;  ///< Mutex used to protect get/release_buffer().
  749. +
  750. +    int next_decoding;             ///< The next context to submit frames to.
  751. +    int next_finished;             ///< The next context to return output from.
  752. +
  753. +    int delaying;                  /**
  754. +                                    * Set for the first N frames, where N is the number of threads.
  755. +                                    * While it is set, ff_en/decode_frame_threaded won't return any results.
  756. +                                    */
  757. +
  758. +    int die;                       ///< Set to cause threads to exit.
  759. +} FrameThreadContext;
  760. +
  761.  static void* attribute_align_arg worker(void *v)
  762.  {
  763.      AVCodecContext *avctx = v;
  764. @@ -84,7 +149,7 @@ static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int t
  765.      pthread_mutex_unlock(&c->current_job_lock);
  766.  }
  767.  
  768. -void avcodec_thread_free(AVCodecContext *avctx)
  769. +static void thread_free(AVCodecContext *avctx)
  770.  {
  771.      ThreadContext *c = avctx->thread_opaque;
  772.      int i;
  773. @@ -109,6 +174,9 @@ static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void
  774.      ThreadContext *c= avctx->thread_opaque;
  775.      int dummy_ret;
  776.  
  777. +    if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
  778. +        return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
  779. +
  780.      if (job_count <= 0)
  781.          return 0;
  782.  
  783. @@ -140,10 +208,11 @@ static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, v
  784.      return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
  785.  }
  786.  
  787. -int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
  788. +static int thread_init(AVCodecContext *avctx)
  789.  {
  790.      int i;
  791.      ThreadContext *c;
  792. +    int thread_count = avctx->thread_count;
  793.  
  794.      avctx->thread_count = thread_count;
  795.  
  796. @@ -184,3 +253,549 @@ int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
  797.      avctx->execute2 = avcodec_thread_execute2;
  798.      return 0;
  799.  }
  800. +
  801. +/**
  802. + * Read and decode frames from the main thread until fctx->die is set.
  803. + * ff_thread_finish_setup() is called before decoding if the codec
  804. + * doesn't define update_thread_context(), and afterwards if the codec errors
  805. + * before calling it.
  806. + */
  807. +static attribute_align_arg void *frame_worker_thread(void *arg)
  808. +{
  809. +    PerThreadContext *p = arg;
  810. +    FrameThreadContext *fctx = p->parent;
  811. +    AVCodecContext *avctx = p->avctx;
  812. +    AVCodec *codec = avctx->codec;
  813. +
  814. +    while (1) {
  815. +        if (p->state == STATE_INPUT_READY && !fctx->die) {
  816. +            pthread_mutex_lock(&p->mutex);
  817. +            while (p->state == STATE_INPUT_READY && !fctx->die)
  818. +                pthread_cond_wait(&p->input_cond, &p->mutex);
  819. +            pthread_mutex_unlock(&p->mutex);
  820. +        }
  821. +
  822. +        if (fctx->die) break;
  823. +
  824. +        if (!codec->update_thread_context) ff_thread_finish_setup(avctx);
  825. +
  826. +        pthread_mutex_lock(&p->mutex);
  827. +        p->result = codec->decode(avctx, &p->picture, &p->got_picture, &p->avpkt);
  828. +
  829. +        if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
  830. +
  831. +        p->state = STATE_INPUT_READY;
  832. +
  833. +        pthread_mutex_lock(&p->progress_mutex);
  834. +        pthread_cond_signal(&p->output_cond);
  835. +        pthread_mutex_unlock(&p->progress_mutex);
  836. +        pthread_mutex_unlock(&p->mutex);
  837. +    };
  838. +
  839. +    return NULL;
  840. +}
  841. +
  842. +/**
  843. + * Update a thread's context from the last thread. This is used for returning
  844. + * frames and for starting new decoding jobs after the previous one finishes
  845. + * predecoding.
  846. + *
  847. + * @param dst The destination context.
  848. + * @param src The source context.
  849. + * @param for_user Whether or not dst is the user-visible context. update_thread_context won't be called and some pointers will be copied.
  850. + */
  851. +static int update_thread_context_from_copy(AVCodecContext *dst, AVCodecContext *src, int for_user)
  852. +{
  853. +    int err = 0;
  854. +#define COPY(f) dst->f = src->f;
  855. +#define COPY_FIELDS(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
  856. +
  857. +    //coded_width/height are not copied here, so that codecs' update_thread_context can see when they change
  858. +    //many encoding parameters could be theoretically changed during encode, but aren't copied ATM
  859. +
  860. +    if (dst != src) {
  861. +        COPY(sub_id);
  862. +        COPY(time_base);
  863. +        COPY(width);
  864. +        COPY(height);
  865. +        COPY(pix_fmt);
  866. +        COPY(real_pict_num); //necessary?
  867. +        COPY(delay);
  868. +        COPY(max_b_frames);
  869. +
  870. +        COPY_FIELDS(mv_bits, opaque);
  871. +
  872. +        COPY(has_b_frames);
  873. +        COPY(bits_per_coded_sample);
  874. +        COPY(sample_aspect_ratio);
  875. +        COPY(idct_algo);
  876. +        memcpy(dst->error, src->error, sizeof(src->error));
  877. +        COPY(last_predictor_count); //necessary?
  878. +        COPY(dtg_active_format);
  879. +        COPY(color_table_id);
  880. +        COPY(profile);
  881. +        COPY(level);
  882. +        COPY(bits_per_raw_sample);
  883. +        COPY(ticks_per_frame);
  884. +        COPY(color_primaries);
  885. +        COPY(color_trc);
  886. +        COPY(colorspace);
  887. +        COPY(color_range);
  888. +    }
  889. +
  890. +    if (for_user) {
  891. +        COPY(coded_frame);
  892. +        dst->has_b_frames += src->thread_count - 1;
  893. +    } else {
  894. +        if (dst->codec->update_thread_context)
  895. +            err = dst->codec->update_thread_context(dst, src);
  896. +    }
  897. +
  898. +    return err;
  899. +}
  900. +
  901. +///Update the next decoding thread with values set by the user
  902. +static void update_thread_context_from_user(AVCodecContext *dst, AVCodecContext *src)
  903. +{
  904. +    COPY(get_buffer);
  905. +    COPY(release_buffer);
  906. +    COPY(opaque);
  907. +    COPY(hurry_up);
  908. +    COPY_FIELDS(skip_loop_filter, bidir_refine);
  909. +    COPY(frame_number);
  910. +    COPY(reordered_opaque);
  911. +}
  912. +
  913. +static void free_progress(AVFrame *f)
  914. +{
  915. +    PerThreadContext *p = f->owner->thread_opaque;
  916. +    int *progress = f->thread_opaque;
  917. +
  918. +    p->used_progress[(progress - p->progress[0]) / 2] = 0;
  919. +}
  920. +
  921. +/// Release all frames passed to ff_thread_release_buffer()
  922. +static void handle_delayed_releases(PerThreadContext *p)
  923. +{
  924. +    FrameThreadContext *fctx = p->parent;
  925. +
  926. +    while (p->num_released_buffers > 0) {
  927. +        AVFrame *f = &p->released_buffers[--p->num_released_buffers];
  928. +
  929. +        pthread_mutex_lock(&fctx->buffer_mutex);
  930. +        free_progress(f);
  931. +        f->thread_opaque = NULL;
  932. +
  933. +        f->owner->release_buffer(f->owner, f);
  934. +        pthread_mutex_unlock(&fctx->buffer_mutex);
  935. +    }
  936. +}
  937. +
  938. +/// Submit a frame to the next decoding thread
  939. +static int submit_frame(PerThreadContext *p, AVPacket *avpkt)
  940. +{
  941. +    FrameThreadContext *fctx = p->parent;
  942. +    PerThreadContext *prev_thread = fctx->prev_thread;
  943. +    AVCodec *codec = p->avctx->codec;
  944. +    uint8_t *buf = p->avpkt.data;
  945. +    int err = 0;
  946. +
  947. +    if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
  948. +
  949. +    pthread_mutex_lock(&p->mutex);
  950. +
  951. +    handle_delayed_releases(p);
  952. +
  953. +    if (prev_thread) {
  954. +        if (prev_thread->state == STATE_SETTING_UP) {
  955. +            pthread_mutex_lock(&prev_thread->progress_mutex);
  956. +            while (prev_thread->state == STATE_SETTING_UP)
  957. +                pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
  958. +            pthread_mutex_unlock(&prev_thread->progress_mutex);
  959. +        }
  960. +
  961. +        err = update_thread_context_from_copy(p->avctx, prev_thread->avctx, 0);
  962. +        if (err) return err;
  963. +    }
  964. +
  965. +    av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
  966. +    p->avpkt = *avpkt;
  967. +    p->avpkt.data = buf;
  968. +    memcpy(buf, avpkt->data, avpkt->size);
  969. +    memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  970. +
  971. +    p->state = STATE_SETTING_UP;
  972. +    pthread_cond_signal(&p->input_cond);
  973. +    pthread_mutex_unlock(&p->mutex);
  974. +
  975. +    fctx->prev_thread = p;
  976. +
  977. +    return err;
  978. +}
  979. +
  980. +int ff_thread_decode_frame(AVCodecContext *avctx,
  981. +                             void *data, int *data_size,
  982. +                             AVPacket *avpkt)
  983. +{
  984. +    FrameThreadContext *fctx = avctx->thread_opaque;
  985. +    int thread_count = avctx->thread_count, err = 0;
  986. +    int returning_thread = fctx->next_finished;
  987. +    PerThreadContext *p;
  988. +
  989. +    p = &fctx->threads[fctx->next_decoding];
  990. +    update_thread_context_from_user(p->avctx, avctx);
  991. +    err = submit_frame(p, avpkt);
  992. +    if (err) return err;
  993. +
  994. +    fctx->next_decoding++;
  995. +
  996. +    if (fctx->delaying && avpkt->size) {
  997. +        if (fctx->next_decoding >= (thread_count-1)) fctx->delaying = 0;
  998. +
  999. +        *data_size=0;
  1000. +        return 0;
  1001. +    }
  1002. +
  1003. +    //If it's draining frames at EOF, ignore null frames from the codec.
  1004. +    //Only return one when we've run out of codec frames to return.
  1005. +    do {
  1006. +        p = &fctx->threads[returning_thread++];
  1007. +
  1008. +        if (p->state != STATE_INPUT_READY) {
  1009. +            pthread_mutex_lock(&p->progress_mutex);
  1010. +            while (p->state != STATE_INPUT_READY)
  1011. +                pthread_cond_wait(&p->output_cond, &p->progress_mutex);
  1012. +            pthread_mutex_unlock(&p->progress_mutex);
  1013. +        }
  1014. +
  1015. +        *(AVFrame*)data = p->picture;
  1016. +        *data_size = p->got_picture;
  1017. +
  1018. +        avcodec_get_frame_defaults(&p->picture);
  1019. +        p->got_picture = 0;
  1020. +
  1021. +        if (returning_thread >= thread_count) returning_thread = 0;
  1022. +    } while (!avpkt->size && !*data_size && returning_thread != fctx->next_finished);
  1023. +
  1024. +    update_thread_context_from_copy(avctx, p->avctx, 1);
  1025. +
  1026. +    if (fctx->next_decoding >= thread_count) fctx->next_decoding = 0;
  1027. +    fctx->next_finished = returning_thread;
  1028. +
  1029. +    return p->result;
  1030. +}
  1031. +
  1032. +void ff_thread_report_progress(AVFrame *f, int n, int field)
  1033. +{
  1034. +    PerThreadContext *p;
  1035. +    int *progress = f->thread_opaque;
  1036. +
  1037. +    if (!progress || progress[field] >= n) return;
  1038. +
  1039. +    p = f->owner->thread_opaque;
  1040. +
  1041. +    if (f->owner->debug&FF_DEBUG_THREADS)
  1042. +        av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
  1043. +
  1044. +    pthread_mutex_lock(&p->progress_mutex);
  1045. +    progress[field] = n;
  1046. +    pthread_cond_broadcast(&p->progress_cond);
  1047. +    pthread_mutex_unlock(&p->progress_mutex);
  1048. +}
  1049. +
  1050. +void ff_thread_await_progress(AVFrame *f, int n, int field)
  1051. +{
  1052. +    PerThreadContext *p;
  1053. +    int *progress = f->thread_opaque;
  1054. +
  1055. +    if (!progress || progress[field] >= n) return;
  1056. +
  1057. +    p = f->owner->thread_opaque;
  1058. +
  1059. +    if (f->owner->debug&FF_DEBUG_THREADS)
  1060. +        av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
  1061. +
  1062. +    pthread_mutex_lock(&p->progress_mutex);
  1063. +    while (progress[field] < n)
  1064. +        pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
  1065. +    pthread_mutex_unlock(&p->progress_mutex);
  1066. +}
  1067. +
  1068. +void ff_thread_finish_frame(AVFrame *f)
  1069. +{
  1070. +    ff_thread_report_progress(f, INT_MAX, 0);
  1071. +    ff_thread_report_progress(f, INT_MAX, 1);
  1072. +}
  1073. +
  1074. +void ff_thread_finish_setup(AVCodecContext *avctx) {
  1075. +    PerThreadContext *p = avctx->thread_opaque;
  1076. +
  1077. +    if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
  1078. +
  1079. +    pthread_mutex_lock(&p->progress_mutex);
  1080. +    p->state = STATE_SETUP_FINISHED;
  1081. +    pthread_cond_broadcast(&p->progress_cond);
  1082. +    pthread_mutex_unlock(&p->progress_mutex);
  1083. +}
  1084. +
  1085. +/// Wait for all threads to finish decoding
  1086. +static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
  1087. +{
  1088. +    int i;
  1089. +
  1090. +    for (i = 0; i < thread_count; i++) {
  1091. +        PerThreadContext *p = &fctx->threads[i];
  1092. +
  1093. +        if (p->state != STATE_INPUT_READY) {
  1094. +            pthread_mutex_lock(&p->progress_mutex);
  1095. +            while (p->state != STATE_INPUT_READY)
  1096. +                pthread_cond_wait(&p->output_cond, &p->progress_mutex);
  1097. +            pthread_mutex_unlock(&p->progress_mutex);
  1098. +        }
  1099. +    }
  1100. +}
  1101. +
  1102. +static void frame_thread_free(AVCodecContext *avctx, int thread_count)
  1103. +{
  1104. +    FrameThreadContext *fctx = avctx->thread_opaque;
  1105. +    AVCodec *codec = avctx->codec;
  1106. +    int i;
  1107. +
  1108. +    park_frame_worker_threads(fctx, thread_count);
  1109. +
  1110. +    if (fctx->prev_thread)
  1111. +        update_thread_context_from_copy(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
  1112. +
  1113. +    fctx->die = 1;
  1114. +
  1115. +    for (i = 0; i < thread_count; i++) {
  1116. +        PerThreadContext *p = &fctx->threads[i];
  1117. +
  1118. +        pthread_mutex_lock(&p->mutex);
  1119. +        pthread_cond_signal(&p->input_cond);
  1120. +        pthread_mutex_unlock(&p->mutex);
  1121. +
  1122. +        pthread_join(p->thread, NULL);
  1123. +
  1124. +        if (codec->close)
  1125. +            codec->close(p->avctx);
  1126. +
  1127. +        handle_delayed_releases(p);
  1128. +    }
  1129. +
  1130. +    for (i = 0; i < thread_count; i++) {
  1131. +        PerThreadContext *p = &fctx->threads[i];
  1132. +
  1133. +        avcodec_default_free_buffers(p->avctx);
  1134. +
  1135. +        pthread_mutex_destroy(&p->mutex);
  1136. +        pthread_mutex_destroy(&p->progress_mutex);
  1137. +        pthread_cond_destroy(&p->input_cond);
  1138. +        pthread_cond_destroy(&p->progress_cond);
  1139. +        pthread_cond_destroy(&p->output_cond);
  1140. +        av_freep(&p->avpkt.data);
  1141. +
  1142. +        if (i)
  1143. +            av_freep(&p->avctx->priv_data);
  1144. +
  1145. +        av_freep(&p->avctx);
  1146. +    }
  1147. +
  1148. +    av_freep(&fctx->threads);
  1149. +    pthread_mutex_destroy(&fctx->buffer_mutex);
  1150. +    av_freep(&avctx->thread_opaque);
  1151. +}
  1152. +
  1153. +static int frame_thread_init(AVCodecContext *avctx)
  1154. +{
  1155. +    FrameThreadContext *fctx;
  1156. +    AVCodecContext *src = avctx;
  1157. +    AVCodec *codec = avctx->codec;
  1158. +    int i, thread_count = avctx->thread_count, err = 0;
  1159. +
  1160. +    avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
  1161. +    fctx->delaying = 1;
  1162. +    pthread_mutex_init(&fctx->buffer_mutex, NULL);
  1163. +
  1164. +    fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
  1165. +
  1166. +    for (i = 0; i < thread_count; i++) {
  1167. +        AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
  1168. +        PerThreadContext *p  = &fctx->threads[i];
  1169. +
  1170. +        pthread_mutex_init(&p->mutex, NULL);
  1171. +        pthread_mutex_init(&p->progress_mutex, NULL);
  1172. +        pthread_cond_init(&p->input_cond, NULL);
  1173. +        pthread_cond_init(&p->progress_cond, NULL);
  1174. +        pthread_cond_init(&p->output_cond, NULL);
  1175. +
  1176. +        p->parent = fctx;
  1177. +        p->avctx  = copy;
  1178. +
  1179. +        *copy = *src;
  1180. +        copy->thread_opaque = p;
  1181. +
  1182. +        if (!i) {
  1183. +            src = copy;
  1184. +
  1185. +            if (codec->init)
  1186. +                err = codec->init(copy);
  1187. +        } else {
  1188. +            copy->is_copy   = 1;
  1189. +            copy->priv_data = av_malloc(codec->priv_data_size);
  1190. +            memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
  1191. +
  1192. +            if (codec->init_thread_copy)
  1193. +                err = codec->init_thread_copy(copy);
  1194. +        }
  1195. +
  1196. +        if (err) goto error;
  1197. +
  1198. +        pthread_create(&p->thread, NULL, frame_worker_thread, p);
  1199. +    }
  1200. +
  1201. +    update_thread_context_from_copy(avctx, src, 1);
  1202. +
  1203. +    return 0;
  1204. +
  1205. +error:
  1206. +    // the failed thread isn't completed but must be freed
  1207. +    frame_thread_free(avctx, i+1);
  1208. +
  1209. +    return err;
  1210. +}
  1211. +
  1212. +void ff_thread_flush(AVCodecContext *avctx)
  1213. +{
  1214. +    FrameThreadContext *fctx = avctx->thread_opaque;
  1215. +
  1216. +    if (!avctx->thread_opaque) return;
  1217. +
  1218. +    park_frame_worker_threads(fctx, avctx->thread_count);
  1219. +
  1220. +    if (fctx->prev_thread)
  1221. +        update_thread_context_from_copy(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
  1222. +
  1223. +    fctx->next_decoding = fctx->next_finished = 0;
  1224. +    fctx->delaying = 1;
  1225. +    fctx->prev_thread = NULL;
  1226. +}
  1227. +
  1228. +static int *allocate_progress(PerThreadContext *p)
  1229. +{
  1230. +    int i;
  1231. +
  1232. +    for (i = 0; i < MAX_BUFFERS; i++)
  1233. +        if (!p->used_progress[i]) break;
  1234. +
  1235. +    if (i == MAX_BUFFERS) {
  1236. +        av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
  1237. +        return NULL;
  1238. +    }
  1239. +
  1240. +    p->used_progress[i] = 1;
  1241. +
  1242. +    return p->progress[i];
  1243. +}
  1244. +
  1245. +int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1246. +{
  1247. +    int ret, *progress;
  1248. +    PerThreadContext *p = avctx->thread_opaque;
  1249. +
  1250. +    f->owner = avctx;
  1251. +
  1252. +    if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
  1253. +        f->thread_opaque = NULL;
  1254. +        return avctx->get_buffer(avctx, f);
  1255. +    }
  1256. +
  1257. +    pthread_mutex_lock(&p->parent->buffer_mutex);
  1258. +    f->thread_opaque = progress = allocate_progress(p);
  1259. +
  1260. +    if (!progress) {
  1261. +        pthread_mutex_unlock(&p->parent->buffer_mutex);
  1262. +        return -1;
  1263. +    }
  1264. +
  1265. +    progress[0] =
  1266. +    progress[1] = -1;
  1267. +
  1268. +    ret = avctx->get_buffer(avctx, f);
  1269. +    pthread_mutex_unlock(&p->parent->buffer_mutex);
  1270. +
  1271. +    /*
  1272. +     * The buffer list isn't shared between threads,
  1273. +     * so age doesn't mean what codecs expect it to mean.
  1274. +     * Disable it for now.
  1275. +     */
  1276. +    f->age = INT_MAX;
  1277. +
  1278. +    return ret;
  1279. +}
  1280. +
  1281. +void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1282. +{
  1283. +    PerThreadContext *p = avctx->thread_opaque;
  1284. +
  1285. +    if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
  1286. +        avctx->release_buffer(avctx, f);
  1287. +        return;
  1288. +    }
  1289. +
  1290. +    if (p->num_released_buffers >= MAX_BUFFERS) {
  1291. +        av_log(p->avctx, AV_LOG_ERROR, "too many delayed release_buffer calls!\n");
  1292. +        return;
  1293. +    }
  1294. +
  1295. +    if(avctx->debug & FF_DEBUG_BUFFERS)
  1296. +        av_log(avctx, AV_LOG_DEBUG, "delayed_release_buffer called on pic %p, %d buffers used\n",
  1297. +                                    f, f->owner->internal_buffer_count);
  1298. +
  1299. +    p->released_buffers[p->num_released_buffers++] = *f;
  1300. +    memset(f->data, 0, sizeof(f->data));
  1301. +}
  1302. +
  1303. +/// Set the threading algorithm used, or none if an algorithm was set but no thread count.
  1304. +static void validate_thread_parameters(AVCodecContext *avctx)
  1305. +{
  1306. +    int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
  1307. +                                && !(avctx->flags & CODEC_FLAG_TRUNCATED)
  1308. +                                && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
  1309. +                                && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
  1310. +    if (avctx->thread_count <= 1)
  1311. +        avctx->active_thread_type = 0;
  1312. +    else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME))
  1313. +        avctx->active_thread_type = FF_THREAD_FRAME;
  1314. +    else
  1315. +        avctx->active_thread_type = FF_THREAD_SLICE;
  1316. +}
  1317. +
  1318. +int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
  1319. +{
  1320. +    if (avctx->thread_opaque) {
  1321. +        av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init called after avcodec_open, this does nothing in ffmpeg-mt\n");
  1322. +        return -1;
  1323. +    }
  1324. +
  1325. +    avctx->thread_count = thread_count;
  1326. +
  1327. +    if (avctx->codec) {
  1328. +        validate_thread_parameters(avctx);
  1329. +
  1330. +        if (avctx->active_thread_type&FF_THREAD_SLICE)
  1331. +            return thread_init(avctx);
  1332. +        else if (avctx->active_thread_type&FF_THREAD_FRAME)
  1333. +            return frame_thread_init(avctx);
  1334. +    }
  1335. +
  1336. +    return 0;
  1337. +}
  1338. +
  1339. +void avcodec_thread_free(AVCodecContext *avctx)
  1340. +{
  1341. +    if (avctx->active_thread_type&FF_THREAD_FRAME)
  1342. +        frame_thread_free(avctx, avctx->thread_count);
  1343. +    else
  1344. +        thread_free(avctx);
  1345. +}
  1346. diff --git a/libavcodec/thread.h b/libavcodec/thread.h
  1347. new file mode 100644
  1348. index 0000000..dc4029e
  1349. --- /dev/null
  1350. +++ b/libavcodec/thread.h
  1351. @@ -0,0 +1,112 @@
  1352. +/*
  1353. + * Multithreading support
  1354. + * Copyright (c) 2008 Alexander Strange <astrange@ithinksw.com>
  1355. + *
  1356. + * This file is part of FFmpeg.
  1357. + *
  1358. + * FFmpeg is free software; you can redistribute it and/or
  1359. + * modify it under the terms of the GNU Lesser General Public
  1360. + * License as published by the Free Software Foundation; either
  1361. + * version 2.1 of the License, or (at your option) any later version.
  1362. + *
  1363. + * FFmpeg is distributed in the hope that it will be useful,
  1364. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1365. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  1366. + * Lesser General Public License for more details.
  1367. + *
  1368. + * You should have received a copy of the GNU Lesser General Public
  1369. + * License along with FFmpeg; if not, write to the Free Software
  1370. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  1371. + */
  1372. +
  1373. +/**
  1374. + * @file thread.h
  1375. + * Multithreading support header.
  1376. + * @author Alexander Strange <astrange@ithinksw.com>
  1377. + */
  1378. +
  1379. +#ifndef AVCODEC_THREAD_H
  1380. +#define AVCODEC_THREAD_H
  1381. +
  1382. +#include "config.h"
  1383. +#include "avcodec.h"
  1384. +
  1385. +/**
  1386. + * Waits for decoding threads to finish and resets the internal
  1387. + * state. Called by avcodec_flush_buffers().
  1388. + */
  1389. +void ff_thread_flush(AVCodecContext *avctx);
  1390. +
  1391. +/**
  1392. + * Submits a new frame to a decoding thread. Parameters are the
  1393. + * same as avcodec_decode_video2(). Returns the earliest available
  1394. + * decoded picture.
  1395. + *
  1396. + * NULL AVFrames returned from the codec will be dropped if
  1397. + * the client passes NULL data in.
  1398. + */
  1399. +int ff_thread_decode_frame(AVCodecContext *avctx,
  1400. +                        void *data, int *data_size,
  1401. +                        AVPacket *avpkt);
  1402. +
  1403. +/**
  1404. + * For codecs which define update_thread_context.
  1405. + * Call this when the context is set up for the next frame to be
  1406. + * decoded. The next decoding thread will start afterwards.
  1407. + * The codec must not modify parts of the context read by
  1408. + * update_thread_context after calling this or it will cause a race
  1409. + * condition.
  1410. + */
  1411. +void ff_thread_finish_setup(AVCodecContext *avctx);
  1412. +
  1413. +/**
  1414. + * Call this when some part of the picture is finished decoding.
  1415. + * Later calls with lower progress values will be ignored.
  1416. + *
  1417. + * @param f The AVFrame containing the current field or frame
  1418. + * @param progress The highest-numbered part finished so far
  1419. + * @param field The field being decoded, for field pictures.
  1420. + * 0 for top field or progressive, 1 for bottom.
  1421. + */
  1422. +void ff_thread_report_progress(AVFrame *f, int progress, int field);
  1423. +
  1424. +/**
  1425. + * Call this before accessing some part of a previous field or frame.
  1426. + * Returns after the previous decoding thread has called ff_thread_report_progress()
  1427. + * with sufficiently high progress.
  1428. + *
  1429. + * @param f The AVFrame containing the reference field or frame
  1430. + * @param progress The highest-numbered part of the reference picture to wait for
  1431. + * @param field The field being referenced, for field pictures.
  1432. + * 0 for top field or progressive, 1 for bottom.
  1433. + */
  1434. +void ff_thread_await_progress(AVFrame *f, int progress, int field);
  1435. +
  1436. +/**
  1437. + * Convenience function to set progress for both fields to INT_MAX.
  1438. + * Can be used to prevent deadlocks in later threads when a decoder
  1439. + * exits early due to errors.
  1440. + *
  1441. + * @param f The frame or field picture being decoded.
  1442. + */
  1443. +void ff_thread_finish_frame(AVFrame *f);
  1444. +
  1445. +/**
  1446. + * Replacement for get_buffer() for frame-level threading.
  1447. + *
  1448. + * Codecs with CODEC_CAP_FRAME_THREADS must call this instead
  1449. + * of calling get_buffer() directly.
  1450. + */
  1451. +int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f);
  1452. +
  1453. +/**
  1454. + * Replacement for release_buffer() for frame-level threading.
  1455. + *
  1456. + * Codecs with CODEC_CAP_FRAME_THREADS must call this instead
  1457. + * of calling release_buffer() directly.
  1458. + *
  1459. + * On return, \p f->data will be cleared.
  1460. + */
  1461. +void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f);
  1462. +
  1463. +#endif /* AVCODEC_THREAD_H */
  1464. diff --git a/libavcodec/utils.c b/libavcodec/utils.c
  1465. index ffd34ee..ea2b5f0 100644
  1466. --- a/libavcodec/utils.c
  1467. +++ b/libavcodec/utils.c
  1468. @@ -34,6 +34,7 @@
  1469.  #include "dsputil.h"
  1470.  #include "libavutil/opt.h"
  1471.  #include "imgconvert.h"
  1472. +#include "thread.h"
  1473.  #include "audioconvert.h"
  1474.  #include "internal.h"
  1475.  #include <stdlib.h>
  1476. @@ -254,6 +255,11 @@ int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  1477.      (*picture_number)++;
  1478.  
  1479.      if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  1480. +        if(s->active_thread_type&FF_THREAD_FRAME) {
  1481. +            av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
  1482. +            return -1;
  1483. +        }
  1484. +
  1485.          for(i=0; i<4; i++){
  1486.              av_freep(&buf->base[i]);
  1487.              buf->data[i]= NULL;
  1488. @@ -352,6 +358,7 @@ void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  1489.      assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  1490.      assert(s->internal_buffer_count);
  1491.  
  1492. +    if(s->internal_buffer){
  1493.      buf = NULL; /* avoids warning */
  1494.      for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  1495.          buf= &((InternalBuffer*)s->internal_buffer)[i];
  1496. @@ -363,6 +370,7 @@ void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  1497.      last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  1498.  
  1499.      FFSWAP(InternalBuffer, *buf, *last);
  1500. +    }
  1501.  
  1502.      for(i=0; i<4; i++){
  1503.          pic->data[i]=NULL;
  1504. @@ -515,18 +523,29 @@ int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  1505.          goto free_and_end;
  1506.      }
  1507.      avctx->frame_number = 0;
  1508. +
  1509. +    if (HAVE_THREADS && !avctx->thread_opaque) {
  1510. +        ret = avcodec_thread_init(avctx, avctx->thread_count);
  1511. +        if (ret < 0) {
  1512. +            goto free_and_end;
  1513. +        }
  1514. +    }
  1515. +
  1516.      if (avctx->codec->max_lowres < avctx->lowres) {
  1517.          av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  1518.                 avctx->codec->max_lowres);
  1519.          goto free_and_end;
  1520.      }
  1521.  
  1522. -    if(avctx->codec->init){
  1523. -        ret = avctx->codec->init(avctx);
  1524. -        if (ret < 0) {
  1525. -            goto free_and_end;
  1526. +    if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  1527. +        if(avctx->codec->init){
  1528. +            ret = avctx->codec->init(avctx);
  1529. +            if (ret < 0) {
  1530. +                goto free_and_end;
  1531. +            }
  1532.          }
  1533.      }
  1534. +
  1535.      ret=0;
  1536.  end:
  1537.      entangled_thread_counter--;
  1538. @@ -612,12 +631,15 @@ int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *pi
  1539.                           AVPacket *avpkt)
  1540.  {
  1541.      int ret;
  1542. +    int threaded = avctx->active_thread_type&FF_THREAD_FRAME;
  1543.  
  1544.      *got_picture_ptr= 0;
  1545.      if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  1546.          return -1;
  1547. -    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  1548. -        ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1549. +    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || threaded){
  1550. +        if (HAVE_PTHREADS && threaded) ret = ff_thread_decode_frame(avctx, picture,
  1551. +                                got_picture_ptr, avpkt);
  1552. +        else ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  1553.                                  avpkt);
  1554.  
  1555.          emms_c(); //needed to avoid an emms_c() call before every return;
  1556. @@ -735,7 +757,7 @@ av_cold int avcodec_close(AVCodecContext *avctx)
  1557.  
  1558.      if (HAVE_THREADS && avctx->thread_opaque)
  1559.          avcodec_thread_free(avctx);
  1560. -    if (avctx->codec && avctx->codec->close)
  1561. +    if (avctx->codec && avctx->codec->close && !(avctx->active_thread_type&FF_THREAD_FRAME))
  1562.          avctx->codec->close(avctx);
  1563.      avcodec_default_free_buffers(avctx);
  1564.      avctx->coded_frame = NULL;
  1565. @@ -743,6 +765,7 @@ av_cold int avcodec_close(AVCodecContext *avctx)
  1566.      if(avctx->codec && avctx->codec->encode)
  1567.          av_freep(&avctx->extradata);
  1568.      avctx->codec = NULL;
  1569. +    avctx->active_thread_type = 0;
  1570.      entangled_thread_counter--;
  1571.  
  1572.      /* Release any user-supplied mutex. */
  1573. @@ -983,6 +1006,8 @@ void avcodec_init(void)
  1574.  
  1575.  void avcodec_flush_buffers(AVCodecContext *avctx)
  1576.  {
  1577. +    if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  1578. +        ff_thread_flush(avctx);
  1579.      if(avctx->codec->flush)
  1580.          avctx->codec->flush(avctx);
  1581.  }
  1582. @@ -1189,7 +1214,38 @@ int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1583.  unsigned int ff_toupper4(unsigned int x)
  1584.  {
  1585.      return     toupper( x     &0xFF)
  1586. -            + (toupper((x>>8 )&0xFF)<<8 )
  1587. -            + (toupper((x>>16)&0xFF)<<16)
  1588. -            + (toupper((x>>24)&0xFF)<<24);
  1589. +    + (toupper((x>>8 )&0xFF)<<8 )
  1590. +    + (toupper((x>>16)&0xFF)<<16)
  1591. +    + (toupper((x>>24)&0xFF)<<24);
  1592. +}
  1593. +
  1594. +#if !HAVE_PTHREADS
  1595. +
  1596. +int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1597. +{
  1598. +    f->owner = avctx;
  1599. +    return avctx->get_buffer(avctx, f);
  1600. +}
  1601. +
  1602. +void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1603. +{
  1604. +    f->owner->release_buffer(f->owner, f);
  1605. +}
  1606. +
  1607. +void ff_thread_finish_setup(AVCodecContext *avctx)
  1608. +{
  1609. +}
  1610. +
  1611. +void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1612. +{
  1613.  }
  1614. +
  1615. +void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1616. +{
  1617. +}
  1618. +
  1619. +void ff_thread_finish_frame(AVFrame *f)
  1620. +{
  1621. +}
  1622. +
  1623. +#endif
  1624. diff --git a/libavcodec/vp3.c b/libavcodec/vp3.c
  1625. index 59719e7..0f63a24 100644
  1626. --- a/libavcodec/vp3.c
  1627. +++ b/libavcodec/vp3.c
  1628. @@ -40,6 +40,7 @@
  1629.  
  1630.  #include "vp3data.h"
  1631.  #include "xiph.h"
  1632. +#include "thread.h"
  1633.  
  1634.  #define FRAGMENT_PIXELS 8
  1635.  
  1636. @@ -1318,6 +1319,12 @@ static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
  1637.      int h, cy;
  1638.      int offset[4];
  1639.  
  1640. +    if (HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
  1641. +        int y_flipped = s->flipped_image ? s->avctx->height-y : y;
  1642. +
  1643. +        ff_thread_report_progress(&s->current_frame, y_flipped==s->avctx->height ? s->avctx->height : y_flipped-1, 0);
  1644. +    }
  1645. +
  1646.      if(s->avctx->draw_horiz_band==NULL)
  1647.          return;
  1648.  
  1649. @@ -1339,6 +1346,32 @@ static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
  1650.      s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
  1651.  }
  1652.  
  1653. +/**
  1654. + * Wait for the reference frame of a fragment.
  1655. + * Units used are luma pixel rows.
  1656. + */
  1657. +static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment, int motion_y, int y)
  1658. +{
  1659. +    AVFrame *ref_frame;
  1660. +    int border = motion_y&1;
  1661. +    int max_row = s->avctx->height;
  1662. +    int ref_row;
  1663. +
  1664. +    if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
  1665. +        return;
  1666. +
  1667. +    if (fragment->coding_method == MODE_USING_GOLDEN ||
  1668. +        fragment->coding_method == MODE_GOLDEN_MV)
  1669. +        ref_frame = &s->golden_frame;
  1670. +    else
  1671. +        ref_frame = &s->last_frame;
  1672. +
  1673. +    ref_row = y + (motion_y >> 1);
  1674. +    ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
  1675. +
  1676. +    ff_thread_await_progress(ref_frame, FFMIN(ref_row, max_row), 0);
  1677. +}
  1678. +
  1679.  /*
  1680.   * Perform the final rendering for a particular slice of data.
  1681.   * The slice number ranges from 0..(c_superblock_height - 1).
  1682. @@ -1399,6 +1432,9 @@ static void render_slice(Vp3DecodeContext *s, int slice)
  1683.  
  1684.                  first_pixel = 8*y*stride + 8*x;
  1685.  
  1686. +                if (s->all_fragments[i].coding_method != MODE_INTRA && !plane)
  1687. +                    await_reference_row(s, &s->all_fragments[i], motion_val[y*fragment_width + x][1], (16*y) >> s->chroma_y_shift);
  1688. +
  1689.                  /* transform if this block was coded */
  1690.                  if (s->all_fragments[i].coding_method != MODE_COPY) {
  1691.                      if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
  1692. @@ -1510,6 +1546,38 @@ static void render_slice(Vp3DecodeContext *s, int slice)
  1693.      vp3_draw_horiz_band(s, FFMIN((32 << s->chroma_y_shift) * (slice + 1) -16, s->height-16));
  1694.  }
  1695.  
  1696. +/// Allocate tables for frame data in Vp3DecodeContext
  1697. +static av_cold int allocate_tables(AVCodecContext *avctx)
  1698. +{
  1699. +    Vp3DecodeContext *s = avctx->priv_data;
  1700. +    int y_fragment_count, c_fragment_count;
  1701. +
  1702. +    y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
  1703. +    c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
  1704. +
  1705. +    s->superblock_coding = av_malloc(s->superblock_count);
  1706. +    s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1707. +    s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int));
  1708. +    s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base));
  1709. +    s->motion_val[0] = av_malloc(y_fragment_count * sizeof(*s->motion_val[0]));
  1710. +    s->motion_val[1] = av_malloc(c_fragment_count * sizeof(*s->motion_val[1]));
  1711. +
  1712. +    /* work out the block mapping tables */
  1713. +    s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  1714. +    s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  1715. +
  1716. +    if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base ||
  1717. +        !s->coded_fragment_list[0] || !s->superblock_fragments || !s->macroblock_coding ||
  1718. +        !s->motion_val[0] || !s->motion_val[1]) {
  1719. +        vp3_decode_end(avctx);
  1720. +        return -1;
  1721. +    }
  1722. +
  1723. +    init_block_mapping(s);
  1724. +
  1725. +    return 0;
  1726. +}
  1727. +
  1728.  /*
  1729.   * This is the ffmpeg/libavcodec API init function.
  1730.   */
  1731. @@ -1559,7 +1627,6 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx)
  1732.      s->superblock_count = s->y_superblock_count + (s->c_superblock_count * 2);
  1733.      s->u_superblock_start = s->y_superblock_count;
  1734.      s->v_superblock_start = s->u_superblock_start + s->c_superblock_count;
  1735. -    s->superblock_coding = av_malloc(s->superblock_count);
  1736.  
  1737.      s->macroblock_width = (s->width + 15) / 16;
  1738.      s->macroblock_height = (s->height + 15) / 16;
  1739. @@ -1577,18 +1644,6 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx)
  1740.      s->fragment_start[1] = y_fragment_count;
  1741.      s->fragment_start[2] = y_fragment_count + c_fragment_count;
  1742.  
  1743. -    s->all_fragments = av_malloc(s->fragment_count * sizeof(Vp3Fragment));
  1744. -    s->coded_fragment_list[0] = av_malloc(s->fragment_count * sizeof(int));
  1745. -    s->dct_tokens_base = av_malloc(64*s->fragment_count * sizeof(*s->dct_tokens_base));
  1746. -    s->motion_val[0] = av_malloc(y_fragment_count * sizeof(*s->motion_val[0]));
  1747. -    s->motion_val[1] = av_malloc(c_fragment_count * sizeof(*s->motion_val[1]));
  1748. -
  1749. -    if (!s->superblock_coding || !s->all_fragments || !s->dct_tokens_base ||
  1750. -        !s->coded_fragment_list[0] || !s->motion_val[0] || !s->motion_val[1]) {
  1751. -        vp3_decode_end(avctx);
  1752. -        return -1;
  1753. -    }
  1754. -
  1755.      if (!s->theora_tables)
  1756.      {
  1757.          for (i = 0; i < 64; i++) {
  1758. @@ -1688,28 +1743,92 @@ static av_cold int vp3_decode_init(AVCodecContext *avctx)
  1759.          &motion_vector_vlc_table[0][1], 2, 1,
  1760.          &motion_vector_vlc_table[0][0], 2, 1, 0);
  1761.  
  1762. -    /* work out the block mapping tables */
  1763. -    s->superblock_fragments = av_malloc(s->superblock_count * 16 * sizeof(int));
  1764. -    s->macroblock_coding = av_malloc(s->macroblock_count + 1);
  1765. -    if (!s->superblock_fragments || !s->macroblock_coding) {
  1766. -        vp3_decode_end(avctx);
  1767. -        return -1;
  1768. -    }
  1769. -    init_block_mapping(s);
  1770. -
  1771.      for (i = 0; i < 3; i++) {
  1772.          s->current_frame.data[i] = NULL;
  1773.          s->last_frame.data[i] = NULL;
  1774.          s->golden_frame.data[i] = NULL;
  1775.      }
  1776.  
  1777. -    return 0;
  1778. +    return allocate_tables(avctx);
  1779.  
  1780.  vlc_fail:
  1781.      av_log(avctx, AV_LOG_FATAL, "Invalid huffman table\n");
  1782.      return -1;
  1783.  }
  1784.  
  1785. +/// Release and shuffle frames after decode finishes
  1786. +static void update_frames(AVCodecContext *avctx)
  1787. +{
  1788. +    Vp3DecodeContext *s = avctx->priv_data;
  1789. +
  1790. +    /* release the last frame, if it is allocated and if it is not the
  1791. +     * golden frame */
  1792. +    if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
  1793. +        ff_thread_release_buffer(avctx, &s->last_frame);
  1794. +
  1795. +    /* shuffle frames (last = current) */
  1796. +    s->last_frame= s->current_frame;
  1797. +
  1798. +    if (s->keyframe) {
  1799. +        if (s->golden_frame.data[0])
  1800. +            ff_thread_release_buffer(avctx, &s->golden_frame);
  1801. +        s->golden_frame = s->current_frame;
  1802. +        s->last_frame.type = FF_BUFFER_TYPE_COPY;
  1803. +    }
  1804. +
  1805. +    s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
  1806. +}
  1807. +
  1808. +#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
  1809. +static int vp3_update_thread_context(AVCodecContext *dst, AVCodecContext *src)
  1810. +{
  1811. +    Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
  1812. +    int qps_changed = 0, i, err;
  1813. +
  1814. +    if (!s1->current_frame.data[0]
  1815. +        ||s->width != s1->width
  1816. +        ||s->height!= s1->height)
  1817. +        return -1;
  1818. +
  1819. +    if (s != s1) {
  1820. +        // init tables if the first frame hasn't been decoded
  1821. +        if (!s->current_frame.data[0]) {
  1822. +            int y_fragment_count, c_fragment_count;
  1823. +            s->avctx = dst;
  1824. +            err = allocate_tables(dst);
  1825. +            if (err)
  1826. +                return err;
  1827. +            y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
  1828. +            c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
  1829. +            memcpy(s->motion_val[0], s1->motion_val[0], y_fragment_count * sizeof(*s->motion_val[0]));
  1830. +            memcpy(s->motion_val[1], s1->motion_val[1], c_fragment_count * sizeof(*s->motion_val[1]));
  1831. +        }
  1832. +
  1833. +        // copy previous frame data
  1834. +        copy_fields(s, s1, golden_frame, dsp);
  1835. +
  1836. +        // copy qscale data if necessary
  1837. +        for (i = 0; i < 3; i++) {
  1838. +            if (s->qps[i] != s1->qps[1]) {
  1839. +                qps_changed = 1;
  1840. +                memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
  1841. +            }
  1842. +        }
  1843. +
  1844. +        if (s->qps[0] != s1->qps[0]) {
  1845. +            memcpy(&s->qscale_table, &s1->qscale_table, sizeof(s->qscale_table));
  1846. +            memcpy(&s->bounding_values_array, &s1->bounding_values_array, sizeof(s->bounding_values_array));
  1847. +        }
  1848. +
  1849. +        if (qps_changed)
  1850. +            copy_fields(s, s1, qps, superblock_count);
  1851. +    }
  1852. +
  1853. +    update_frames(dst);
  1854. +
  1855. +    return 0;
  1856. +}
  1857. +
  1858.  /*
  1859.   * This is the ffmpeg/libavcodec API frame decode function.
  1860.   */
  1861. @@ -1721,7 +1840,6 @@ static int vp3_decode_frame(AVCodecContext *avctx,
  1862.      int buf_size = avpkt->size;
  1863.      Vp3DecodeContext *s = avctx->priv_data;
  1864.      GetBitContext gb;
  1865. -    static int counter = 0;
  1866.      int i;
  1867.  
  1868.      init_get_bits(&gb, buf, buf_size * 8);
  1869. @@ -1747,8 +1865,7 @@ static int vp3_decode_frame(AVCodecContext *avctx,
  1870.  
  1871.      if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1872.          av_log(s->avctx, AV_LOG_INFO, " VP3 %sframe #%d: Q index = %d\n",
  1873. -            s->keyframe?"key":"", counter, s->qps[0]);
  1874. -    counter++;
  1875. +            s->keyframe?"key":"", avctx->frame_number+1, s->qps[0]);
  1876.  
  1877.      s->skip_loop_filter = !s->filter_limit_values[s->qps[0]] ||
  1878.          avctx->skip_loop_filter >= (s->keyframe ? AVDISCARD_ALL : AVDISCARD_NONKEY);
  1879. @@ -1767,7 +1884,7 @@ static int vp3_decode_frame(AVCodecContext *avctx,
  1880.  
  1881.      s->current_frame.reference = 3;
  1882.      s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
  1883. -    if (avctx->get_buffer(avctx, &s->current_frame) < 0) {
  1884. +    if (ff_thread_get_buffer(avctx, &s->current_frame) < 0) {
  1885.          av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1886.          goto error;
  1887.      }
  1888. @@ -1780,7 +1897,7 @@ static int vp3_decode_frame(AVCodecContext *avctx,
  1889.              if (s->version)
  1890.              {
  1891.                  s->version = get_bits(&gb, 5);
  1892. -                if (counter == 1)
  1893. +                if (avctx->frame_number == 0)
  1894.                      av_log(s->avctx, AV_LOG_DEBUG, "VP version: %d\n", s->version);
  1895.              }
  1896.          }
  1897. @@ -1796,7 +1913,7 @@ static int vp3_decode_frame(AVCodecContext *avctx,
  1898.  
  1899.              s->golden_frame.reference = 3;
  1900.              s->golden_frame.pict_type = FF_I_TYPE;
  1901. -            if (avctx->get_buffer(avctx, &s->golden_frame) < 0) {
  1902. +            if (ff_thread_get_buffer(avctx, &s->golden_frame) < 0) {
  1903.                  av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1904.                  goto error;
  1905.              }
  1906. @@ -1809,6 +1926,7 @@ static int vp3_decode_frame(AVCodecContext *avctx,
  1907.      s->current_frame.qstride= 0;
  1908.  
  1909.      memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
  1910. +    ff_thread_finish_setup(avctx);
  1911.  
  1912.      if (unpack_superblocks(s, &gb)){
  1913.          av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
  1914. @@ -1853,28 +1971,17 @@ static int vp3_decode_frame(AVCodecContext *avctx,
  1915.      *data_size=sizeof(AVFrame);
  1916.      *(AVFrame*)data= s->current_frame;
  1917.  
  1918. -    /* release the last frame, if it is allocated and if it is not the
  1919. -     * golden frame */
  1920. -    if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
  1921. -        avctx->release_buffer(avctx, &s->last_frame);
  1922. -
  1923. -    /* shuffle frames (last = current) */
  1924. -    s->last_frame= s->current_frame;
  1925. -
  1926. -    if (s->keyframe) {
  1927. -        if (s->golden_frame.data[0])
  1928. -            avctx->release_buffer(avctx, &s->golden_frame);
  1929. -        s->golden_frame = s->current_frame;
  1930. -        s->last_frame.type = FF_BUFFER_TYPE_COPY;
  1931. -    }
  1932. -
  1933. -    s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
  1934. +    if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
  1935. +        update_frames(avctx);
  1936.  
  1937.      return buf_size;
  1938.  
  1939.  error:
  1940. -    if (s->current_frame.data[0])
  1941. +    ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
  1942. +
  1943. +    if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
  1944.          avctx->release_buffer(avctx, &s->current_frame);
  1945. +
  1946.      return -1;
  1947.  }
  1948.  
  1949. @@ -1886,6 +1993,9 @@ static av_cold int vp3_decode_end(AVCodecContext *avctx)
  1950.      Vp3DecodeContext *s = avctx->priv_data;
  1951.      int i;
  1952.  
  1953. +    if (avctx->is_copy && !s->current_frame.data[0])
  1954. +        return 0;
  1955. +
  1956.      av_free(s->superblock_coding);
  1957.      av_free(s->all_fragments);
  1958.      av_free(s->coded_fragment_list[0]);
  1959. @@ -1895,6 +2005,8 @@ static av_cold int vp3_decode_end(AVCodecContext *avctx)
  1960.      av_free(s->motion_val[0]);
  1961.      av_free(s->motion_val[1]);
  1962.  
  1963. +    if (avctx->is_copy) return 0;
  1964. +
  1965.      for (i = 0; i < 16; i++) {
  1966.          free_vlc(&s->dc_vlc[i]);
  1967.          free_vlc(&s->ac_vlc_1[i]);
  1968. @@ -1910,9 +2022,9 @@ static av_cold int vp3_decode_end(AVCodecContext *avctx)
  1969.  
  1970.      /* release all frames */
  1971.      if (s->golden_frame.data[0])
  1972. -        avctx->release_buffer(avctx, &s->golden_frame);
  1973. +        ff_thread_release_buffer(avctx, &s->golden_frame);
  1974.      if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
  1975. -        avctx->release_buffer(avctx, &s->last_frame);
  1976. +        ff_thread_release_buffer(avctx, &s->last_frame);
  1977.      /* no need to release the current_frame since it will always be pointing
  1978.       * to the same frame as either the golden or last frame */
  1979.  
  1980. @@ -2227,9 +2339,10 @@ AVCodec theora_decoder = {
  1981.      NULL,
  1982.      vp3_decode_end,
  1983.      vp3_decode_frame,
  1984. -    CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
  1985. +    CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
  1986.      NULL,
  1987.      .long_name = NULL_IF_CONFIG_SMALL("Theora"),
  1988. +    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
  1989.  };
  1990.  #endif
  1991.  
  1992. @@ -2242,7 +2355,8 @@ AVCodec vp3_decoder = {
  1993.      NULL,
  1994.      vp3_decode_end,
  1995.      vp3_decode_frame,
  1996. -    CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
  1997. +    CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
  1998.      NULL,
  1999.      .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
  2000. +    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
  2001.  };
  2002. diff --git a/libavcodec/w32thread.c b/libavcodec/w32thread.c
  2003. index f7a1430..007508e 100644
  2004. --- a/libavcodec/w32thread.c
  2005. +++ b/libavcodec/w32thread.c
  2006. @@ -129,7 +129,13 @@ int avcodec_thread_init(AVCodecContext *s, int thread_count){
  2007.      ThreadContext *c;
  2008.      uint32_t threadid;
  2009.  
  2010. +    if(!(s->thread_type & FF_THREAD_SLICE)){
  2011. +        av_log(s, AV_LOG_WARNING, "The requested thread algorithm is not supported with this thread library.\n");
  2012. +        return 0;
  2013. +    }
  2014. +
  2015.      s->thread_count= thread_count;
  2016. +    s->active_thread_type= FF_THREAD_SLICE;
  2017.  
  2018.      if (thread_count <= 1)
  2019.          return 0;
  2020. diff --git a/libavformat/utils.c b/libavformat/utils.c
  2021. index 8a08557..16957e6 100644
  2022. --- a/libavformat/utils.c
  2023. +++ b/libavformat/utils.c
  2024. @@ -909,6 +909,11 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
  2025.      /* do we have a video B-frame ? */
  2026.      delay= st->codec->has_b_frames;
  2027.      presentation_delayed = 0;
  2028. +
  2029. +    // this delay should not count for undecoded frames
  2030. +    if (delay && st->codec->active_thread_type&FF_THREAD_FRAME)
  2031. +        delay -= st->codec->thread_count-1;
  2032. +
  2033.      /* XXX: need has_b_frame, but cannot get it if the codec is
  2034.          not initialized */
  2035.      if (delay &&
  2036. diff --git a/libavutil/internal.h b/libavutil/internal.h
  2037. index 53d2b94..cc20ce7 100644
  2038. --- a/libavutil/internal.h
  2039. +++ b/libavutil/internal.h
  2040. @@ -202,7 +202,6 @@
  2041.  #   define NULL_IF_CONFIG_SMALL(x) x
  2042.  #endif
  2043.  
  2044. -
  2045.  /**
  2046.   * Define a function with only the non-default version specified.
  2047.   *
  2048. @@ -231,4 +230,15 @@
  2049.      type ff_##name args
  2050.  #endif
  2051.  
  2052. +/**
  2053. + * Returns NULL if a threading library has not been enabled.
  2054. + * Used to disable threading functions in AVCodec definitions
  2055. + * when not needed.
  2056. + */
  2057. +#if HAVE_THREADS
  2058. +#   define ONLY_IF_THREADS_ENABLED(x) x
  2059. +#else
  2060. +#   define ONLY_IF_THREADS_ENABLED(x) NULL
  2061. +#endif
  2062. +
  2063.  #endif /* AVUTIL_INTERNAL_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement