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.

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