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.

1363 lines
40KB

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