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.

1297 lines
37KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file libavcodec/utils.c
  24. * utils.
  25. */
  26. /* needed for mkstemp() */
  27. #define _XOPEN_SOURCE 600
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/integer.h"
  30. #include "libavutil/crc.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "avcodec.h"
  33. #include "dsputil.h"
  34. #include "opt.h"
  35. #include "imgconvert.h"
  36. #include "audioconvert.h"
  37. #include "libxvid_internal.h"
  38. #include "internal.h"
  39. #include <stdlib.h>
  40. #include <stdarg.h>
  41. #include <limits.h>
  42. #include <float.h>
  43. #if !HAVE_MKSTEMP
  44. #include <fcntl.h>
  45. #endif
  46. static int volatile entangled_thread_counter=0;
  47. int (*ff_lockmgr_cb)(void **mutex, enum AVLockOp op);
  48. static void *codec_mutex;
  49. void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
  50. {
  51. if(min_size < *size)
  52. return ptr;
  53. *size= FFMAX(17*min_size/16 + 32, min_size);
  54. ptr= av_realloc(ptr, *size);
  55. if(!ptr) //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
  56. *size= 0;
  57. return ptr;
  58. }
  59. void av_fast_malloc(void *ptr, unsigned int *size, unsigned int min_size)
  60. {
  61. void **p = ptr;
  62. if (min_size < *size)
  63. return;
  64. *size= FFMAX(17*min_size/16 + 32, min_size);
  65. av_free(*p);
  66. *p = av_malloc(*size);
  67. if (!*p) *size = 0;
  68. }
  69. /* encoder management */
  70. static AVCodec *first_avcodec = NULL;
  71. AVCodec *av_codec_next(AVCodec *c){
  72. if(c) return c->next;
  73. else return first_avcodec;
  74. }
  75. void avcodec_register(AVCodec *codec)
  76. {
  77. AVCodec **p;
  78. avcodec_init();
  79. p = &first_avcodec;
  80. while (*p != NULL) p = &(*p)->next;
  81. *p = codec;
  82. codec->next = NULL;
  83. }
  84. #if LIBAVCODEC_VERSION_MAJOR < 53
  85. void register_avcodec(AVCodec *codec)
  86. {
  87. avcodec_register(codec);
  88. }
  89. #endif
  90. void avcodec_set_dimensions(AVCodecContext *s, int width, int height){
  91. s->coded_width = width;
  92. s->coded_height= height;
  93. s->width = -((-width )>>s->lowres);
  94. s->height= -((-height)>>s->lowres);
  95. }
  96. typedef struct InternalBuffer{
  97. int last_pic_num;
  98. uint8_t *base[4];
  99. uint8_t *data[4];
  100. int linesize[4];
  101. int width, height;
  102. enum PixelFormat pix_fmt;
  103. }InternalBuffer;
  104. #define INTERNAL_BUFFER_SIZE 32
  105. void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[4]){
  106. int w_align= 1;
  107. int h_align= 1;
  108. switch(s->pix_fmt){
  109. case PIX_FMT_YUV420P:
  110. case PIX_FMT_YUYV422:
  111. case PIX_FMT_UYVY422:
  112. case PIX_FMT_YUV422P:
  113. case PIX_FMT_YUV440P:
  114. case PIX_FMT_YUV444P:
  115. case PIX_FMT_GRAY8:
  116. case PIX_FMT_GRAY16BE:
  117. case PIX_FMT_GRAY16LE:
  118. case PIX_FMT_YUVJ420P:
  119. case PIX_FMT_YUVJ422P:
  120. case PIX_FMT_YUVJ440P:
  121. case PIX_FMT_YUVJ444P:
  122. case PIX_FMT_YUVA420P:
  123. w_align= 16; //FIXME check for non mpeg style codecs and use less alignment
  124. h_align= 16;
  125. if(s->codec_id == CODEC_ID_MPEG2VIDEO || s->codec_id == CODEC_ID_MJPEG || s->codec_id == CODEC_ID_AMV || s->codec_id == CODEC_ID_THP)
  126. h_align= 32; // interlaced is rounded up to 2 MBs
  127. break;
  128. case PIX_FMT_YUV411P:
  129. case PIX_FMT_UYYVYY411:
  130. w_align=32;
  131. h_align=8;
  132. break;
  133. case PIX_FMT_YUV410P:
  134. if(s->codec_id == CODEC_ID_SVQ1){
  135. w_align=64;
  136. h_align=64;
  137. }
  138. case PIX_FMT_RGB555:
  139. if(s->codec_id == CODEC_ID_RPZA){
  140. w_align=4;
  141. h_align=4;
  142. }
  143. case PIX_FMT_PAL8:
  144. case PIX_FMT_BGR8:
  145. case PIX_FMT_RGB8:
  146. if(s->codec_id == CODEC_ID_SMC){
  147. w_align=4;
  148. h_align=4;
  149. }
  150. break;
  151. case PIX_FMT_BGR24:
  152. if((s->codec_id == CODEC_ID_MSZH) || (s->codec_id == CODEC_ID_ZLIB)){
  153. w_align=4;
  154. h_align=4;
  155. }
  156. break;
  157. default:
  158. w_align= 1;
  159. h_align= 1;
  160. break;
  161. }
  162. *width = FFALIGN(*width , w_align);
  163. *height= FFALIGN(*height, h_align);
  164. if(s->codec_id == CODEC_ID_H264)
  165. *height+=2; // some of the optimized chroma MC reads one line too much
  166. linesize_align[0] =
  167. linesize_align[1] =
  168. linesize_align[2] =
  169. linesize_align[3] = STRIDE_ALIGN;
  170. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  171. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  172. //picture size unneccessarily in some cases. The solution here is not
  173. //pretty and better ideas are welcome!
  174. #if HAVE_MMX
  175. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  176. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  177. s->codec_id == CODEC_ID_VP6A) {
  178. linesize_align[0] =
  179. linesize_align[1] =
  180. linesize_align[2] = 16;
  181. }
  182. #endif
  183. }
  184. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  185. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  186. int linesize_align[4];
  187. int align;
  188. avcodec_align_dimensions2(s, width, height, linesize_align);
  189. align = FFMAX(linesize_align[0], linesize_align[3]);
  190. linesize_align[1] <<= chroma_shift;
  191. linesize_align[2] <<= chroma_shift;
  192. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  193. *width=FFALIGN(*width, align);
  194. }
  195. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  196. if((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
  197. return 0;
  198. av_log(av_log_ctx, AV_LOG_ERROR, "picture size invalid (%ux%u)\n", w, h);
  199. return -1;
  200. }
  201. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  202. int i;
  203. int w= s->width;
  204. int h= s->height;
  205. InternalBuffer *buf;
  206. int *picture_number;
  207. if(pic->data[0]!=NULL) {
  208. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  209. return -1;
  210. }
  211. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  212. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  213. return -1;
  214. }
  215. if(avcodec_check_dimensions(s,w,h))
  216. return -1;
  217. if(s->internal_buffer==NULL){
  218. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  219. }
  220. #if 0
  221. s->internal_buffer= av_fast_realloc(
  222. s->internal_buffer,
  223. &s->internal_buffer_size,
  224. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  225. );
  226. #endif
  227. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  228. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  229. (*picture_number)++;
  230. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  231. for(i=0; i<4; i++){
  232. av_freep(&buf->base[i]);
  233. buf->data[i]= NULL;
  234. }
  235. }
  236. if(buf->base[0]){
  237. pic->age= *picture_number - buf->last_pic_num;
  238. buf->last_pic_num= *picture_number;
  239. }else{
  240. int h_chroma_shift, v_chroma_shift;
  241. int size[4] = {0};
  242. int tmpsize;
  243. int unaligned;
  244. AVPicture picture;
  245. int stride_align[4];
  246. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  247. avcodec_align_dimensions2(s, &w, &h, stride_align);
  248. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  249. w+= EDGE_WIDTH*2;
  250. h+= EDGE_WIDTH*2;
  251. }
  252. do {
  253. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  254. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  255. ff_fill_linesize(&picture, s->pix_fmt, w);
  256. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  257. w += w & ~(w-1);
  258. unaligned = 0;
  259. for (i=0; i<4; i++){
  260. unaligned |= picture.linesize[i] % stride_align[i];
  261. }
  262. } while (unaligned);
  263. tmpsize = ff_fill_pointer(&picture, NULL, s->pix_fmt, h);
  264. if (tmpsize < 0)
  265. return -1;
  266. for (i=0; i<3 && picture.data[i+1]; i++)
  267. size[i] = picture.data[i+1] - picture.data[i];
  268. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  269. buf->last_pic_num= -256*256*256*64;
  270. memset(buf->base, 0, sizeof(buf->base));
  271. memset(buf->data, 0, sizeof(buf->data));
  272. for(i=0; i<4 && size[i]; i++){
  273. const int h_shift= i==0 ? 0 : h_chroma_shift;
  274. const int v_shift= i==0 ? 0 : v_chroma_shift;
  275. buf->linesize[i]= picture.linesize[i];
  276. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  277. if(buf->base[i]==NULL) return -1;
  278. memset(buf->base[i], 128, size[i]);
  279. // no edge if EDEG EMU or not planar YUV
  280. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  281. buf->data[i] = buf->base[i];
  282. else
  283. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  284. }
  285. if(size[1] && !size[2])
  286. ff_set_systematic_pal((uint32_t*)buf->data[1], s->pix_fmt);
  287. buf->width = s->width;
  288. buf->height = s->height;
  289. buf->pix_fmt= s->pix_fmt;
  290. pic->age= 256*256*256*64;
  291. }
  292. pic->type= FF_BUFFER_TYPE_INTERNAL;
  293. for(i=0; i<4; i++){
  294. pic->base[i]= buf->base[i];
  295. pic->data[i]= buf->data[i];
  296. pic->linesize[i]= buf->linesize[i];
  297. }
  298. s->internal_buffer_count++;
  299. pic->reordered_opaque= s->reordered_opaque;
  300. if(s->debug&FF_DEBUG_BUFFERS)
  301. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  302. return 0;
  303. }
  304. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  305. int i;
  306. InternalBuffer *buf, *last;
  307. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  308. assert(s->internal_buffer_count);
  309. buf = NULL; /* avoids warning */
  310. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  311. buf= &((InternalBuffer*)s->internal_buffer)[i];
  312. if(buf->data[0] == pic->data[0])
  313. break;
  314. }
  315. assert(i < s->internal_buffer_count);
  316. s->internal_buffer_count--;
  317. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  318. FFSWAP(InternalBuffer, *buf, *last);
  319. for(i=0; i<4; i++){
  320. pic->data[i]=NULL;
  321. // pic->base[i]=NULL;
  322. }
  323. //printf("R%X\n", pic->opaque);
  324. if(s->debug&FF_DEBUG_BUFFERS)
  325. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  326. }
  327. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  328. AVFrame temp_pic;
  329. int i;
  330. /* If no picture return a new buffer */
  331. if(pic->data[0] == NULL) {
  332. /* We will copy from buffer, so must be readable */
  333. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  334. return s->get_buffer(s, pic);
  335. }
  336. /* If internal buffer type return the same buffer */
  337. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  338. pic->reordered_opaque= s->reordered_opaque;
  339. return 0;
  340. }
  341. /*
  342. * Not internal type and reget_buffer not overridden, emulate cr buffer
  343. */
  344. temp_pic = *pic;
  345. for(i = 0; i < 4; i++)
  346. pic->data[i] = pic->base[i] = NULL;
  347. pic->opaque = NULL;
  348. /* Allocate new frame */
  349. if (s->get_buffer(s, pic))
  350. return -1;
  351. /* Copy image data from old buffer to new buffer */
  352. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  353. s->height);
  354. s->release_buffer(s, &temp_pic); // Release old frame
  355. return 0;
  356. }
  357. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  358. int i;
  359. for(i=0; i<count; i++){
  360. int r= func(c, (char*)arg + i*size);
  361. if(ret) ret[i]= r;
  362. }
  363. return 0;
  364. }
  365. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  366. int i;
  367. for(i=0; i<count; i++){
  368. int r= func(c, arg, i, 0);
  369. if(ret) ret[i]= r;
  370. }
  371. return 0;
  372. }
  373. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  374. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  375. ++fmt;
  376. return fmt[0];
  377. }
  378. void avcodec_get_frame_defaults(AVFrame *pic){
  379. memset(pic, 0, sizeof(AVFrame));
  380. pic->pts= AV_NOPTS_VALUE;
  381. pic->key_frame= 1;
  382. }
  383. AVFrame *avcodec_alloc_frame(void){
  384. AVFrame *pic= av_malloc(sizeof(AVFrame));
  385. if(pic==NULL) return NULL;
  386. avcodec_get_frame_defaults(pic);
  387. return pic;
  388. }
  389. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  390. {
  391. int ret= -1;
  392. /* If there is a user-supplied mutex locking routine, call it. */
  393. if (ff_lockmgr_cb) {
  394. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  395. return -1;
  396. }
  397. entangled_thread_counter++;
  398. if(entangled_thread_counter != 1){
  399. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  400. goto end;
  401. }
  402. if(avctx->codec || !codec)
  403. goto end;
  404. if (codec->priv_data_size > 0) {
  405. avctx->priv_data = av_mallocz(codec->priv_data_size);
  406. if (!avctx->priv_data) {
  407. ret = AVERROR(ENOMEM);
  408. goto end;
  409. }
  410. } else {
  411. avctx->priv_data = NULL;
  412. }
  413. if(avctx->coded_width && avctx->coded_height)
  414. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  415. else if(avctx->width && avctx->height)
  416. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  417. #define SANE_NB_CHANNELS 128U
  418. if (((avctx->coded_width || avctx->coded_height)
  419. && avcodec_check_dimensions(avctx, avctx->coded_width, avctx->coded_height))
  420. || avctx->channels > SANE_NB_CHANNELS) {
  421. ret = AVERROR(EINVAL);
  422. goto free_and_end;
  423. }
  424. avctx->codec = codec;
  425. if ((avctx->codec_type == CODEC_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  426. avctx->codec_id == CODEC_ID_NONE) {
  427. avctx->codec_type = codec->type;
  428. avctx->codec_id = codec->id;
  429. }
  430. if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){
  431. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  432. goto free_and_end;
  433. }
  434. avctx->frame_number = 0;
  435. if(avctx->codec->init){
  436. ret = avctx->codec->init(avctx);
  437. if (ret < 0) {
  438. goto free_and_end;
  439. }
  440. }
  441. ret=0;
  442. end:
  443. entangled_thread_counter--;
  444. /* Release any user-supplied mutex. */
  445. if (ff_lockmgr_cb) {
  446. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  447. }
  448. return ret;
  449. free_and_end:
  450. av_freep(&avctx->priv_data);
  451. avctx->codec= NULL;
  452. goto end;
  453. }
  454. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  455. const short *samples)
  456. {
  457. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  458. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  459. return -1;
  460. }
  461. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  462. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  463. avctx->frame_number++;
  464. return ret;
  465. }else
  466. return 0;
  467. }
  468. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  469. const AVFrame *pict)
  470. {
  471. if(buf_size < FF_MIN_BUFFER_SIZE){
  472. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  473. return -1;
  474. }
  475. if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))
  476. return -1;
  477. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  478. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  479. avctx->frame_number++;
  480. emms_c(); //needed to avoid an emms_c() call before every return;
  481. return ret;
  482. }else
  483. return 0;
  484. }
  485. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  486. const AVSubtitle *sub)
  487. {
  488. int ret;
  489. if(sub->start_display_time) {
  490. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  491. return -1;
  492. }
  493. if(sub->num_rects == 0 || !sub->rects)
  494. return -1;
  495. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  496. avctx->frame_number++;
  497. return ret;
  498. }
  499. #if LIBAVCODEC_VERSION_MAJOR < 53
  500. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  501. int *got_picture_ptr,
  502. const uint8_t *buf, int buf_size)
  503. {
  504. AVPacket avpkt;
  505. av_init_packet(&avpkt);
  506. avpkt.data = buf;
  507. avpkt.size = buf_size;
  508. // HACK for CorePNG to decode as normal PNG by default
  509. avpkt.flags = AV_PKT_FLAG_KEY;
  510. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  511. }
  512. #endif
  513. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  514. int *got_picture_ptr,
  515. AVPacket *avpkt)
  516. {
  517. int ret;
  518. *got_picture_ptr= 0;
  519. if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))
  520. return -1;
  521. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  522. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  523. avpkt);
  524. emms_c(); //needed to avoid an emms_c() call before every return;
  525. if (*got_picture_ptr)
  526. avctx->frame_number++;
  527. }else
  528. ret= 0;
  529. return ret;
  530. }
  531. #if LIBAVCODEC_VERSION_MAJOR < 53
  532. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  533. int *frame_size_ptr,
  534. const uint8_t *buf, int buf_size)
  535. {
  536. AVPacket avpkt;
  537. av_init_packet(&avpkt);
  538. avpkt.data = buf;
  539. avpkt.size = buf_size;
  540. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  541. }
  542. #endif
  543. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  544. int *frame_size_ptr,
  545. AVPacket *avpkt)
  546. {
  547. int ret;
  548. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  549. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  550. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  551. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  552. return -1;
  553. }
  554. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  555. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  556. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  557. return -1;
  558. }
  559. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  560. avctx->frame_number++;
  561. }else{
  562. ret= 0;
  563. *frame_size_ptr=0;
  564. }
  565. return ret;
  566. }
  567. #if LIBAVCODEC_VERSION_MAJOR < 53
  568. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  569. int *got_sub_ptr,
  570. const uint8_t *buf, int buf_size)
  571. {
  572. AVPacket avpkt;
  573. av_init_packet(&avpkt);
  574. avpkt.data = buf;
  575. avpkt.size = buf_size;
  576. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  577. }
  578. #endif
  579. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  580. int *got_sub_ptr,
  581. AVPacket *avpkt)
  582. {
  583. int ret;
  584. *got_sub_ptr = 0;
  585. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  586. if (*got_sub_ptr)
  587. avctx->frame_number++;
  588. return ret;
  589. }
  590. av_cold int avcodec_close(AVCodecContext *avctx)
  591. {
  592. /* If there is a user-supplied mutex locking routine, call it. */
  593. if (ff_lockmgr_cb) {
  594. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  595. return -1;
  596. }
  597. entangled_thread_counter++;
  598. if(entangled_thread_counter != 1){
  599. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  600. entangled_thread_counter--;
  601. return -1;
  602. }
  603. if (HAVE_THREADS && avctx->thread_opaque)
  604. avcodec_thread_free(avctx);
  605. if (avctx->codec && avctx->codec->close)
  606. avctx->codec->close(avctx);
  607. avcodec_default_free_buffers(avctx);
  608. av_freep(&avctx->priv_data);
  609. if(avctx->codec->encode)
  610. av_freep(&avctx->extradata);
  611. avctx->codec = NULL;
  612. entangled_thread_counter--;
  613. /* Release any user-supplied mutex. */
  614. if (ff_lockmgr_cb) {
  615. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  616. }
  617. return 0;
  618. }
  619. AVCodec *avcodec_find_encoder(enum CodecID id)
  620. {
  621. AVCodec *p;
  622. p = first_avcodec;
  623. while (p) {
  624. if (p->encode != NULL && p->id == id)
  625. return p;
  626. p = p->next;
  627. }
  628. return NULL;
  629. }
  630. AVCodec *avcodec_find_encoder_by_name(const char *name)
  631. {
  632. AVCodec *p;
  633. if (!name)
  634. return NULL;
  635. p = first_avcodec;
  636. while (p) {
  637. if (p->encode != NULL && strcmp(name,p->name) == 0)
  638. return p;
  639. p = p->next;
  640. }
  641. return NULL;
  642. }
  643. AVCodec *avcodec_find_decoder(enum CodecID id)
  644. {
  645. AVCodec *p;
  646. p = first_avcodec;
  647. while (p) {
  648. if (p->decode != NULL && p->id == id)
  649. return p;
  650. p = p->next;
  651. }
  652. return NULL;
  653. }
  654. AVCodec *avcodec_find_decoder_by_name(const char *name)
  655. {
  656. AVCodec *p;
  657. if (!name)
  658. return NULL;
  659. p = first_avcodec;
  660. while (p) {
  661. if (p->decode != NULL && strcmp(name,p->name) == 0)
  662. return p;
  663. p = p->next;
  664. }
  665. return NULL;
  666. }
  667. static int get_bit_rate(AVCodecContext *ctx)
  668. {
  669. int bit_rate;
  670. int bits_per_sample;
  671. switch(ctx->codec_type) {
  672. case CODEC_TYPE_VIDEO:
  673. bit_rate = ctx->bit_rate;
  674. break;
  675. case CODEC_TYPE_AUDIO:
  676. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  677. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  678. break;
  679. case CODEC_TYPE_DATA:
  680. bit_rate = ctx->bit_rate;
  681. break;
  682. case CODEC_TYPE_SUBTITLE:
  683. bit_rate = ctx->bit_rate;
  684. break;
  685. case CODEC_TYPE_ATTACHMENT:
  686. bit_rate = ctx->bit_rate;
  687. break;
  688. default:
  689. bit_rate = 0;
  690. break;
  691. }
  692. return bit_rate;
  693. }
  694. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  695. {
  696. const char *codec_name;
  697. AVCodec *p;
  698. char buf1[32];
  699. int bitrate;
  700. AVRational display_aspect_ratio;
  701. if (encode)
  702. p = avcodec_find_encoder(enc->codec_id);
  703. else
  704. p = avcodec_find_decoder(enc->codec_id);
  705. if (p) {
  706. codec_name = p->name;
  707. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  708. /* fake mpeg2 transport stream codec (currently not
  709. registered) */
  710. codec_name = "mpeg2ts";
  711. } else if (enc->codec_name[0] != '\0') {
  712. codec_name = enc->codec_name;
  713. } else {
  714. /* output avi tags */
  715. if( isprint(enc->codec_tag&0xFF) && isprint((enc->codec_tag>>8)&0xFF)
  716. && isprint((enc->codec_tag>>16)&0xFF) && isprint((enc->codec_tag>>24)&0xFF)){
  717. snprintf(buf1, sizeof(buf1), "%c%c%c%c / 0x%04X",
  718. enc->codec_tag & 0xff,
  719. (enc->codec_tag >> 8) & 0xff,
  720. (enc->codec_tag >> 16) & 0xff,
  721. (enc->codec_tag >> 24) & 0xff,
  722. enc->codec_tag);
  723. } else {
  724. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  725. }
  726. codec_name = buf1;
  727. }
  728. switch(enc->codec_type) {
  729. case CODEC_TYPE_VIDEO:
  730. snprintf(buf, buf_size,
  731. "Video: %s%s",
  732. codec_name, enc->mb_decision ? " (hq)" : "");
  733. if (enc->pix_fmt != PIX_FMT_NONE) {
  734. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  735. ", %s",
  736. avcodec_get_pix_fmt_name(enc->pix_fmt));
  737. }
  738. if (enc->width) {
  739. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  740. ", %dx%d",
  741. enc->width, enc->height);
  742. if (enc->sample_aspect_ratio.num) {
  743. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  744. enc->width*enc->sample_aspect_ratio.num,
  745. enc->height*enc->sample_aspect_ratio.den,
  746. 1024*1024);
  747. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  748. " [PAR %d:%d DAR %d:%d]",
  749. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  750. display_aspect_ratio.num, display_aspect_ratio.den);
  751. }
  752. if(av_log_get_level() >= AV_LOG_DEBUG){
  753. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  754. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  755. ", %d/%d",
  756. enc->time_base.num/g, enc->time_base.den/g);
  757. }
  758. }
  759. if (encode) {
  760. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  761. ", q=%d-%d", enc->qmin, enc->qmax);
  762. }
  763. break;
  764. case CODEC_TYPE_AUDIO:
  765. snprintf(buf, buf_size,
  766. "Audio: %s",
  767. codec_name);
  768. if (enc->sample_rate) {
  769. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  770. ", %d Hz", enc->sample_rate);
  771. }
  772. av_strlcat(buf, ", ", buf_size);
  773. avcodec_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  774. if (enc->sample_fmt != SAMPLE_FMT_NONE) {
  775. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  776. ", %s", avcodec_get_sample_fmt_name(enc->sample_fmt));
  777. }
  778. break;
  779. case CODEC_TYPE_DATA:
  780. snprintf(buf, buf_size, "Data: %s", codec_name);
  781. break;
  782. case CODEC_TYPE_SUBTITLE:
  783. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  784. break;
  785. case CODEC_TYPE_ATTACHMENT:
  786. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  787. break;
  788. default:
  789. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  790. return;
  791. }
  792. if (encode) {
  793. if (enc->flags & CODEC_FLAG_PASS1)
  794. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  795. ", pass 1");
  796. if (enc->flags & CODEC_FLAG_PASS2)
  797. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  798. ", pass 2");
  799. }
  800. bitrate = get_bit_rate(enc);
  801. if (bitrate != 0) {
  802. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  803. ", %d kb/s", bitrate / 1000);
  804. }
  805. }
  806. unsigned avcodec_version( void )
  807. {
  808. return LIBAVCODEC_VERSION_INT;
  809. }
  810. const char *avcodec_configuration(void)
  811. {
  812. return FFMPEG_CONFIGURATION;
  813. }
  814. const char *avcodec_license(void)
  815. {
  816. #define LICENSE_PREFIX "libavcodec license: "
  817. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  818. }
  819. void avcodec_init(void)
  820. {
  821. static int initialized = 0;
  822. if (initialized != 0)
  823. return;
  824. initialized = 1;
  825. dsputil_static_init();
  826. }
  827. void avcodec_flush_buffers(AVCodecContext *avctx)
  828. {
  829. if(avctx->codec->flush)
  830. avctx->codec->flush(avctx);
  831. }
  832. void avcodec_default_free_buffers(AVCodecContext *s){
  833. int i, j;
  834. if(s->internal_buffer==NULL) return;
  835. if (s->internal_buffer_count)
  836. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  837. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  838. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  839. for(j=0; j<4; j++){
  840. av_freep(&buf->base[j]);
  841. buf->data[j]= NULL;
  842. }
  843. }
  844. av_freep(&s->internal_buffer);
  845. s->internal_buffer_count=0;
  846. }
  847. char av_get_pict_type_char(int pict_type){
  848. switch(pict_type){
  849. case FF_I_TYPE: return 'I';
  850. case FF_P_TYPE: return 'P';
  851. case FF_B_TYPE: return 'B';
  852. case FF_S_TYPE: return 'S';
  853. case FF_SI_TYPE:return 'i';
  854. case FF_SP_TYPE:return 'p';
  855. case FF_BI_TYPE:return 'b';
  856. default: return '?';
  857. }
  858. }
  859. int av_get_bits_per_sample(enum CodecID codec_id){
  860. switch(codec_id){
  861. case CODEC_ID_ADPCM_SBPRO_2:
  862. return 2;
  863. case CODEC_ID_ADPCM_SBPRO_3:
  864. return 3;
  865. case CODEC_ID_ADPCM_SBPRO_4:
  866. case CODEC_ID_ADPCM_CT:
  867. case CODEC_ID_ADPCM_IMA_WAV:
  868. case CODEC_ID_ADPCM_MS:
  869. case CODEC_ID_ADPCM_YAMAHA:
  870. return 4;
  871. case CODEC_ID_PCM_ALAW:
  872. case CODEC_ID_PCM_MULAW:
  873. case CODEC_ID_PCM_S8:
  874. case CODEC_ID_PCM_U8:
  875. case CODEC_ID_PCM_ZORK:
  876. return 8;
  877. case CODEC_ID_PCM_S16BE:
  878. case CODEC_ID_PCM_S16LE:
  879. case CODEC_ID_PCM_S16LE_PLANAR:
  880. case CODEC_ID_PCM_U16BE:
  881. case CODEC_ID_PCM_U16LE:
  882. return 16;
  883. case CODEC_ID_PCM_S24DAUD:
  884. case CODEC_ID_PCM_S24BE:
  885. case CODEC_ID_PCM_S24LE:
  886. case CODEC_ID_PCM_U24BE:
  887. case CODEC_ID_PCM_U24LE:
  888. return 24;
  889. case CODEC_ID_PCM_S32BE:
  890. case CODEC_ID_PCM_S32LE:
  891. case CODEC_ID_PCM_U32BE:
  892. case CODEC_ID_PCM_U32LE:
  893. case CODEC_ID_PCM_F32BE:
  894. case CODEC_ID_PCM_F32LE:
  895. return 32;
  896. case CODEC_ID_PCM_F64BE:
  897. case CODEC_ID_PCM_F64LE:
  898. return 64;
  899. default:
  900. return 0;
  901. }
  902. }
  903. int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) {
  904. switch (sample_fmt) {
  905. case SAMPLE_FMT_U8:
  906. return 8;
  907. case SAMPLE_FMT_S16:
  908. return 16;
  909. case SAMPLE_FMT_S32:
  910. case SAMPLE_FMT_FLT:
  911. return 32;
  912. case SAMPLE_FMT_DBL:
  913. return 64;
  914. default:
  915. return 0;
  916. }
  917. }
  918. #if !HAVE_THREADS
  919. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  920. s->thread_count = thread_count;
  921. return -1;
  922. }
  923. #endif
  924. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  925. {
  926. unsigned int n = 0;
  927. while(v >= 0xff) {
  928. *s++ = 0xff;
  929. v -= 0xff;
  930. n++;
  931. }
  932. *s = v;
  933. n++;
  934. return n;
  935. }
  936. /* Wrapper to work around the lack of mkstemp() on mingw/cygin.
  937. * Also, tries to create file in /tmp first, if possible.
  938. * *prefix can be a character constant; *filename will be allocated internally.
  939. * Returns file descriptor of opened file (or -1 on error)
  940. * and opened file name in **filename. */
  941. int av_tempfile(char *prefix, char **filename) {
  942. int fd=-1;
  943. #if !HAVE_MKSTEMP
  944. *filename = tempnam(".", prefix);
  945. #else
  946. size_t len = strlen(prefix) + 12; /* room for "/tmp/" and "XXXXXX\0" */
  947. *filename = av_malloc(len);
  948. #endif
  949. /* -----common section-----*/
  950. if (*filename == NULL) {
  951. av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
  952. return -1;
  953. }
  954. #if !HAVE_MKSTEMP
  955. fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
  956. #else
  957. snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
  958. fd = mkstemp(*filename);
  959. if (fd < 0) {
  960. snprintf(*filename, len, "./%sXXXXXX", prefix);
  961. fd = mkstemp(*filename);
  962. }
  963. #endif
  964. /* -----common section-----*/
  965. if (fd < 0) {
  966. av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
  967. return -1;
  968. }
  969. return fd; /* success */
  970. }
  971. typedef struct {
  972. const char *abbr;
  973. int width, height;
  974. } VideoFrameSizeAbbr;
  975. typedef struct {
  976. const char *abbr;
  977. int rate_num, rate_den;
  978. } VideoFrameRateAbbr;
  979. static const VideoFrameSizeAbbr video_frame_size_abbrs[] = {
  980. { "ntsc", 720, 480 },
  981. { "pal", 720, 576 },
  982. { "qntsc", 352, 240 }, /* VCD compliant NTSC */
  983. { "qpal", 352, 288 }, /* VCD compliant PAL */
  984. { "sntsc", 640, 480 }, /* square pixel NTSC */
  985. { "spal", 768, 576 }, /* square pixel PAL */
  986. { "film", 352, 240 },
  987. { "ntsc-film", 352, 240 },
  988. { "sqcif", 128, 96 },
  989. { "qcif", 176, 144 },
  990. { "cif", 352, 288 },
  991. { "4cif", 704, 576 },
  992. { "16cif", 1408,1152 },
  993. { "qqvga", 160, 120 },
  994. { "qvga", 320, 240 },
  995. { "vga", 640, 480 },
  996. { "svga", 800, 600 },
  997. { "xga", 1024, 768 },
  998. { "uxga", 1600,1200 },
  999. { "qxga", 2048,1536 },
  1000. { "sxga", 1280,1024 },
  1001. { "qsxga", 2560,2048 },
  1002. { "hsxga", 5120,4096 },
  1003. { "wvga", 852, 480 },
  1004. { "wxga", 1366, 768 },
  1005. { "wsxga", 1600,1024 },
  1006. { "wuxga", 1920,1200 },
  1007. { "woxga", 2560,1600 },
  1008. { "wqsxga", 3200,2048 },
  1009. { "wquxga", 3840,2400 },
  1010. { "whsxga", 6400,4096 },
  1011. { "whuxga", 7680,4800 },
  1012. { "cga", 320, 200 },
  1013. { "ega", 640, 350 },
  1014. { "hd480", 852, 480 },
  1015. { "hd720", 1280, 720 },
  1016. { "hd1080", 1920,1080 },
  1017. };
  1018. static const VideoFrameRateAbbr video_frame_rate_abbrs[]= {
  1019. { "ntsc", 30000, 1001 },
  1020. { "pal", 25, 1 },
  1021. { "qntsc", 30000, 1001 }, /* VCD compliant NTSC */
  1022. { "qpal", 25, 1 }, /* VCD compliant PAL */
  1023. { "sntsc", 30000, 1001 }, /* square pixel NTSC */
  1024. { "spal", 25, 1 }, /* square pixel PAL */
  1025. { "film", 24, 1 },
  1026. { "ntsc-film", 24000, 1001 },
  1027. };
  1028. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  1029. {
  1030. int i;
  1031. int n = FF_ARRAY_ELEMS(video_frame_size_abbrs);
  1032. char *p;
  1033. int frame_width = 0, frame_height = 0;
  1034. for(i=0;i<n;i++) {
  1035. if (!strcmp(video_frame_size_abbrs[i].abbr, str)) {
  1036. frame_width = video_frame_size_abbrs[i].width;
  1037. frame_height = video_frame_size_abbrs[i].height;
  1038. break;
  1039. }
  1040. }
  1041. if (i == n) {
  1042. p = str;
  1043. frame_width = strtol(p, &p, 10);
  1044. if (*p)
  1045. p++;
  1046. frame_height = strtol(p, &p, 10);
  1047. }
  1048. if (frame_width <= 0 || frame_height <= 0)
  1049. return -1;
  1050. *width_ptr = frame_width;
  1051. *height_ptr = frame_height;
  1052. return 0;
  1053. }
  1054. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  1055. {
  1056. int i;
  1057. int n = FF_ARRAY_ELEMS(video_frame_rate_abbrs);
  1058. char* cp;
  1059. /* First, we check our abbreviation table */
  1060. for (i = 0; i < n; ++i)
  1061. if (!strcmp(video_frame_rate_abbrs[i].abbr, arg)) {
  1062. frame_rate->num = video_frame_rate_abbrs[i].rate_num;
  1063. frame_rate->den = video_frame_rate_abbrs[i].rate_den;
  1064. return 0;
  1065. }
  1066. /* Then, we try to parse it as fraction */
  1067. cp = strchr(arg, '/');
  1068. if (!cp)
  1069. cp = strchr(arg, ':');
  1070. if (cp) {
  1071. char* cpp;
  1072. frame_rate->num = strtol(arg, &cpp, 10);
  1073. if (cpp != arg || cpp == cp)
  1074. frame_rate->den = strtol(cp+1, &cpp, 10);
  1075. else
  1076. frame_rate->num = 0;
  1077. }
  1078. else {
  1079. /* Finally we give up and parse it as double */
  1080. AVRational time_base = av_d2q(strtod(arg, 0), 1001000);
  1081. frame_rate->den = time_base.den;
  1082. frame_rate->num = time_base.num;
  1083. }
  1084. if (!frame_rate->num || !frame_rate->den)
  1085. return -1;
  1086. else
  1087. return 0;
  1088. }
  1089. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1090. int i;
  1091. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1092. return i;
  1093. }
  1094. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1095. {
  1096. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1097. "version to the newest one from SVN. If the problem still "
  1098. "occurs, it means that your file has a feature which has not "
  1099. "been implemented.", feature);
  1100. if(want_sample)
  1101. av_log_ask_for_sample(avc, NULL);
  1102. else
  1103. av_log(avc, AV_LOG_WARNING, "\n");
  1104. }
  1105. void av_log_ask_for_sample(void *avc, const char *msg)
  1106. {
  1107. if (msg)
  1108. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  1109. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1110. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1111. "and contact the ffmpeg-devel mailing list.\n");
  1112. }
  1113. static AVHWAccel *first_hwaccel = NULL;
  1114. void av_register_hwaccel(AVHWAccel *hwaccel)
  1115. {
  1116. AVHWAccel **p = &first_hwaccel;
  1117. while (*p)
  1118. p = &(*p)->next;
  1119. *p = hwaccel;
  1120. hwaccel->next = NULL;
  1121. }
  1122. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1123. {
  1124. return hwaccel ? hwaccel->next : first_hwaccel;
  1125. }
  1126. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1127. {
  1128. AVHWAccel *hwaccel=NULL;
  1129. while((hwaccel= av_hwaccel_next(hwaccel))){
  1130. if ( hwaccel->id == codec_id
  1131. && hwaccel->pix_fmt == pix_fmt)
  1132. return hwaccel;
  1133. }
  1134. return NULL;
  1135. }
  1136. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1137. {
  1138. if (ff_lockmgr_cb) {
  1139. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1140. return -1;
  1141. }
  1142. ff_lockmgr_cb = cb;
  1143. if (ff_lockmgr_cb) {
  1144. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1145. return -1;
  1146. }
  1147. return 0;
  1148. }