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