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.

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