Mercurial > libavcodec.hg
comparison utils.c @ 9742:0d6f887d91fb libavcodec
Add a lock manager API to libavcodec.
Allows an application to register a callback that manages mutexes
on behalf of FFmpeg.
With this callback registered FFmpeg is fully thread safe.
| author | andoma |
|---|---|
| date | Sun, 31 May 2009 06:51:18 +0000 |
| parents | bc32976d6d9d |
| children | f114b5f2ac47 |
comparison
equal
deleted
inserted
replaced
| 9741:e52891e48ceb | 9742:0d6f887d91fb |
|---|---|
| 63 0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7, | 63 0x07,0x87,0x47,0xC7,0x27,0xA7,0x67,0xE7,0x17,0x97,0x57,0xD7,0x37,0xB7,0x77,0xF7, |
| 64 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF, | 64 0x0F,0x8F,0x4F,0xCF,0x2F,0xAF,0x6F,0xEF,0x1F,0x9F,0x5F,0xDF,0x3F,0xBF,0x7F,0xFF, |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 static int volatile entangled_thread_counter=0; | 67 static int volatile entangled_thread_counter=0; |
| 68 int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op); | |
| 69 static void *codec_mutex; | |
| 68 | 70 |
| 69 void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size) | 71 void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size) |
| 70 { | 72 { |
| 71 if(min_size < *size) | 73 if(min_size < *size) |
| 72 return ptr; | 74 return ptr; |
| 437 | 439 |
| 438 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec) | 440 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec) |
| 439 { | 441 { |
| 440 int ret= -1; | 442 int ret= -1; |
| 441 | 443 |
| 444 /* If there is a user-supplied mutex locking routine, call it. */ | |
| 445 if (ff_lockmgr_cb) { | |
| 446 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) | |
| 447 return -1; | |
| 448 } | |
| 449 | |
| 442 entangled_thread_counter++; | 450 entangled_thread_counter++; |
| 443 if(entangled_thread_counter != 1){ | 451 if(entangled_thread_counter != 1){ |
| 444 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n"); | 452 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n"); |
| 445 goto end; | 453 goto end; |
| 446 } | 454 } |
| 481 } | 489 } |
| 482 } | 490 } |
| 483 ret=0; | 491 ret=0; |
| 484 end: | 492 end: |
| 485 entangled_thread_counter--; | 493 entangled_thread_counter--; |
| 494 | |
| 495 /* Release any user-supplied mutex. */ | |
| 496 if (ff_lockmgr_cb) { | |
| 497 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); | |
| 498 } | |
| 486 return ret; | 499 return ret; |
| 487 } | 500 } |
| 488 | 501 |
| 489 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size, | 502 int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size, |
| 490 const short *samples) | 503 const short *samples) |
| 640 return ret; | 653 return ret; |
| 641 } | 654 } |
| 642 | 655 |
| 643 int avcodec_close(AVCodecContext *avctx) | 656 int avcodec_close(AVCodecContext *avctx) |
| 644 { | 657 { |
| 658 /* If there is a user-supplied mutex locking routine, call it. */ | |
| 659 if (ff_lockmgr_cb) { | |
| 660 if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN)) | |
| 661 return -1; | |
| 662 } | |
| 663 | |
| 645 entangled_thread_counter++; | 664 entangled_thread_counter++; |
| 646 if(entangled_thread_counter != 1){ | 665 if(entangled_thread_counter != 1){ |
| 647 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n"); | 666 av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n"); |
| 648 entangled_thread_counter--; | 667 entangled_thread_counter--; |
| 649 return -1; | 668 return -1; |
| 655 avctx->codec->close(avctx); | 674 avctx->codec->close(avctx); |
| 656 avcodec_default_free_buffers(avctx); | 675 avcodec_default_free_buffers(avctx); |
| 657 av_freep(&avctx->priv_data); | 676 av_freep(&avctx->priv_data); |
| 658 avctx->codec = NULL; | 677 avctx->codec = NULL; |
| 659 entangled_thread_counter--; | 678 entangled_thread_counter--; |
| 679 | |
| 680 /* Release any user-supplied mutex. */ | |
| 681 if (ff_lockmgr_cb) { | |
| 682 (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE); | |
| 683 } | |
| 660 return 0; | 684 return 0; |
| 661 } | 685 } |
| 662 | 686 |
| 663 AVCodec *avcodec_find_encoder(enum CodecID id) | 687 AVCodec *avcodec_find_encoder(enum CodecID id) |
| 664 { | 688 { |
| 1211 && hwaccel->pix_fmt == pix_fmt) | 1235 && hwaccel->pix_fmt == pix_fmt) |
| 1212 return hwaccel; | 1236 return hwaccel; |
| 1213 } | 1237 } |
| 1214 return NULL; | 1238 return NULL; |
| 1215 } | 1239 } |
| 1240 | |
| 1241 int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op)) | |
| 1242 { | |
| 1243 if (ff_lockmgr_cb) { | |
| 1244 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY)) | |
| 1245 return -1; | |
| 1246 } | |
| 1247 | |
| 1248 ff_lockmgr_cb = cb; | |
| 1249 | |
| 1250 if (ff_lockmgr_cb) { | |
| 1251 if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE)) | |
| 1252 return -1; | |
| 1253 } | |
| 1254 return 0; | |
| 1255 } |
