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.

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