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.

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