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.

1320 lines
38KB

  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((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  651. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  652. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  653. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  654. return -1;
  655. }
  656. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  657. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  658. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  659. return -1;
  660. }
  661. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  662. avctx->frame_number++;
  663. }else{
  664. ret= 0;
  665. *frame_size_ptr=0;
  666. }
  667. return ret;
  668. }
  669. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  670. int *got_sub_ptr,
  671. AVPacket *avpkt)
  672. {
  673. int ret;
  674. avctx->pkt = avpkt;
  675. *got_sub_ptr = 0;
  676. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  677. if (*got_sub_ptr)
  678. avctx->frame_number++;
  679. return ret;
  680. }
  681. void avsubtitle_free(AVSubtitle *sub)
  682. {
  683. int i;
  684. for (i = 0; i < sub->num_rects; i++)
  685. {
  686. av_freep(&sub->rects[i]->pict.data[0]);
  687. av_freep(&sub->rects[i]->pict.data[1]);
  688. av_freep(&sub->rects[i]->pict.data[2]);
  689. av_freep(&sub->rects[i]->pict.data[3]);
  690. av_freep(&sub->rects[i]->text);
  691. av_freep(&sub->rects[i]->ass);
  692. av_freep(&sub->rects[i]);
  693. }
  694. av_freep(&sub->rects);
  695. memset(sub, 0, sizeof(AVSubtitle));
  696. }
  697. av_cold int avcodec_close(AVCodecContext *avctx)
  698. {
  699. /* If there is a user-supplied mutex locking routine, call it. */
  700. if (ff_lockmgr_cb) {
  701. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  702. return -1;
  703. }
  704. entangled_thread_counter++;
  705. if(entangled_thread_counter != 1){
  706. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  707. entangled_thread_counter--;
  708. return -1;
  709. }
  710. if (HAVE_THREADS && avctx->thread_opaque)
  711. ff_thread_free(avctx);
  712. if (avctx->codec && avctx->codec->close)
  713. avctx->codec->close(avctx);
  714. avcodec_default_free_buffers(avctx);
  715. avctx->coded_frame = NULL;
  716. if (avctx->codec && avctx->codec->priv_class)
  717. av_opt_free(avctx->priv_data);
  718. av_opt_free(avctx);
  719. av_freep(&avctx->priv_data);
  720. if(avctx->codec && avctx->codec->encode)
  721. av_freep(&avctx->extradata);
  722. avctx->codec = NULL;
  723. avctx->active_thread_type = 0;
  724. entangled_thread_counter--;
  725. /* Release any user-supplied mutex. */
  726. if (ff_lockmgr_cb) {
  727. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  728. }
  729. return 0;
  730. }
  731. AVCodec *avcodec_find_encoder(enum CodecID id)
  732. {
  733. AVCodec *p, *experimental=NULL;
  734. p = first_avcodec;
  735. while (p) {
  736. if (p->encode != NULL && p->id == id) {
  737. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  738. experimental = p;
  739. } else
  740. return p;
  741. }
  742. p = p->next;
  743. }
  744. return experimental;
  745. }
  746. AVCodec *avcodec_find_encoder_by_name(const char *name)
  747. {
  748. AVCodec *p;
  749. if (!name)
  750. return NULL;
  751. p = first_avcodec;
  752. while (p) {
  753. if (p->encode != NULL && strcmp(name,p->name) == 0)
  754. return p;
  755. p = p->next;
  756. }
  757. return NULL;
  758. }
  759. AVCodec *avcodec_find_decoder(enum CodecID id)
  760. {
  761. AVCodec *p;
  762. p = first_avcodec;
  763. while (p) {
  764. if (p->decode != NULL && p->id == id)
  765. return p;
  766. p = p->next;
  767. }
  768. return NULL;
  769. }
  770. AVCodec *avcodec_find_decoder_by_name(const char *name)
  771. {
  772. AVCodec *p;
  773. if (!name)
  774. return NULL;
  775. p = first_avcodec;
  776. while (p) {
  777. if (p->decode != NULL && strcmp(name,p->name) == 0)
  778. return p;
  779. p = p->next;
  780. }
  781. return NULL;
  782. }
  783. static int get_bit_rate(AVCodecContext *ctx)
  784. {
  785. int bit_rate;
  786. int bits_per_sample;
  787. switch(ctx->codec_type) {
  788. case AVMEDIA_TYPE_VIDEO:
  789. case AVMEDIA_TYPE_DATA:
  790. case AVMEDIA_TYPE_SUBTITLE:
  791. case AVMEDIA_TYPE_ATTACHMENT:
  792. bit_rate = ctx->bit_rate;
  793. break;
  794. case AVMEDIA_TYPE_AUDIO:
  795. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  796. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  797. break;
  798. default:
  799. bit_rate = 0;
  800. break;
  801. }
  802. return bit_rate;
  803. }
  804. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  805. {
  806. int i, len, ret = 0;
  807. for (i = 0; i < 4; i++) {
  808. len = snprintf(buf, buf_size,
  809. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  810. buf += len;
  811. buf_size = buf_size > len ? buf_size - len : 0;
  812. ret += len;
  813. codec_tag>>=8;
  814. }
  815. return ret;
  816. }
  817. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  818. {
  819. const char *codec_name;
  820. const char *profile = NULL;
  821. AVCodec *p;
  822. char buf1[32];
  823. int bitrate;
  824. AVRational display_aspect_ratio;
  825. if (encode)
  826. p = avcodec_find_encoder(enc->codec_id);
  827. else
  828. p = avcodec_find_decoder(enc->codec_id);
  829. if (p) {
  830. codec_name = p->name;
  831. profile = av_get_profile_name(p, enc->profile);
  832. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  833. /* fake mpeg2 transport stream codec (currently not
  834. registered) */
  835. codec_name = "mpeg2ts";
  836. } else if (enc->codec_name[0] != '\0') {
  837. codec_name = enc->codec_name;
  838. } else {
  839. /* output avi tags */
  840. char tag_buf[32];
  841. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  842. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  843. codec_name = buf1;
  844. }
  845. switch(enc->codec_type) {
  846. case AVMEDIA_TYPE_VIDEO:
  847. snprintf(buf, buf_size,
  848. "Video: %s%s",
  849. codec_name, enc->mb_decision ? " (hq)" : "");
  850. if (profile)
  851. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  852. " (%s)", profile);
  853. if (enc->pix_fmt != PIX_FMT_NONE) {
  854. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  855. ", %s",
  856. av_get_pix_fmt_name(enc->pix_fmt));
  857. }
  858. if (enc->width) {
  859. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  860. ", %dx%d",
  861. enc->width, enc->height);
  862. if (enc->sample_aspect_ratio.num) {
  863. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  864. enc->width*enc->sample_aspect_ratio.num,
  865. enc->height*enc->sample_aspect_ratio.den,
  866. 1024*1024);
  867. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  868. " [PAR %d:%d DAR %d:%d]",
  869. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  870. display_aspect_ratio.num, display_aspect_ratio.den);
  871. }
  872. if(av_log_get_level() >= AV_LOG_DEBUG){
  873. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  874. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  875. ", %d/%d",
  876. enc->time_base.num/g, enc->time_base.den/g);
  877. }
  878. }
  879. if (encode) {
  880. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  881. ", q=%d-%d", enc->qmin, enc->qmax);
  882. }
  883. break;
  884. case AVMEDIA_TYPE_AUDIO:
  885. snprintf(buf, buf_size,
  886. "Audio: %s",
  887. codec_name);
  888. if (profile)
  889. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  890. " (%s)", profile);
  891. if (enc->sample_rate) {
  892. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  893. ", %d Hz", enc->sample_rate);
  894. }
  895. av_strlcat(buf, ", ", buf_size);
  896. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  897. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  898. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  899. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  900. }
  901. break;
  902. case AVMEDIA_TYPE_DATA:
  903. snprintf(buf, buf_size, "Data: %s", codec_name);
  904. break;
  905. case AVMEDIA_TYPE_SUBTITLE:
  906. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  907. break;
  908. case AVMEDIA_TYPE_ATTACHMENT:
  909. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  910. break;
  911. default:
  912. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  913. return;
  914. }
  915. if (encode) {
  916. if (enc->flags & CODEC_FLAG_PASS1)
  917. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  918. ", pass 1");
  919. if (enc->flags & CODEC_FLAG_PASS2)
  920. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  921. ", pass 2");
  922. }
  923. bitrate = get_bit_rate(enc);
  924. if (bitrate != 0) {
  925. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  926. ", %d kb/s", bitrate / 1000);
  927. }
  928. }
  929. const char *av_get_profile_name(const AVCodec *codec, int profile)
  930. {
  931. const AVProfile *p;
  932. if (profile == FF_PROFILE_UNKNOWN || !codec->profiles)
  933. return NULL;
  934. for (p = codec->profiles; p->profile != FF_PROFILE_UNKNOWN; p++)
  935. if (p->profile == profile)
  936. return p->name;
  937. return NULL;
  938. }
  939. unsigned avcodec_version( void )
  940. {
  941. return LIBAVCODEC_VERSION_INT;
  942. }
  943. const char *avcodec_configuration(void)
  944. {
  945. return LIBAV_CONFIGURATION;
  946. }
  947. const char *avcodec_license(void)
  948. {
  949. #define LICENSE_PREFIX "libavcodec license: "
  950. return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  951. }
  952. void avcodec_flush_buffers(AVCodecContext *avctx)
  953. {
  954. if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  955. ff_thread_flush(avctx);
  956. else if(avctx->codec->flush)
  957. avctx->codec->flush(avctx);
  958. }
  959. void avcodec_default_free_buffers(AVCodecContext *s){
  960. int i, j;
  961. if(s->internal_buffer==NULL) return;
  962. if (s->internal_buffer_count)
  963. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  964. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  965. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  966. for(j=0; j<4; j++){
  967. av_freep(&buf->base[j]);
  968. buf->data[j]= NULL;
  969. }
  970. }
  971. av_freep(&s->internal_buffer);
  972. s->internal_buffer_count=0;
  973. }
  974. #if FF_API_OLD_FF_PICT_TYPES
  975. char av_get_pict_type_char(int pict_type){
  976. return av_get_picture_type_char(pict_type);
  977. }
  978. #endif
  979. int av_get_bits_per_sample(enum CodecID codec_id){
  980. switch(codec_id){
  981. case CODEC_ID_ADPCM_SBPRO_2:
  982. return 2;
  983. case CODEC_ID_ADPCM_SBPRO_3:
  984. return 3;
  985. case CODEC_ID_ADPCM_SBPRO_4:
  986. case CODEC_ID_ADPCM_CT:
  987. case CODEC_ID_ADPCM_IMA_WAV:
  988. case CODEC_ID_ADPCM_IMA_QT:
  989. case CODEC_ID_ADPCM_SWF:
  990. case CODEC_ID_ADPCM_MS:
  991. case CODEC_ID_ADPCM_YAMAHA:
  992. return 4;
  993. case CODEC_ID_ADPCM_G722:
  994. case CODEC_ID_PCM_ALAW:
  995. case CODEC_ID_PCM_MULAW:
  996. case CODEC_ID_PCM_S8:
  997. case CODEC_ID_PCM_U8:
  998. case CODEC_ID_PCM_ZORK:
  999. return 8;
  1000. case CODEC_ID_PCM_S16BE:
  1001. case CODEC_ID_PCM_S16LE:
  1002. case CODEC_ID_PCM_S16LE_PLANAR:
  1003. case CODEC_ID_PCM_U16BE:
  1004. case CODEC_ID_PCM_U16LE:
  1005. return 16;
  1006. case CODEC_ID_PCM_S24DAUD:
  1007. case CODEC_ID_PCM_S24BE:
  1008. case CODEC_ID_PCM_S24LE:
  1009. case CODEC_ID_PCM_U24BE:
  1010. case CODEC_ID_PCM_U24LE:
  1011. return 24;
  1012. case CODEC_ID_PCM_S32BE:
  1013. case CODEC_ID_PCM_S32LE:
  1014. case CODEC_ID_PCM_U32BE:
  1015. case CODEC_ID_PCM_U32LE:
  1016. case CODEC_ID_PCM_F32BE:
  1017. case CODEC_ID_PCM_F32LE:
  1018. return 32;
  1019. case CODEC_ID_PCM_F64BE:
  1020. case CODEC_ID_PCM_F64LE:
  1021. return 64;
  1022. default:
  1023. return 0;
  1024. }
  1025. }
  1026. #if FF_API_OLD_SAMPLE_FMT
  1027. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1028. return av_get_bytes_per_sample(sample_fmt) << 3;
  1029. }
  1030. #endif
  1031. #if !HAVE_THREADS
  1032. int ff_thread_init(AVCodecContext *s){
  1033. return -1;
  1034. }
  1035. #endif
  1036. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1037. {
  1038. unsigned int n = 0;
  1039. while(v >= 0xff) {
  1040. *s++ = 0xff;
  1041. v -= 0xff;
  1042. n++;
  1043. }
  1044. *s = v;
  1045. n++;
  1046. return n;
  1047. }
  1048. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1049. int i;
  1050. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1051. return i;
  1052. }
  1053. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1054. {
  1055. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your Libav "
  1056. "version to the newest one from Git. If the problem still "
  1057. "occurs, it means that your file has a feature which has not "
  1058. "been implemented.\n", feature);
  1059. if(want_sample)
  1060. av_log_ask_for_sample(avc, NULL);
  1061. }
  1062. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1063. {
  1064. va_list argument_list;
  1065. va_start(argument_list, msg);
  1066. if (msg)
  1067. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1068. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1069. "of this file to ftp://upload.libav.org/incoming/ "
  1070. "and contact the libav-devel mailing list.\n");
  1071. va_end(argument_list);
  1072. }
  1073. static AVHWAccel *first_hwaccel = NULL;
  1074. void av_register_hwaccel(AVHWAccel *hwaccel)
  1075. {
  1076. AVHWAccel **p = &first_hwaccel;
  1077. while (*p)
  1078. p = &(*p)->next;
  1079. *p = hwaccel;
  1080. hwaccel->next = NULL;
  1081. }
  1082. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1083. {
  1084. return hwaccel ? hwaccel->next : first_hwaccel;
  1085. }
  1086. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1087. {
  1088. AVHWAccel *hwaccel=NULL;
  1089. while((hwaccel= av_hwaccel_next(hwaccel))){
  1090. if ( hwaccel->id == codec_id
  1091. && hwaccel->pix_fmt == pix_fmt)
  1092. return hwaccel;
  1093. }
  1094. return NULL;
  1095. }
  1096. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1097. {
  1098. if (ff_lockmgr_cb) {
  1099. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1100. return -1;
  1101. }
  1102. ff_lockmgr_cb = cb;
  1103. if (ff_lockmgr_cb) {
  1104. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1105. return -1;
  1106. }
  1107. return 0;
  1108. }
  1109. unsigned int ff_toupper4(unsigned int x)
  1110. {
  1111. return toupper( x &0xFF)
  1112. + (toupper((x>>8 )&0xFF)<<8 )
  1113. + (toupper((x>>16)&0xFF)<<16)
  1114. + (toupper((x>>24)&0xFF)<<24);
  1115. }
  1116. #if !HAVE_PTHREADS
  1117. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1118. {
  1119. f->owner = avctx;
  1120. return avctx->get_buffer(avctx, f);
  1121. }
  1122. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1123. {
  1124. f->owner->release_buffer(f->owner, f);
  1125. }
  1126. void ff_thread_finish_setup(AVCodecContext *avctx)
  1127. {
  1128. }
  1129. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1130. {
  1131. }
  1132. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1133. {
  1134. }
  1135. #endif
  1136. #if FF_API_THREAD_INIT
  1137. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1138. {
  1139. s->thread_count = thread_count;
  1140. return ff_thread_init(s);
  1141. }
  1142. #endif
  1143. enum AVMediaType avcodec_get_type(enum CodecID codec_id)
  1144. {
  1145. if (codec_id <= CODEC_ID_NONE)
  1146. return AVMEDIA_TYPE_UNKNOWN;
  1147. else if (codec_id < CODEC_ID_FIRST_AUDIO)
  1148. return AVMEDIA_TYPE_VIDEO;
  1149. else if (codec_id < CODEC_ID_FIRST_SUBTITLE)
  1150. return AVMEDIA_TYPE_AUDIO;
  1151. else if (codec_id < CODEC_ID_FIRST_UNKNOWN)
  1152. return AVMEDIA_TYPE_SUBTITLE;
  1153. return AVMEDIA_TYPE_UNKNOWN;
  1154. }