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.

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