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.

1192 lines
34KB

  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 "libavcore/audioconvert.h"
  31. #include "libavcore/imgutils.h"
  32. #include "libavcore/internal.h"
  33. #include "libavcore/samplefmt.h"
  34. #include "avcodec.h"
  35. #include "dsputil.h"
  36. #include "libavutil/opt.h"
  37. #include "imgconvert.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. 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. 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)
  169. *height+=2; // some of the optimized chroma MC reads one line too much
  170. linesize_align[0] =
  171. linesize_align[1] =
  172. linesize_align[2] =
  173. linesize_align[3] = STRIDE_ALIGN;
  174. //STRIDE_ALIGN is 8 for SSE* but this does not work for SVQ1 chroma planes
  175. //we could change STRIDE_ALIGN to 16 for x86/sse but it would increase the
  176. //picture size unneccessarily in some cases. The solution here is not
  177. //pretty and better ideas are welcome!
  178. #if HAVE_MMX
  179. if(s->codec_id == CODEC_ID_SVQ1 || s->codec_id == CODEC_ID_VP5 ||
  180. s->codec_id == CODEC_ID_VP6 || s->codec_id == CODEC_ID_VP6F ||
  181. s->codec_id == CODEC_ID_VP6A) {
  182. linesize_align[0] =
  183. linesize_align[1] =
  184. linesize_align[2] = 16;
  185. }
  186. #endif
  187. }
  188. void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height){
  189. int chroma_shift = av_pix_fmt_descriptors[s->pix_fmt].log2_chroma_w;
  190. int linesize_align[4];
  191. int align;
  192. avcodec_align_dimensions2(s, width, height, linesize_align);
  193. align = FFMAX(linesize_align[0], linesize_align[3]);
  194. linesize_align[1] <<= chroma_shift;
  195. linesize_align[2] <<= chroma_shift;
  196. align = FFMAX3(align, linesize_align[1], linesize_align[2]);
  197. *width=FFALIGN(*width, align);
  198. }
  199. #if LIBAVCODEC_VERSION_MAJOR < 53
  200. int avcodec_check_dimensions(void *av_log_ctx, unsigned int w, unsigned int h){
  201. return av_image_check_size(w, h, 0, av_log_ctx);
  202. }
  203. #endif
  204. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  205. int i;
  206. int w= s->width;
  207. int h= s->height;
  208. InternalBuffer *buf;
  209. int *picture_number;
  210. if(pic->data[0]!=NULL) {
  211. av_log(s, AV_LOG_ERROR, "pic->data[0]!=NULL in avcodec_default_get_buffer\n");
  212. return -1;
  213. }
  214. if(s->internal_buffer_count >= INTERNAL_BUFFER_SIZE) {
  215. av_log(s, AV_LOG_ERROR, "internal_buffer_count overflow (missing release_buffer?)\n");
  216. return -1;
  217. }
  218. if(av_image_check_size(w, h, 0, s))
  219. return -1;
  220. if(s->internal_buffer==NULL){
  221. s->internal_buffer= av_mallocz((INTERNAL_BUFFER_SIZE+1)*sizeof(InternalBuffer));
  222. }
  223. #if 0
  224. s->internal_buffer= av_fast_realloc(
  225. s->internal_buffer,
  226. &s->internal_buffer_size,
  227. sizeof(InternalBuffer)*FFMAX(99, s->internal_buffer_count+1)/*FIXME*/
  228. );
  229. #endif
  230. buf= &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  231. picture_number= &(((InternalBuffer*)s->internal_buffer)[INTERNAL_BUFFER_SIZE]).last_pic_num; //FIXME ugly hack
  232. (*picture_number)++;
  233. if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
  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. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  250. avcodec_align_dimensions2(s, &w, &h, stride_align);
  251. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  252. w+= EDGE_WIDTH*2;
  253. h+= EDGE_WIDTH*2;
  254. }
  255. do {
  256. // NOTE: do not align linesizes individually, this breaks e.g. assumptions
  257. // that linesize[0] == 2*linesize[1] in the MPEG-encoder for 4:2:2
  258. av_image_fill_linesizes(picture.linesize, s->pix_fmt, w);
  259. // increase alignment of w for next try (rhs gives the lowest bit set in w)
  260. w += w & ~(w-1);
  261. unaligned = 0;
  262. for (i=0; i<4; i++){
  263. unaligned |= picture.linesize[i] % stride_align[i];
  264. }
  265. } while (unaligned);
  266. tmpsize = av_image_fill_pointers(picture.data, s->pix_fmt, h, NULL, picture.linesize);
  267. if (tmpsize < 0)
  268. return -1;
  269. for (i=0; i<3 && picture.data[i+1]; i++)
  270. size[i] = picture.data[i+1] - picture.data[i];
  271. size[i] = tmpsize - (picture.data[i] - picture.data[0]);
  272. buf->last_pic_num= -256*256*256*64;
  273. memset(buf->base, 0, sizeof(buf->base));
  274. memset(buf->data, 0, sizeof(buf->data));
  275. for(i=0; i<4 && size[i]; i++){
  276. const int h_shift= i==0 ? 0 : h_chroma_shift;
  277. const int v_shift= i==0 ? 0 : v_chroma_shift;
  278. buf->linesize[i]= picture.linesize[i];
  279. buf->base[i]= av_malloc(size[i]+16); //FIXME 16
  280. if(buf->base[i]==NULL) return -1;
  281. memset(buf->base[i], 128, size[i]);
  282. // no edge if EDGE EMU or not planar YUV
  283. if((s->flags&CODEC_FLAG_EMU_EDGE) || !size[2])
  284. buf->data[i] = buf->base[i];
  285. else
  286. buf->data[i] = buf->base[i] + FFALIGN((buf->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift), stride_align[i]);
  287. }
  288. if(size[1] && !size[2])
  289. ff_set_systematic_pal2((uint32_t*)buf->data[1], s->pix_fmt);
  290. buf->width = s->width;
  291. buf->height = s->height;
  292. buf->pix_fmt= s->pix_fmt;
  293. pic->age= 256*256*256*64;
  294. }
  295. pic->type= FF_BUFFER_TYPE_INTERNAL;
  296. for(i=0; i<4; i++){
  297. pic->base[i]= buf->base[i];
  298. pic->data[i]= buf->data[i];
  299. pic->linesize[i]= buf->linesize[i];
  300. }
  301. s->internal_buffer_count++;
  302. pic->reordered_opaque= s->reordered_opaque;
  303. if(s->debug&FF_DEBUG_BUFFERS)
  304. av_log(s, AV_LOG_DEBUG, "default_get_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  305. return 0;
  306. }
  307. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  308. int i;
  309. InternalBuffer *buf, *last;
  310. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  311. assert(s->internal_buffer_count);
  312. buf = NULL; /* avoids warning */
  313. for(i=0; i<s->internal_buffer_count; i++){ //just 3-5 checks so is not worth to optimize
  314. buf= &((InternalBuffer*)s->internal_buffer)[i];
  315. if(buf->data[0] == pic->data[0])
  316. break;
  317. }
  318. assert(i < s->internal_buffer_count);
  319. s->internal_buffer_count--;
  320. last = &((InternalBuffer*)s->internal_buffer)[s->internal_buffer_count];
  321. FFSWAP(InternalBuffer, *buf, *last);
  322. for(i=0; i<4; i++){
  323. pic->data[i]=NULL;
  324. // pic->base[i]=NULL;
  325. }
  326. //printf("R%X\n", pic->opaque);
  327. if(s->debug&FF_DEBUG_BUFFERS)
  328. av_log(s, AV_LOG_DEBUG, "default_release_buffer called on pic %p, %d buffers used\n", pic, s->internal_buffer_count);
  329. }
  330. int avcodec_default_reget_buffer(AVCodecContext *s, AVFrame *pic){
  331. AVFrame temp_pic;
  332. int i;
  333. /* If no picture return a new buffer */
  334. if(pic->data[0] == NULL) {
  335. /* We will copy from buffer, so must be readable */
  336. pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
  337. return s->get_buffer(s, pic);
  338. }
  339. /* If internal buffer type return the same buffer */
  340. if(pic->type == FF_BUFFER_TYPE_INTERNAL) {
  341. pic->reordered_opaque= s->reordered_opaque;
  342. return 0;
  343. }
  344. /*
  345. * Not internal type and reget_buffer not overridden, emulate cr buffer
  346. */
  347. temp_pic = *pic;
  348. for(i = 0; i < 4; i++)
  349. pic->data[i] = pic->base[i] = NULL;
  350. pic->opaque = NULL;
  351. /* Allocate new frame */
  352. if (s->get_buffer(s, pic))
  353. return -1;
  354. /* Copy image data from old buffer to new buffer */
  355. av_picture_copy((AVPicture*)pic, (AVPicture*)&temp_pic, s->pix_fmt, s->width,
  356. s->height);
  357. s->release_buffer(s, &temp_pic); // Release old frame
  358. return 0;
  359. }
  360. int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size){
  361. int i;
  362. for(i=0; i<count; i++){
  363. int r= func(c, (char*)arg + i*size);
  364. if(ret) ret[i]= r;
  365. }
  366. return 0;
  367. }
  368. int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int jobnr, int threadnr),void *arg, int *ret, int count){
  369. int i;
  370. for(i=0; i<count; i++){
  371. int r= func(c, arg, i, 0);
  372. if(ret) ret[i]= r;
  373. }
  374. return 0;
  375. }
  376. enum PixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum PixelFormat *fmt){
  377. while (*fmt != PIX_FMT_NONE && ff_is_hwaccel_pix_fmt(*fmt))
  378. ++fmt;
  379. return fmt[0];
  380. }
  381. void avcodec_get_frame_defaults(AVFrame *pic){
  382. memset(pic, 0, sizeof(AVFrame));
  383. pic->pts= AV_NOPTS_VALUE;
  384. pic->key_frame= 1;
  385. }
  386. AVFrame *avcodec_alloc_frame(void){
  387. AVFrame *pic= av_malloc(sizeof(AVFrame));
  388. if(pic==NULL) return NULL;
  389. avcodec_get_frame_defaults(pic);
  390. return pic;
  391. }
  392. int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  393. {
  394. int ret= -1;
  395. /* If there is a user-supplied mutex locking routine, call it. */
  396. if (ff_lockmgr_cb) {
  397. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  398. return -1;
  399. }
  400. entangled_thread_counter++;
  401. if(entangled_thread_counter != 1){
  402. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  403. goto end;
  404. }
  405. if(avctx->codec || !codec)
  406. goto end;
  407. if (codec->priv_data_size > 0) {
  408. if(!avctx->priv_data){
  409. avctx->priv_data = av_mallocz(codec->priv_data_size);
  410. if (!avctx->priv_data) {
  411. ret = AVERROR(ENOMEM);
  412. goto end;
  413. }
  414. if(codec->priv_class){ //this can be droped once all user apps use avcodec_get_context_defaults3()
  415. *(AVClass**)avctx->priv_data= codec->priv_class;
  416. av_opt_set_defaults(avctx->priv_data);
  417. }
  418. }
  419. } else {
  420. avctx->priv_data = NULL;
  421. }
  422. if(avctx->coded_width && avctx->coded_height)
  423. avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
  424. else if(avctx->width && avctx->height)
  425. avcodec_set_dimensions(avctx, avctx->width, avctx->height);
  426. if ((avctx->coded_width || avctx->coded_height || avctx->width || avctx->height)
  427. && ( av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx) < 0
  428. || av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0)) {
  429. av_log(avctx, AV_LOG_WARNING, "ignoring invalid width/height values\n");
  430. avcodec_set_dimensions(avctx, 0, 0);
  431. }
  432. #define SANE_NB_CHANNELS 128U
  433. if (avctx->channels > SANE_NB_CHANNELS) {
  434. ret = AVERROR(EINVAL);
  435. goto free_and_end;
  436. }
  437. avctx->codec = codec;
  438. if ((avctx->codec_type == AVMEDIA_TYPE_UNKNOWN || avctx->codec_type == codec->type) &&
  439. avctx->codec_id == CODEC_ID_NONE) {
  440. avctx->codec_type = codec->type;
  441. avctx->codec_id = codec->id;
  442. }
  443. if(avctx->codec_id != codec->id || avctx->codec_type != codec->type){
  444. av_log(avctx, AV_LOG_ERROR, "codec type or id mismatches\n");
  445. goto free_and_end;
  446. }
  447. avctx->frame_number = 0;
  448. if (avctx->codec->max_lowres < avctx->lowres) {
  449. av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
  450. avctx->codec->max_lowres);
  451. goto free_and_end;
  452. }
  453. if(avctx->codec->init){
  454. ret = avctx->codec->init(avctx);
  455. if (ret < 0) {
  456. goto free_and_end;
  457. }
  458. }
  459. ret=0;
  460. end:
  461. entangled_thread_counter--;
  462. /* Release any user-supplied mutex. */
  463. if (ff_lockmgr_cb) {
  464. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  465. }
  466. return ret;
  467. free_and_end:
  468. av_freep(&avctx->priv_data);
  469. avctx->codec= NULL;
  470. goto end;
  471. }
  472. int attribute_align_arg avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  473. const short *samples)
  474. {
  475. if(buf_size < FF_MIN_BUFFER_SIZE && 0){
  476. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  477. return -1;
  478. }
  479. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){
  480. int ret = avctx->codec->encode(avctx, buf, buf_size, samples);
  481. avctx->frame_number++;
  482. return ret;
  483. }else
  484. return 0;
  485. }
  486. int attribute_align_arg avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  487. const AVFrame *pict)
  488. {
  489. if(buf_size < FF_MIN_BUFFER_SIZE){
  490. av_log(avctx, AV_LOG_ERROR, "buffer smaller than minimum size\n");
  491. return -1;
  492. }
  493. if(av_image_check_size(avctx->width, avctx->height, 0, avctx))
  494. return -1;
  495. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){
  496. int ret = avctx->codec->encode(avctx, buf, buf_size, pict);
  497. avctx->frame_number++;
  498. emms_c(); //needed to avoid an emms_c() call before every return;
  499. return ret;
  500. }else
  501. return 0;
  502. }
  503. int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  504. const AVSubtitle *sub)
  505. {
  506. int ret;
  507. if(sub->start_display_time) {
  508. av_log(avctx, AV_LOG_ERROR, "start_display_time must be 0.\n");
  509. return -1;
  510. }
  511. if(sub->num_rects == 0 || !sub->rects)
  512. return -1;
  513. ret = avctx->codec->encode(avctx, buf, buf_size, sub);
  514. avctx->frame_number++;
  515. return ret;
  516. }
  517. #if FF_API_VIDEO_OLD
  518. int attribute_align_arg avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  519. int *got_picture_ptr,
  520. const uint8_t *buf, int buf_size)
  521. {
  522. AVPacket avpkt;
  523. av_init_packet(&avpkt);
  524. avpkt.data = buf;
  525. avpkt.size = buf_size;
  526. // HACK for CorePNG to decode as normal PNG by default
  527. avpkt.flags = AV_PKT_FLAG_KEY;
  528. return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
  529. }
  530. #endif
  531. int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
  532. int *got_picture_ptr,
  533. AVPacket *avpkt)
  534. {
  535. int ret;
  536. *got_picture_ptr= 0;
  537. if((avctx->coded_width||avctx->coded_height) && av_image_check_size(avctx->coded_width, avctx->coded_height, 0, avctx))
  538. return -1;
  539. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  540. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  541. avpkt);
  542. emms_c(); //needed to avoid an emms_c() call before every return;
  543. if (*got_picture_ptr)
  544. avctx->frame_number++;
  545. }else
  546. ret= 0;
  547. return ret;
  548. }
  549. #if FF_API_AUDIO_OLD
  550. int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
  551. int *frame_size_ptr,
  552. const uint8_t *buf, int buf_size)
  553. {
  554. AVPacket avpkt;
  555. av_init_packet(&avpkt);
  556. avpkt.data = buf;
  557. avpkt.size = buf_size;
  558. return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
  559. }
  560. #endif
  561. int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
  562. int *frame_size_ptr,
  563. AVPacket *avpkt)
  564. {
  565. int ret;
  566. if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
  567. //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
  568. if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
  569. av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
  570. return -1;
  571. }
  572. if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
  573. *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
  574. av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
  575. return -1;
  576. }
  577. ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
  578. avctx->frame_number++;
  579. }else{
  580. ret= 0;
  581. *frame_size_ptr=0;
  582. }
  583. return ret;
  584. }
  585. #if FF_API_SUBTITLE_OLD
  586. int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,
  587. int *got_sub_ptr,
  588. const uint8_t *buf, int buf_size)
  589. {
  590. AVPacket avpkt;
  591. av_init_packet(&avpkt);
  592. avpkt.data = buf;
  593. avpkt.size = buf_size;
  594. return avcodec_decode_subtitle2(avctx, sub, got_sub_ptr, &avpkt);
  595. }
  596. #endif
  597. int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub,
  598. int *got_sub_ptr,
  599. AVPacket *avpkt)
  600. {
  601. int ret;
  602. *got_sub_ptr = 0;
  603. ret = avctx->codec->decode(avctx, sub, got_sub_ptr, avpkt);
  604. if (*got_sub_ptr)
  605. avctx->frame_number++;
  606. return ret;
  607. }
  608. void avsubtitle_free(AVSubtitle *sub)
  609. {
  610. int i;
  611. for (i = 0; i < sub->num_rects; i++)
  612. {
  613. av_freep(&sub->rects[i]->pict.data[0]);
  614. av_freep(&sub->rects[i]->pict.data[1]);
  615. av_freep(&sub->rects[i]->pict.data[2]);
  616. av_freep(&sub->rects[i]->pict.data[3]);
  617. av_freep(&sub->rects[i]->text);
  618. av_freep(&sub->rects[i]->ass);
  619. av_freep(&sub->rects[i]);
  620. }
  621. av_freep(&sub->rects);
  622. memset(sub, 0, sizeof(AVSubtitle));
  623. }
  624. av_cold int avcodec_close(AVCodecContext *avctx)
  625. {
  626. /* If there is a user-supplied mutex locking routine, call it. */
  627. if (ff_lockmgr_cb) {
  628. if ((*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_OBTAIN))
  629. return -1;
  630. }
  631. entangled_thread_counter++;
  632. if(entangled_thread_counter != 1){
  633. av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");
  634. entangled_thread_counter--;
  635. return -1;
  636. }
  637. if (HAVE_THREADS && avctx->thread_opaque)
  638. avcodec_thread_free(avctx);
  639. if (avctx->codec && avctx->codec->close)
  640. avctx->codec->close(avctx);
  641. avcodec_default_free_buffers(avctx);
  642. avctx->coded_frame = NULL;
  643. av_freep(&avctx->priv_data);
  644. if(avctx->codec && avctx->codec->encode)
  645. av_freep(&avctx->extradata);
  646. avctx->codec = NULL;
  647. entangled_thread_counter--;
  648. /* Release any user-supplied mutex. */
  649. if (ff_lockmgr_cb) {
  650. (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
  651. }
  652. return 0;
  653. }
  654. AVCodec *avcodec_find_encoder(enum CodecID id)
  655. {
  656. AVCodec *p, *experimental=NULL;
  657. p = first_avcodec;
  658. while (p) {
  659. if (p->encode != NULL && p->id == id) {
  660. if (p->capabilities & CODEC_CAP_EXPERIMENTAL && !experimental) {
  661. experimental = p;
  662. } else
  663. return p;
  664. }
  665. p = p->next;
  666. }
  667. return experimental;
  668. }
  669. AVCodec *avcodec_find_encoder_by_name(const char *name)
  670. {
  671. AVCodec *p;
  672. if (!name)
  673. return NULL;
  674. p = first_avcodec;
  675. while (p) {
  676. if (p->encode != NULL && strcmp(name,p->name) == 0)
  677. return p;
  678. p = p->next;
  679. }
  680. return NULL;
  681. }
  682. AVCodec *avcodec_find_decoder(enum CodecID id)
  683. {
  684. AVCodec *p;
  685. p = first_avcodec;
  686. while (p) {
  687. if (p->decode != NULL && p->id == id)
  688. return p;
  689. p = p->next;
  690. }
  691. return NULL;
  692. }
  693. AVCodec *avcodec_find_decoder_by_name(const char *name)
  694. {
  695. AVCodec *p;
  696. if (!name)
  697. return NULL;
  698. p = first_avcodec;
  699. while (p) {
  700. if (p->decode != NULL && strcmp(name,p->name) == 0)
  701. return p;
  702. p = p->next;
  703. }
  704. return NULL;
  705. }
  706. static int get_bit_rate(AVCodecContext *ctx)
  707. {
  708. int bit_rate;
  709. int bits_per_sample;
  710. switch(ctx->codec_type) {
  711. case AVMEDIA_TYPE_VIDEO:
  712. case AVMEDIA_TYPE_DATA:
  713. case AVMEDIA_TYPE_SUBTITLE:
  714. case AVMEDIA_TYPE_ATTACHMENT:
  715. bit_rate = ctx->bit_rate;
  716. break;
  717. case AVMEDIA_TYPE_AUDIO:
  718. bits_per_sample = av_get_bits_per_sample(ctx->codec_id);
  719. bit_rate = bits_per_sample ? ctx->sample_rate * ctx->channels * bits_per_sample : ctx->bit_rate;
  720. break;
  721. default:
  722. bit_rate = 0;
  723. break;
  724. }
  725. return bit_rate;
  726. }
  727. size_t av_get_codec_tag_string(char *buf, size_t buf_size, unsigned int codec_tag)
  728. {
  729. int i, len, ret = 0;
  730. for (i = 0; i < 4; i++) {
  731. len = snprintf(buf, buf_size,
  732. isprint(codec_tag&0xFF) ? "%c" : "[%d]", codec_tag&0xFF);
  733. buf += len;
  734. buf_size = buf_size > len ? buf_size - len : 0;
  735. ret += len;
  736. codec_tag>>=8;
  737. }
  738. return ret;
  739. }
  740. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  741. {
  742. const char *codec_name;
  743. AVCodec *p;
  744. char buf1[32];
  745. int bitrate;
  746. AVRational display_aspect_ratio;
  747. if (encode)
  748. p = avcodec_find_encoder(enc->codec_id);
  749. else
  750. p = avcodec_find_decoder(enc->codec_id);
  751. if (p) {
  752. codec_name = p->name;
  753. } else if (enc->codec_id == CODEC_ID_MPEG2TS) {
  754. /* fake mpeg2 transport stream codec (currently not
  755. registered) */
  756. codec_name = "mpeg2ts";
  757. } else if (enc->codec_name[0] != '\0') {
  758. codec_name = enc->codec_name;
  759. } else {
  760. /* output avi tags */
  761. char tag_buf[32];
  762. av_get_codec_tag_string(tag_buf, sizeof(tag_buf), enc->codec_tag);
  763. snprintf(buf1, sizeof(buf1), "%s / 0x%04X", tag_buf, enc->codec_tag);
  764. codec_name = buf1;
  765. }
  766. switch(enc->codec_type) {
  767. case AVMEDIA_TYPE_VIDEO:
  768. snprintf(buf, buf_size,
  769. "Video: %s%s",
  770. codec_name, enc->mb_decision ? " (hq)" : "");
  771. if (enc->pix_fmt != PIX_FMT_NONE) {
  772. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  773. ", %s",
  774. avcodec_get_pix_fmt_name(enc->pix_fmt));
  775. }
  776. if (enc->width) {
  777. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  778. ", %dx%d",
  779. enc->width, enc->height);
  780. if (enc->sample_aspect_ratio.num) {
  781. av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
  782. enc->width*enc->sample_aspect_ratio.num,
  783. enc->height*enc->sample_aspect_ratio.den,
  784. 1024*1024);
  785. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  786. " [PAR %d:%d DAR %d:%d]",
  787. enc->sample_aspect_ratio.num, enc->sample_aspect_ratio.den,
  788. display_aspect_ratio.num, display_aspect_ratio.den);
  789. }
  790. if(av_log_get_level() >= AV_LOG_DEBUG){
  791. int g= av_gcd(enc->time_base.num, enc->time_base.den);
  792. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  793. ", %d/%d",
  794. enc->time_base.num/g, enc->time_base.den/g);
  795. }
  796. }
  797. if (encode) {
  798. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  799. ", q=%d-%d", enc->qmin, enc->qmax);
  800. }
  801. break;
  802. case AVMEDIA_TYPE_AUDIO:
  803. snprintf(buf, buf_size,
  804. "Audio: %s",
  805. codec_name);
  806. if (enc->sample_rate) {
  807. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  808. ", %d Hz", enc->sample_rate);
  809. }
  810. av_strlcat(buf, ", ", buf_size);
  811. av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout);
  812. if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
  813. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  814. ", %s", av_get_sample_fmt_name(enc->sample_fmt));
  815. }
  816. break;
  817. case AVMEDIA_TYPE_DATA:
  818. snprintf(buf, buf_size, "Data: %s", codec_name);
  819. break;
  820. case AVMEDIA_TYPE_SUBTITLE:
  821. snprintf(buf, buf_size, "Subtitle: %s", codec_name);
  822. break;
  823. case AVMEDIA_TYPE_ATTACHMENT:
  824. snprintf(buf, buf_size, "Attachment: %s", codec_name);
  825. break;
  826. default:
  827. snprintf(buf, buf_size, "Invalid Codec type %d", enc->codec_type);
  828. return;
  829. }
  830. if (encode) {
  831. if (enc->flags & CODEC_FLAG_PASS1)
  832. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  833. ", pass 1");
  834. if (enc->flags & CODEC_FLAG_PASS2)
  835. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  836. ", pass 2");
  837. }
  838. bitrate = get_bit_rate(enc);
  839. if (bitrate != 0) {
  840. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  841. ", %d kb/s", bitrate / 1000);
  842. }
  843. }
  844. unsigned avcodec_version( void )
  845. {
  846. return LIBAVCODEC_VERSION_INT;
  847. }
  848. const char *avcodec_configuration(void)
  849. {
  850. return FFMPEG_CONFIGURATION;
  851. }
  852. const char *avcodec_license(void)
  853. {
  854. #define LICENSE_PREFIX "libavcodec license: "
  855. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  856. }
  857. void avcodec_init(void)
  858. {
  859. static int initialized = 0;
  860. if (initialized != 0)
  861. return;
  862. initialized = 1;
  863. dsputil_static_init();
  864. }
  865. void avcodec_flush_buffers(AVCodecContext *avctx)
  866. {
  867. if(avctx->codec->flush)
  868. avctx->codec->flush(avctx);
  869. }
  870. void avcodec_default_free_buffers(AVCodecContext *s){
  871. int i, j;
  872. if(s->internal_buffer==NULL) return;
  873. if (s->internal_buffer_count)
  874. av_log(s, AV_LOG_WARNING, "Found %i unreleased buffers!\n", s->internal_buffer_count);
  875. for(i=0; i<INTERNAL_BUFFER_SIZE; i++){
  876. InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];
  877. for(j=0; j<4; j++){
  878. av_freep(&buf->base[j]);
  879. buf->data[j]= NULL;
  880. }
  881. }
  882. av_freep(&s->internal_buffer);
  883. s->internal_buffer_count=0;
  884. }
  885. char av_get_pict_type_char(int pict_type){
  886. switch(pict_type){
  887. case FF_I_TYPE: return 'I';
  888. case FF_P_TYPE: return 'P';
  889. case FF_B_TYPE: return 'B';
  890. case FF_S_TYPE: return 'S';
  891. case FF_SI_TYPE:return 'i';
  892. case FF_SP_TYPE:return 'p';
  893. case FF_BI_TYPE:return 'b';
  894. default: return '?';
  895. }
  896. }
  897. int av_get_bits_per_sample(enum CodecID codec_id){
  898. switch(codec_id){
  899. case CODEC_ID_ADPCM_SBPRO_2:
  900. return 2;
  901. case CODEC_ID_ADPCM_SBPRO_3:
  902. return 3;
  903. case CODEC_ID_ADPCM_SBPRO_4:
  904. case CODEC_ID_ADPCM_CT:
  905. case CODEC_ID_ADPCM_IMA_WAV:
  906. case CODEC_ID_ADPCM_MS:
  907. case CODEC_ID_ADPCM_YAMAHA:
  908. return 4;
  909. case CODEC_ID_PCM_ALAW:
  910. case CODEC_ID_PCM_MULAW:
  911. case CODEC_ID_PCM_S8:
  912. case CODEC_ID_PCM_U8:
  913. case CODEC_ID_PCM_ZORK:
  914. return 8;
  915. case CODEC_ID_PCM_S16BE:
  916. case CODEC_ID_PCM_S16LE:
  917. case CODEC_ID_PCM_S16LE_PLANAR:
  918. case CODEC_ID_PCM_U16BE:
  919. case CODEC_ID_PCM_U16LE:
  920. return 16;
  921. case CODEC_ID_PCM_S24DAUD:
  922. case CODEC_ID_PCM_S24BE:
  923. case CODEC_ID_PCM_S24LE:
  924. case CODEC_ID_PCM_U24BE:
  925. case CODEC_ID_PCM_U24LE:
  926. return 24;
  927. case CODEC_ID_PCM_S32BE:
  928. case CODEC_ID_PCM_S32LE:
  929. case CODEC_ID_PCM_U32BE:
  930. case CODEC_ID_PCM_U32LE:
  931. case CODEC_ID_PCM_F32BE:
  932. case CODEC_ID_PCM_F32LE:
  933. return 32;
  934. case CODEC_ID_PCM_F64BE:
  935. case CODEC_ID_PCM_F64LE:
  936. return 64;
  937. default:
  938. return 0;
  939. }
  940. }
  941. #if FF_API_OLD_SAMPLE_FMT
  942. int av_get_bits_per_sample_format(enum AVSampleFormat sample_fmt) {
  943. return av_get_bits_per_sample_fmt(sample_fmt);
  944. }
  945. #endif
  946. #if !HAVE_THREADS
  947. int avcodec_thread_init(AVCodecContext *s, int thread_count){
  948. s->thread_count = thread_count;
  949. return -1;
  950. }
  951. #endif
  952. unsigned int av_xiphlacing(unsigned char *s, unsigned int v)
  953. {
  954. unsigned int n = 0;
  955. while(v >= 0xff) {
  956. *s++ = 0xff;
  957. v -= 0xff;
  958. n++;
  959. }
  960. *s = v;
  961. n++;
  962. return n;
  963. }
  964. #if LIBAVCODEC_VERSION_MAJOR < 53
  965. #include "libavcore/parseutils.h"
  966. int av_parse_video_frame_size(int *width_ptr, int *height_ptr, const char *str)
  967. {
  968. return av_parse_video_size(width_ptr, height_ptr, str);
  969. }
  970. int av_parse_video_frame_rate(AVRational *frame_rate, const char *arg)
  971. {
  972. return av_parse_video_rate(frame_rate, arg);
  973. }
  974. #endif
  975. int ff_match_2uint16(const uint16_t (*tab)[2], int size, int a, int b){
  976. int i;
  977. for(i=0; i<size && !(tab[i][0]==a && tab[i][1]==b); i++);
  978. return i;
  979. }
  980. void av_log_missing_feature(void *avc, const char *feature, int want_sample)
  981. {
  982. av_log(avc, AV_LOG_WARNING, "%s not implemented. Update your FFmpeg "
  983. "version to the newest one from SVN. If the problem still "
  984. "occurs, it means that your file has a feature which has not "
  985. "been implemented.", feature);
  986. if(want_sample)
  987. av_log_ask_for_sample(avc, NULL);
  988. else
  989. av_log(avc, AV_LOG_WARNING, "\n");
  990. }
  991. void av_log_ask_for_sample(void *avc, const char *msg)
  992. {
  993. if (msg)
  994. av_log(avc, AV_LOG_WARNING, "%s ", msg);
  995. av_log(avc, AV_LOG_WARNING, "If you want to help, upload a sample "
  996. "of this file to ftp://upload.ffmpeg.org/MPlayer/incoming/ "
  997. "and contact the ffmpeg-devel mailing list.\n");
  998. }
  999. static AVHWAccel *first_hwaccel = NULL;
  1000. void av_register_hwaccel(AVHWAccel *hwaccel)
  1001. {
  1002. AVHWAccel **p = &first_hwaccel;
  1003. while (*p)
  1004. p = &(*p)->next;
  1005. *p = hwaccel;
  1006. hwaccel->next = NULL;
  1007. }
  1008. AVHWAccel *av_hwaccel_next(AVHWAccel *hwaccel)
  1009. {
  1010. return hwaccel ? hwaccel->next : first_hwaccel;
  1011. }
  1012. AVHWAccel *ff_find_hwaccel(enum CodecID codec_id, enum PixelFormat pix_fmt)
  1013. {
  1014. AVHWAccel *hwaccel=NULL;
  1015. while((hwaccel= av_hwaccel_next(hwaccel))){
  1016. if ( hwaccel->id == codec_id
  1017. && hwaccel->pix_fmt == pix_fmt)
  1018. return hwaccel;
  1019. }
  1020. return NULL;
  1021. }
  1022. int av_lockmgr_register(int (*cb)(void **mutex, enum AVLockOp op))
  1023. {
  1024. if (ff_lockmgr_cb) {
  1025. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_DESTROY))
  1026. return -1;
  1027. }
  1028. ff_lockmgr_cb = cb;
  1029. if (ff_lockmgr_cb) {
  1030. if (ff_lockmgr_cb(&codec_mutex, AV_LOCK_CREATE))
  1031. return -1;
  1032. }
  1033. return 0;
  1034. }
  1035. unsigned int ff_toupper4(unsigned int x)
  1036. {
  1037. return toupper( x &0xFF)
  1038. + (toupper((x>>8 )&0xFF)<<8 )
  1039. + (toupper((x>>16)&0xFF)<<16)
  1040. + (toupper((x>>24)&0xFF)<<24);
  1041. }