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.

1325 lines
39KB

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