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.

1331 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) {
  302. pic->pkt_pts = s->pkt->pts;
  303. pic->pkt_pos = s->pkt->pos;
  304. } else {
  305. pic->pkt_pts = AV_NOPTS_VALUE;
  306. pic->pkt_pos = -1;
  307. }
  308. pic->reordered_opaque= s->reordered_opaque;
  309. if(s->debug&FF_DEBUG_BUFFERS)
  310. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  311. return 0;
  312. }
  313. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  314. int i;
  315. InternalBuffer *buf, *last;
  316. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  317. assert(s->internal_buffer_count);
  318. if(s->internal_buffer){
  319. buf = NULL; /* avoids warning */
  320. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  321. buf= &((InternalBuffer*)s->internal_buffer)[i];
  322. if(buf->data[0] == pic->data[0])
  323. break;
  324. }
  325. assert(i < s->internal_buffer_count);
  326. s->internal_buffer_count--;
  327. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  328. FFSWAP(InternalBuffer, *buf, *last);
  329. }
  330. for(i=0; i<4; i++){
  331. pic->data[i]=NULL;
  332. // pic->base[i]=NULL;
  333. }
  334. //printf("R%X\n", pic->opaque);
  335. if(s->debug&FF_DEBUG_BUFFERS)
  336. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  337. }
  338. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  339. AVFrame temp_pic;
  340. int i;
  341. /* If no picture return a new buffer */
  342. if(pic->data[0] == NULL) {
  343. /* We will copy from buffer, so must be readable */
  344. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  345. return s->get_buffer(s, pic);
  346. }
  347. /* If internal buffer type return the same buffer */
  348. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  349. if(s->pkt) pic->pkt_pts= s->pkt->pts;
  350. else pic->pkt_pts= AV_NOPTS_VALUE;
  351. pic->reordered_opaque= s->reordered_opaque;
  352. return 0;
  353. }
  354. /*
  355. * Not internal type and reget_buffer not overridden, emulate cr buffer
  356. */
  357. temp_pic = *pic;
  358. for(i = 0; i < 4; i++)
  359. pic->data[i] = pic->base[i] = NULL;
  360. pic->opaque = NULL;
  361. /* Allocate new frame */
  362. if (s->get_buffer(s, pic))
  363. return -1;
  364. /* Copy image data from old buffer to new buffer */
  365. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  366. s->height);
  367. s->release_buffer(s, &temp_pic); // Release old frame
  368. return 0;
  369. }
  370. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  371. int i;
  372. for(i=0; i<count; i++){
  373. int r= func(c, (char*)arg + i*size);
  374. if(ret) ret[i]= r;
  375. }
  376. return 0;
  377. }
  378. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  379. int i;
  380. for(i=0; i<count; i++){
  381. int r= func(c, arg, i, 0);
  382. if(ret) ret[i]= r;
  383. }
  384. return 0;
  385. }
  386. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  387. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  388. ++fmt;
  389. return fmt[0];
  390. }
  391. void avcodec_get_frame_defaults(AVFrame *pic){
  392. memset(pic, 0, sizeof(AVFrame));
  393. pic->pts = pic->best_effort_timestamp = AV_NOPTS_VALUE;
  394. pic->pkt_pos = -1;
  395. pic->key_frame= 1;
  396. pic->sample_aspect_ratio = (AVRational){0, 1};
  397. }
  398. AVFrame *avcodec_alloc_frame(void){
  399. AVFrame *pic= av_malloc(sizeof(AVFrame));
  400. if(pic==NULL) return NULL;
  401. avcodec_get_frame_defaults(pic);
  402. return pic;
  403. }
  404. static void avcodec_get_subtitle_defaults(AVSubtitle *sub)
  405. {
  406. memset(sub, 0, sizeof(*sub));
  407. sub->pts = AV_NOPTS_VALUE;
  408. }
  409. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  410. {
  411. int ret = 0;
  412. /* If there is a user-supplied mutex locking routine, call it. */
  413. if (ff_lockmgr_cb) {
  414. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  415. return -1;
  416. }
  417. entangled_thread_counter++;
  418. if(entangled_thread_counter != 1){
  419. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  420. ret = -1;
  421. goto end;
  422. }
  423. if(avctx->codec || !codec) {
  424. ret = AVERROR(EINVAL);
  425. goto end;
  426. }
  427. if (codec->priv_data_size > 0) {
  428. if(!avctx->priv_data){
  429. avctx->priv_data = av_mallocz(codec->priv_data_size);
  430. if (!avctx->priv_data) {
  431. ret = AVERROR(ENOMEM);
  432. goto end;
  433. }
  434. if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
  435. *(AVClass**)avctx->priv_data= codec->priv_class;
  436. av_opt_set_defaults(avctx->priv_data);
  437. }
  438. }
  439. } else {
  440. avctx->priv_data = NULL;
  441. }
  442. if(avctx->coded_width && avctx->coded_height)
  443. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  444. else if(avctx->width && avctx->height)
  445. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  446. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  447. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  448. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  449. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  450. avcodec_set_dimensions(avctx, 0, 0);
  451. }
  452. /* if the decoder init function was already called previously,
  453. free the already allocated subtitle_header before overwriting it */
  454. if (codec->decode)
  455. av_freep(&avctx->subtitle_header);
  456. #define SANE_NB_CHANNELS 128U
  457. if (avctx->channels > SANE_NB_CHANNELS) {
  458. ret = AVERROR(EINVAL);
  459. goto free_and_end;
  460. }
  461. avctx->codec = codec;
  462. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  463. avctx->codec_id == CODEC_ID_NONE) {
  464. avctx->codec_type = codec->type;
  465. avctx->codec_id = codec->id;
  466. }
  467. if (avctx->codec_id != codec->id || (avctx->codec_type != codec->type
  468. && avctx->codec_type != AVMEDIA_TYPE_ATTACHMENT)) {
  469. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  470. ret = AVERROR(EINVAL);
  471. goto free_and_end;
  472. }
  473. avctx->frame_number = 0;
  474. if (HAVE_THREADS && !avctx->thread_opaque) {
  475. ret = ff_thread_init(avctx);
  476. if (ret < 0) {
  477. goto free_and_end;
  478. }
  479. }
  480. if (avctx->codec->max_lowres < avctx->lowres) {
  481. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  482. avctx->codec->max_lowres);
  483. ret = AVERROR(EINVAL);
  484. goto free_and_end;
  485. }
  486. if (avctx->codec->encode) {
  487. int i;
  488. if (avctx->codec->sample_fmts) {
  489. for (i = 0; avctx->codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE; i++)
  490. if (avctx->sample_fmt == avctx->codec->sample_fmts[i])
  491. break;
  492. if (avctx->codec->sample_fmts[i] == AV_SAMPLE_FMT_NONE) {
  493. av_log(avctx, AV_LOG_ERROR, "Specified sample_fmt is not supported.\n");
  494. ret = AVERROR(EINVAL);
  495. goto free_and_end;
  496. }
  497. }
  498. if (avctx->codec->supported_samplerates) {
  499. for (i = 0; avctx->codec->supported_samplerates[i] != 0; i++)
  500. if (avctx->sample_rate == avctx->codec->supported_samplerates[i])
  501. break;
  502. if (avctx->codec->supported_samplerates[i] == 0) {
  503. av_log(avctx, AV_LOG_ERROR, "Specified sample_rate is not supported\n");
  504. ret = AVERROR(EINVAL);
  505. goto free_and_end;
  506. }
  507. }
  508. if (avctx->codec->channel_layouts) {
  509. if (!avctx->channel_layout) {
  510. av_log(avctx, AV_LOG_WARNING, "channel_layout not specified\n");
  511. } else {
  512. for (i = 0; avctx->codec->channel_layouts[i] != 0; i++)
  513. if (avctx->channel_layout == avctx->codec->channel_layouts[i])
  514. break;
  515. if (avctx->codec->channel_layouts[i] == 0) {
  516. av_log(avctx, AV_LOG_ERROR, "Specified channel_layout is not supported\n");
  517. ret = AVERROR(EINVAL);
  518. goto free_and_end;
  519. }
  520. }
  521. }
  522. if (avctx->channel_layout && avctx->channels) {
  523. if (av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels) {
  524. av_log(avctx, AV_LOG_ERROR, "channel layout does not match number of channels\n");
  525. ret = AVERROR(EINVAL);
  526. goto free_and_end;
  527. }
  528. } else if (avctx->channel_layout) {
  529. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  530. }
  531. }
  532. avctx->pts_correction_num_faulty_pts =
  533. avctx->pts_correction_num_faulty_dts = 0;
  534. avctx->pts_correction_last_pts =
  535. avctx->pts_correction_last_dts = INT64_MIN;
  536. if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
  537. ret = avctx->codec->init(avctx);
  538. if (ret < 0) {
  539. goto free_and_end;
  540. }
  541. }
  542. ret=0;
  543. end:
  544. entangled_thread_counter--;
  545. /* Release any user-supplied mutex. */
  546. if (ff_lockmgr_cb) {
  547. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  548. }
  549. return ret;
  550. free_and_end:
  551. av_freep(&avctx->priv_data);
  552. avctx->codec= NULL;
  553. goto end;
  554. }
  555. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  556. const short *samples)
  557. {
  558. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  559. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  560. return -1;
  561. }
  562. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  563. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  564. avctx->frame_number++;
  565. return ret;
  566. }else
  567. return 0;
  568. }
  569. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  570. const AVFrame *pict)
  571. {
  572. if(buf_size < FF_MIN_BUFFER_SIZE){
  573. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  574. return -1;
  575. }
  576. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  577. return -1;
  578. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  579. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  580. avctx->frame_number++;
  581. emms_c(); //needed to avoid an emms_c() call before every return;
  582. return ret;
  583. }else
  584. return 0;
  585. }
  586. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  587. const AVSubtitle *sub)
  588. {
  589. int ret;
  590. if(sub->start_display_time) {
  591. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  592. return -1;
  593. }
  594. if(sub->num_rects == 0 || !sub->rects)
  595. return -1;
  596. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  597. avctx->frame_number++;
  598. return ret;
  599. }
  600. /**
  601. * Attempt to guess proper monotonic timestamps for decoded video frames
  602. * which might have incorrect times. Input timestamps may wrap around, in
  603. * which case the output will as well.
  604. *
  605. * @param pts the pts field of the decoded AVPacket, as passed through
  606. * AVFrame.pkt_pts
  607. * @param dts the dts field of the decoded AVPacket
  608. * @return one of the input values, may be AV_NOPTS_VALUE
  609. */
  610. static int64_t guess_correct_pts(AVCodecContext *ctx,
  611. int64_t reordered_pts, int64_t dts)
  612. {
  613. int64_t pts = AV_NOPTS_VALUE;
  614. if (dts != AV_NOPTS_VALUE) {
  615. ctx->pts_correction_num_faulty_dts += dts <= ctx->pts_correction_last_dts;
  616. ctx->pts_correction_last_dts = dts;
  617. }
  618. if (reordered_pts != AV_NOPTS_VALUE) {
  619. ctx->pts_correction_num_faulty_pts += reordered_pts <= ctx->pts_correction_last_pts;
  620. ctx->pts_correction_last_pts = reordered_pts;
  621. }
  622. if ((ctx->pts_correction_num_faulty_pts<=ctx->pts_correction_num_faulty_dts || dts == AV_NOPTS_VALUE)
  623. && reordered_pts != AV_NOPTS_VALUE)
  624. pts = reordered_pts;
  625. else
  626. pts = dts;
  627. return pts;
  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_PTHREADS && 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. picture->pkt_pos= avpkt->pos;
  647. if (!picture->sample_aspect_ratio.num)
  648. picture->sample_aspect_ratio = avctx->sample_aspect_ratio;
  649. }
  650. emms_c(); //needed to avoid an emms_c() call before every return;
  651. if (*got_picture_ptr){
  652. avctx->frame_number++;
  653. picture->best_effort_timestamp = guess_correct_pts(avctx,
  654. picture->pkt_pts,
  655. picture->pkt_dts);
  656. }
  657. }else
  658. ret= 0;
  659. return ret;
  660. }
  661. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  662. int *frame_size_ptr,
  663. AVPacket *avpkt)
  664. {
  665. int ret;
  666. avctx->pkt = avpkt;
  667. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  668. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  669. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  670. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  671. return -1;
  672. }
  673. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  674. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  675. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  676. return -1;
  677. }
  678. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  679. avctx->frame_number++;
  680. }else{
  681. ret= 0;
  682. *frame_size_ptr=0;
  683. }
  684. return ret;
  685. }
  686. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  687. int *got_sub_ptr,
  688. AVPacket *avpkt)
  689. {
  690. int ret;
  691. avctx->pkt = avpkt;
  692. *got_sub_ptr = 0;
  693. avcodec_get_subtitle_defaults(sub);
  694. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  695. if (*got_sub_ptr)
  696. avctx->frame_number++;
  697. return ret;
  698. }
  699. void avsubtitle_free(AVSubtitle *sub)
  700. {
  701. int i;
  702. for (i = 0; i < sub->num_rects; i++)
  703. {
  704. av_freep(&sub->rects[i]->pict.data[0]);
  705. av_freep(&sub->rects[i]->pict.data[1]);
  706. av_freep(&sub->rects[i]->pict.data[2]);
  707. av_freep(&sub->rects[i]->pict.data[3]);
  708. av_freep(&sub->rects[i]->text);
  709. av_freep(&sub->rects[i]->ass);
  710. av_freep(&sub->rects[i]);
  711. }
  712. av_freep(&sub->rects);
  713. memset(sub, 0, sizeof(AVSubtitle));
  714. }
  715. av_cold int avcodec_close(AVCodecContext *avctx)
  716. {
  717. /* If there is a user-supplied mutex locking routine, call it. */
  718. if (ff_lockmgr_cb) {
  719. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  720. return -1;
  721. }
  722. entangled_thread_counter++;
  723. if(entangled_thread_counter != 1){
  724. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  725. entangled_thread_counter--;
  726. return -1;
  727. }
  728. if (HAVE_THREADS && avctx->thread_opaque)
  729. ff_thread_free(avctx);
  730. if (avctx->codec && avctx->codec->close)
  731. avctx->codec->close(avctx);
  732. avcodec_default_free_buffers(avctx);
  733. avctx->coded_frame = NULL;
  734. 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. avcodec_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 FFMPEG_CONFIGURATION;
  961. }
  962. const char *avcodec_license(void)
  963. {
  964. #define LICENSE_PREFIX "libavcodec license: "
  965. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  966. }
  967. void avcodec_init(void)
  968. {
  969. static int initialized = 0;
  970. if (initialized != 0)
  971. return;
  972. initialized = 1;
  973. dsputil_static_init();
  974. }
  975. void avcodec_flush_buffers(AVCodecContext *avctx)
  976. {
  977. if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
  978. ff_thread_flush(avctx);
  979. if(avctx->codec->flush)
  980. avctx->codec->flush(avctx);
  981. }
  982. void avcodec_default_free_buffers(AVCodecContext *s){
  983. int i, j;
  984. if(s->internal_buffer==NULL) return;
  985. if (s->internal_buffer_count)
  986. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  987. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  988. InternalBuffer *buf= &((InternalBuffer*)s->internal_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(&s->internal_buffer);
  995. s->internal_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_MS:
  1012. case CODEC_ID_ADPCM_YAMAHA:
  1013. return 4;
  1014. case CODEC_ID_ADPCM_G722:
  1015. case CODEC_ID_PCM_ALAW:
  1016. case CODEC_ID_PCM_MULAW:
  1017. case CODEC_ID_PCM_S8:
  1018. case CODEC_ID_PCM_U8:
  1019. case CODEC_ID_PCM_ZORK:
  1020. return 8;
  1021. case CODEC_ID_PCM_S16BE:
  1022. case CODEC_ID_PCM_S16LE:
  1023. case CODEC_ID_PCM_S16LE_PLANAR:
  1024. case CODEC_ID_PCM_U16BE:
  1025. case CODEC_ID_PCM_U16LE:
  1026. return 16;
  1027. case CODEC_ID_PCM_S24DAUD:
  1028. case CODEC_ID_PCM_S24BE:
  1029. case CODEC_ID_PCM_S24LE:
  1030. case CODEC_ID_PCM_U24BE:
  1031. case CODEC_ID_PCM_U24LE:
  1032. return 24;
  1033. case CODEC_ID_PCM_S32BE:
  1034. case CODEC_ID_PCM_S32LE:
  1035. case CODEC_ID_PCM_U32BE:
  1036. case CODEC_ID_PCM_U32LE:
  1037. case CODEC_ID_PCM_F32BE:
  1038. case CODEC_ID_PCM_F32LE:
  1039. return 32;
  1040. case CODEC_ID_PCM_F64BE:
  1041. case CODEC_ID_PCM_F64LE:
  1042. return 64;
  1043. default:
  1044. return 0;
  1045. }
  1046. }
  1047. #if FF_API_OLD_SAMPLE_FMT
  1048. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  1049. return av_get_bits_per_sample_fmt(sample_fmt);
  1050. }
  1051. #endif
  1052. #if !HAVE_THREADS
  1053. int ff_thread_init(AVCodecContext *s){
  1054. return -1;
  1055. }
  1056. #endif
  1057. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  1058. {
  1059. unsigned int n = 0;
  1060. while(v >= 0xff) {
  1061. *s++ = 0xff;
  1062. v -= 0xff;
  1063. n++;
  1064. }
  1065. *s = v;
  1066. n++;
  1067. return n;
  1068. }
  1069. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  1070. int i;
  1071. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  1072. return i;
  1073. }
  1074. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  1075. {
  1076. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  1077. "version to the newest one from Git. If the problem still "
  1078. "occurs, it means that your file has a feature which has not "
  1079. "been implemented.\n", feature);
  1080. if(want_sample)
  1081. av_log_ask_for_sample(avc, NULL);
  1082. }
  1083. void av_log_ask_for_sample(void *avc, const char *msg, ...)
  1084. {
  1085. va_list argument_list;
  1086. va_start(argument_list, msg);
  1087. if (msg)
  1088. av_vlog(avc, AV_LOG_WARNING, msg, argument_list);
  1089. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  1090. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  1091. "and contact the ffmpeg-devel mailing list.\n");
  1092. va_end(argument_list);
  1093. }
  1094. static AVHWAccel *first_hwaccel = NULL;
  1095. void av_register_hwaccel(AVHWAccel *hwaccel)
  1096. {
  1097. AVHWAccel **p = &first_hwaccel;
  1098. while (*p)
  1099. p = &(*p)->next;
  1100. *p = hwaccel;
  1101. hwaccel->next = NULL;
  1102. }
  1103. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1104. {
  1105. return hwaccel ? hwaccel->next : first_hwaccel;
  1106. }
  1107. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1108. {
  1109. AVHWAccel *hwaccel=NULL;
  1110. while((hwaccel= av_hwaccel_next(hwaccel))){
  1111. if ( hwaccel->id == codec_id
  1112. && hwaccel->pix_fmt == pix_fmt)
  1113. return hwaccel;
  1114. }
  1115. return NULL;
  1116. }
  1117. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1118. {
  1119. if (ff_lockmgr_cb) {
  1120. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1121. return -1;
  1122. }
  1123. ff_lockmgr_cb = cb;
  1124. if (ff_lockmgr_cb) {
  1125. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1126. return -1;
  1127. }
  1128. return 0;
  1129. }
  1130. unsigned int ff_toupper4(unsigned int x)
  1131. {
  1132. return toupper( x &0xFF)
  1133. + (toupper((x>>8 )&0xFF)<<8 )
  1134. + (toupper((x>>16)&0xFF)<<16)
  1135. + (toupper((x>>24)&0xFF)<<24);
  1136. }
  1137. #if !HAVE_PTHREADS
  1138. int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
  1139. {
  1140. f->owner = avctx;
  1141. return avctx->get_buffer(avctx, f);
  1142. }
  1143. void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
  1144. {
  1145. f->owner->release_buffer(f->owner, f);
  1146. }
  1147. void ff_thread_finish_setup(AVCodecContext *avctx)
  1148. {
  1149. }
  1150. void ff_thread_report_progress(AVFrame *f, int progress, int field)
  1151. {
  1152. }
  1153. void ff_thread_await_progress(AVFrame *f, int progress, int field)
  1154. {
  1155. }
  1156. #endif
  1157. #if FF_API_THREAD_INIT
  1158. int avcodec_thread_init(AVCodecContext *s, int thread_count)
  1159. {
  1160. s->thread_count = thread_count;
  1161. return ff_thread_init(s);
  1162. }
  1163. #endif