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.

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