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.

717 lines
18KB

  1. /*
  2. * utils for libavcodec
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #include "mpegvideo.h"
  22. void *av_mallocz(unsigned int size)
  23. {
  24. void *ptr;
  25. if(size == 0) fprintf(stderr, "Warning, allocating 0 bytes\n");
  26. ptr = av_malloc(size);
  27. if (!ptr)
  28. return NULL;
  29. memset(ptr, 0, size);
  30. return ptr;
  31. }
  32. /* allocation of static arrays - do not use for normal allocation */
  33. static unsigned int last_static = 0;
  34. static char*** array_static = NULL;
  35. static const unsigned int grow_static = 64; // ^2
  36. void *__av_mallocz_static(void** location, unsigned int size)
  37. {
  38. int l = (last_static + grow_static) & ~(grow_static - 1);
  39. void *ptr = av_mallocz(size);
  40. if (!ptr)
  41. return NULL;
  42. if (location)
  43. {
  44. if (l > last_static)
  45. array_static = realloc(array_static, l);
  46. array_static[last_static++] = (char**) location;
  47. *location = ptr;
  48. }
  49. return ptr;
  50. }
  51. /* free all static arrays and reset pointers to 0 */
  52. void av_free_static()
  53. {
  54. if (array_static)
  55. {
  56. unsigned i;
  57. for (i = 0; i < last_static; i++)
  58. {
  59. free(*array_static[i]);
  60. *array_static[i] = NULL;
  61. }
  62. free(array_static);
  63. array_static = 0;
  64. }
  65. last_static = 0;
  66. }
  67. /* cannot call it directly because of 'void **' casting is not automatic */
  68. void __av_freep(void **ptr)
  69. {
  70. av_free(*ptr);
  71. *ptr = NULL;
  72. }
  73. /* encoder management */
  74. AVCodec *first_avcodec;
  75. void register_avcodec(AVCodec *format)
  76. {
  77. AVCodec **p;
  78. p = &first_avcodec;
  79. while (*p != NULL) p = &(*p)->next;
  80. *p = format;
  81. format->next = NULL;
  82. }
  83. void avcodec_get_chroma_sub_sample(int fmt, int *h_shift, int *v_shift){
  84. switch(fmt){
  85. case PIX_FMT_YUV410P:
  86. *h_shift=2;
  87. *v_shift=2;
  88. break;
  89. case PIX_FMT_YUV420P:
  90. *h_shift=1;
  91. *v_shift=1;
  92. break;
  93. case PIX_FMT_YUV411P:
  94. *h_shift=2;
  95. *v_shift=0;
  96. break;
  97. case PIX_FMT_YUV422P:
  98. case PIX_FMT_YUV422:
  99. *h_shift=1;
  100. *v_shift=0;
  101. break;
  102. default: //RGB/...
  103. *h_shift=0;
  104. *v_shift=0;
  105. break;
  106. }
  107. }
  108. typedef struct DefaultPicOpaque{
  109. int last_pic_num;
  110. uint8_t *data[4];
  111. }DefaultPicOpaque;
  112. int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
  113. int i;
  114. const int width = s->width;
  115. const int height= s->height;
  116. DefaultPicOpaque *opaque;
  117. assert(pic->data[0]==NULL);
  118. assert(pic->type==0 || pic->type==FF_TYPE_INTERNAL);
  119. if(pic->opaque){
  120. opaque= (DefaultPicOpaque *)pic->opaque;
  121. for(i=0; i<3; i++)
  122. pic->data[i]= opaque->data[i];
  123. // printf("get_buffer %X coded_pic_num:%d last:%d\n", pic->opaque, pic->coded_picture_number, opaque->last_pic_num);
  124. pic->age= pic->coded_picture_number - opaque->last_pic_num;
  125. opaque->last_pic_num= pic->coded_picture_number;
  126. //printf("age: %d %d %d\n", pic->age, c->picture_number, pic->coded_picture_number);
  127. }else{
  128. int align, h_chroma_shift, v_chroma_shift;
  129. int w, h, pixel_size;
  130. avcodec_get_chroma_sub_sample(s->pix_fmt, &h_chroma_shift, &v_chroma_shift);
  131. switch(s->pix_fmt){
  132. case PIX_FMT_YUV422:
  133. pixel_size=2;
  134. break;
  135. case PIX_FMT_RGB24:
  136. case PIX_FMT_BGR24:
  137. pixel_size=3;
  138. break;
  139. case PIX_FMT_BGRA32:
  140. case PIX_FMT_RGBA32:
  141. pixel_size=4;
  142. break;
  143. default:
  144. pixel_size=1;
  145. }
  146. if(s->codec_id==CODEC_ID_SVQ1) align=63;
  147. else align=15;
  148. w= (width +align)&~align;
  149. h= (height+align)&~align;
  150. if(!(s->flags&CODEC_FLAG_EMU_EDGE)){
  151. w+= EDGE_WIDTH*2;
  152. h+= EDGE_WIDTH*2;
  153. }
  154. opaque= av_mallocz(sizeof(DefaultPicOpaque));
  155. if(opaque==NULL) return -1;
  156. pic->opaque= opaque;
  157. opaque->last_pic_num= -256*256*256*64;
  158. for(i=0; i<3; i++){
  159. int h_shift= i==0 ? 0 : h_chroma_shift;
  160. int v_shift= i==0 ? 0 : v_chroma_shift;
  161. pic->linesize[i]= pixel_size*w>>h_shift;
  162. pic->base[i]= av_mallocz((pic->linesize[i]*h>>v_shift)+16); //FIXME 16
  163. if(pic->base[i]==NULL) return -1;
  164. memset(pic->base[i], 128, pic->linesize[i]*h>>v_shift);
  165. if(s->flags&CODEC_FLAG_EMU_EDGE)
  166. pic->data[i] = pic->base[i] + 16; //FIXME 16
  167. else
  168. pic->data[i] = pic->base[i] + (pic->linesize[i]*EDGE_WIDTH>>v_shift) + (EDGE_WIDTH>>h_shift) + 16; //FIXME 16
  169. opaque->data[i]= pic->data[i];
  170. }
  171. pic->age= 256*256*256*64;
  172. pic->type= FF_BUFFER_TYPE_INTERNAL;
  173. }
  174. return 0;
  175. }
  176. void avcodec_default_release_buffer(AVCodecContext *s, AVFrame *pic){
  177. int i;
  178. assert(pic->type==FF_BUFFER_TYPE_INTERNAL);
  179. for(i=0; i<3; i++)
  180. pic->data[i]=NULL;
  181. //printf("R%X\n", pic->opaque);
  182. }
  183. void avcodec_get_context_defaults(AVCodecContext *s){
  184. s->bit_rate= 800*1000;
  185. s->bit_rate_tolerance= s->bit_rate*10;
  186. s->qmin= 2;
  187. s->qmax= 31;
  188. s->mb_qmin= 2;
  189. s->mb_qmax= 31;
  190. s->rc_eq= "tex^qComp";
  191. s->qcompress= 0.5;
  192. s->max_qdiff= 3;
  193. s->b_quant_factor=1.25;
  194. s->b_quant_offset=1.25;
  195. s->i_quant_factor=-0.8;
  196. s->i_quant_offset=0.0;
  197. s->error_concealment= 3;
  198. s->error_resilience= 1;
  199. s->workaround_bugs= FF_BUG_AUTODETECT;
  200. s->frame_rate = 25 * FRAME_RATE_BASE;
  201. s->gop_size= 50;
  202. s->me_method= ME_EPZS;
  203. s->get_buffer= avcodec_default_get_buffer;
  204. s->release_buffer= avcodec_default_release_buffer;
  205. s->me_subpel_quality=8;
  206. }
  207. /**
  208. * allocates a AVCodecContext and set it to defaults.
  209. * this can be deallocated by simply calling free()
  210. */
  211. AVCodecContext *avcodec_alloc_context(void){
  212. AVCodecContext *avctx= av_mallocz(sizeof(AVCodecContext));
  213. if(avctx==NULL) return NULL;
  214. avcodec_get_context_defaults(avctx);
  215. return avctx;
  216. }
  217. /**
  218. * allocates a AVPFrame and set it to defaults.
  219. * this can be deallocated by simply calling free()
  220. */
  221. AVFrame *avcodec_alloc_frame(void){
  222. AVFrame *pic= av_mallocz(sizeof(AVFrame));
  223. return pic;
  224. }
  225. int avcodec_open(AVCodecContext *avctx, AVCodec *codec)
  226. {
  227. int ret;
  228. avctx->codec = codec;
  229. avctx->codec_id = codec->id;
  230. avctx->frame_number = 0;
  231. if (codec->priv_data_size > 0) {
  232. avctx->priv_data = av_mallocz(codec->priv_data_size);
  233. if (!avctx->priv_data)
  234. return -ENOMEM;
  235. } else {
  236. avctx->priv_data = NULL;
  237. }
  238. ret = avctx->codec->init(avctx);
  239. if (ret < 0) {
  240. av_freep(&avctx->priv_data);
  241. return ret;
  242. }
  243. return 0;
  244. }
  245. int avcodec_encode_audio(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  246. const short *samples)
  247. {
  248. int ret;
  249. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);
  250. avctx->frame_number++;
  251. return ret;
  252. }
  253. int avcodec_encode_video(AVCodecContext *avctx, UINT8 *buf, int buf_size,
  254. const AVFrame *pict)
  255. {
  256. int ret;
  257. ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);
  258. emms_c(); //needed to avoid a emms_c() call before every return;
  259. avctx->frame_number++;
  260. return ret;
  261. }
  262. /* decode a frame. return -1 if error, otherwise return the number of
  263. bytes used. If no frame could be decompressed, *got_picture_ptr is
  264. zero. Otherwise, it is non zero */
  265. int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
  266. int *got_picture_ptr,
  267. UINT8 *buf, int buf_size)
  268. {
  269. int ret;
  270. ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
  271. buf, buf_size);
  272. emms_c(); //needed to avoid a emms_c() call before every return;
  273. if (*got_picture_ptr)
  274. avctx->frame_number++;
  275. return ret;
  276. }
  277. /* decode an audio frame. return -1 if error, otherwise return the
  278. *number of bytes used. If no frame could be decompressed,
  279. *frame_size_ptr is zero. Otherwise, it is the decompressed frame
  280. *size in BYTES. */
  281. int avcodec_decode_audio(AVCodecContext *avctx, INT16 *samples,
  282. int *frame_size_ptr,
  283. UINT8 *buf, int buf_size)
  284. {
  285. int ret;
  286. ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
  287. buf, buf_size);
  288. avctx->frame_number++;
  289. return ret;
  290. }
  291. int avcodec_close(AVCodecContext *avctx)
  292. {
  293. if (avctx->codec->close)
  294. avctx->codec->close(avctx);
  295. av_freep(&avctx->priv_data);
  296. avctx->codec = NULL;
  297. return 0;
  298. }
  299. AVCodec *avcodec_find_encoder(enum CodecID id)
  300. {
  301. AVCodec *p;
  302. p = first_avcodec;
  303. while (p) {
  304. if (p->encode != NULL && p->id == id)
  305. return p;
  306. p = p->next;
  307. }
  308. return NULL;
  309. }
  310. AVCodec *avcodec_find_encoder_by_name(const char *name)
  311. {
  312. AVCodec *p;
  313. p = first_avcodec;
  314. while (p) {
  315. if (p->encode != NULL && strcmp(name,p->name) == 0)
  316. return p;
  317. p = p->next;
  318. }
  319. return NULL;
  320. }
  321. AVCodec *avcodec_find_decoder(enum CodecID id)
  322. {
  323. AVCodec *p;
  324. p = first_avcodec;
  325. while (p) {
  326. if (p->decode != NULL && p->id == id)
  327. return p;
  328. p = p->next;
  329. }
  330. return NULL;
  331. }
  332. AVCodec *avcodec_find_decoder_by_name(const char *name)
  333. {
  334. AVCodec *p;
  335. p = first_avcodec;
  336. while (p) {
  337. if (p->decode != NULL && strcmp(name,p->name) == 0)
  338. return p;
  339. p = p->next;
  340. }
  341. return NULL;
  342. }
  343. AVCodec *avcodec_find(enum CodecID id)
  344. {
  345. AVCodec *p;
  346. p = first_avcodec;
  347. while (p) {
  348. if (p->id == id)
  349. return p;
  350. p = p->next;
  351. }
  352. return NULL;
  353. }
  354. const char *pix_fmt_str[] = {
  355. "yuv420p",
  356. "yuv422",
  357. "rgb24",
  358. "bgr24",
  359. "yuv422p",
  360. "yuv444p",
  361. "rgba32",
  362. "bgra32",
  363. "yuv410p",
  364. "yuv411p",
  365. };
  366. void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
  367. {
  368. const char *codec_name;
  369. AVCodec *p;
  370. char buf1[32];
  371. char channels_str[100];
  372. int bitrate;
  373. if (encode)
  374. p = avcodec_find_encoder(enc->codec_id);
  375. else
  376. p = avcodec_find_decoder(enc->codec_id);
  377. if (p) {
  378. codec_name = p->name;
  379. } else if (enc->codec_name[0] != '\0') {
  380. codec_name = enc->codec_name;
  381. } else {
  382. /* output avi tags */
  383. if (enc->codec_type == CODEC_TYPE_VIDEO) {
  384. snprintf(buf1, sizeof(buf1), "%c%c%c%c",
  385. enc->codec_tag & 0xff,
  386. (enc->codec_tag >> 8) & 0xff,
  387. (enc->codec_tag >> 16) & 0xff,
  388. (enc->codec_tag >> 24) & 0xff);
  389. } else {
  390. snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);
  391. }
  392. codec_name = buf1;
  393. }
  394. switch(enc->codec_type) {
  395. case CODEC_TYPE_VIDEO:
  396. snprintf(buf, buf_size,
  397. "Video: %s%s",
  398. codec_name, enc->flags & CODEC_FLAG_HQ ? " (hq)" : "");
  399. if (enc->codec_id == CODEC_ID_RAWVIDEO) {
  400. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  401. ", %s",
  402. pix_fmt_str[enc->pix_fmt]);
  403. }
  404. if (enc->width) {
  405. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  406. ", %dx%d, %0.2f fps",
  407. enc->width, enc->height,
  408. (float)enc->frame_rate / FRAME_RATE_BASE);
  409. }
  410. if (encode) {
  411. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  412. ", q=%d-%d", enc->qmin, enc->qmax);
  413. }
  414. bitrate = enc->bit_rate;
  415. break;
  416. case CODEC_TYPE_AUDIO:
  417. snprintf(buf, buf_size,
  418. "Audio: %s",
  419. codec_name);
  420. switch (enc->channels) {
  421. case 1:
  422. strcpy(channels_str, "mono");
  423. break;
  424. case 2:
  425. strcpy(channels_str, "stereo");
  426. break;
  427. case 6:
  428. strcpy(channels_str, "5:1");
  429. break;
  430. default:
  431. sprintf(channels_str, "%d channels", enc->channels);
  432. break;
  433. }
  434. if (enc->sample_rate) {
  435. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  436. ", %d Hz, %s",
  437. enc->sample_rate,
  438. channels_str);
  439. }
  440. /* for PCM codecs, compute bitrate directly */
  441. switch(enc->codec_id) {
  442. case CODEC_ID_PCM_S16LE:
  443. case CODEC_ID_PCM_S16BE:
  444. case CODEC_ID_PCM_U16LE:
  445. case CODEC_ID_PCM_U16BE:
  446. bitrate = enc->sample_rate * enc->channels * 16;
  447. break;
  448. case CODEC_ID_PCM_S8:
  449. case CODEC_ID_PCM_U8:
  450. case CODEC_ID_PCM_ALAW:
  451. case CODEC_ID_PCM_MULAW:
  452. bitrate = enc->sample_rate * enc->channels * 8;
  453. break;
  454. default:
  455. bitrate = enc->bit_rate;
  456. break;
  457. }
  458. break;
  459. default:
  460. av_abort();
  461. }
  462. if (encode) {
  463. if (enc->flags & CODEC_FLAG_PASS1)
  464. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  465. ", pass 1");
  466. if (enc->flags & CODEC_FLAG_PASS2)
  467. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  468. ", pass 2");
  469. }
  470. if (bitrate != 0) {
  471. snprintf(buf + strlen(buf), buf_size - strlen(buf),
  472. ", %d kb/s", bitrate / 1000);
  473. }
  474. }
  475. /* Picture field are filled with 'ptr' addresses */
  476. void avpicture_fill(AVPicture *picture, UINT8 *ptr,
  477. int pix_fmt, int width, int height)
  478. {
  479. int size;
  480. size = width * height;
  481. switch(pix_fmt) {
  482. case PIX_FMT_YUV420P:
  483. picture->data[0] = ptr;
  484. picture->data[1] = picture->data[0] + size;
  485. picture->data[2] = picture->data[1] + size / 4;
  486. picture->linesize[0] = width;
  487. picture->linesize[1] = width / 2;
  488. picture->linesize[2] = width / 2;
  489. break;
  490. case PIX_FMT_YUV422P:
  491. picture->data[0] = ptr;
  492. picture->data[1] = picture->data[0] + size;
  493. picture->data[2] = picture->data[1] + size / 2;
  494. picture->linesize[0] = width;
  495. picture->linesize[1] = width / 2;
  496. picture->linesize[2] = width / 2;
  497. break;
  498. case PIX_FMT_YUV444P:
  499. picture->data[0] = ptr;
  500. picture->data[1] = picture->data[0] + size;
  501. picture->data[2] = picture->data[1] + size;
  502. picture->linesize[0] = width;
  503. picture->linesize[1] = width;
  504. picture->linesize[2] = width;
  505. break;
  506. case PIX_FMT_RGB24:
  507. case PIX_FMT_BGR24:
  508. picture->data[0] = ptr;
  509. picture->data[1] = NULL;
  510. picture->data[2] = NULL;
  511. picture->linesize[0] = width * 3;
  512. break;
  513. case PIX_FMT_RGBA32:
  514. case PIX_FMT_BGRA32:
  515. picture->data[0] = ptr;
  516. picture->data[1] = NULL;
  517. picture->data[2] = NULL;
  518. picture->linesize[0] = width * 4;
  519. break;
  520. case PIX_FMT_YUV422:
  521. picture->data[0] = ptr;
  522. picture->data[1] = NULL;
  523. picture->data[2] = NULL;
  524. picture->linesize[0] = width * 2;
  525. break;
  526. default:
  527. picture->data[0] = NULL;
  528. picture->data[1] = NULL;
  529. picture->data[2] = NULL;
  530. break;
  531. }
  532. }
  533. int avpicture_get_size(int pix_fmt, int width, int height)
  534. {
  535. int size;
  536. size = width * height;
  537. switch(pix_fmt) {
  538. case PIX_FMT_YUV420P:
  539. size = (size * 3) / 2;
  540. break;
  541. case PIX_FMT_YUV422P:
  542. size = (size * 2);
  543. break;
  544. case PIX_FMT_YUV444P:
  545. size = (size * 3);
  546. break;
  547. case PIX_FMT_RGB24:
  548. case PIX_FMT_BGR24:
  549. size = (size * 3);
  550. break;
  551. case PIX_FMT_RGBA32:
  552. case PIX_FMT_BGRA32:
  553. size = (size * 4);
  554. break;
  555. case PIX_FMT_YUV422:
  556. size = (size * 2);
  557. break;
  558. default:
  559. size = -1;
  560. break;
  561. }
  562. return size;
  563. }
  564. unsigned avcodec_version( void )
  565. {
  566. return LIBAVCODEC_VERSION_INT;
  567. }
  568. unsigned avcodec_build( void )
  569. {
  570. return LIBAVCODEC_BUILD;
  571. }
  572. /* must be called before any other functions */
  573. void avcodec_init(void)
  574. {
  575. static int inited = 0;
  576. if (inited != 0)
  577. return;
  578. inited = 1;
  579. //dsputil_init();
  580. }
  581. /* this can be called after seeking and before trying to decode the next keyframe */
  582. void avcodec_flush_buffers(AVCodecContext *avctx)
  583. {
  584. int i;
  585. MpegEncContext *s = avctx->priv_data;
  586. switch(avctx->codec_id){
  587. case CODEC_ID_MPEG1VIDEO:
  588. case CODEC_ID_H263:
  589. case CODEC_ID_RV10:
  590. case CODEC_ID_MJPEG:
  591. case CODEC_ID_MJPEGB:
  592. case CODEC_ID_MPEG4:
  593. case CODEC_ID_MSMPEG4V1:
  594. case CODEC_ID_MSMPEG4V2:
  595. case CODEC_ID_MSMPEG4V3:
  596. case CODEC_ID_WMV1:
  597. case CODEC_ID_WMV2:
  598. case CODEC_ID_H263P:
  599. case CODEC_ID_H263I:
  600. case CODEC_ID_SVQ1:
  601. for(i=0; i<MAX_PICTURE_COUNT; i++){
  602. if(s->picture[i].data[0] && ( s->picture[i].type == FF_BUFFER_TYPE_INTERNAL
  603. || s->picture[i].type == FF_BUFFER_TYPE_USER))
  604. avctx->release_buffer(avctx, (AVFrame*)&s->picture[i]);
  605. }
  606. s->last_picture.data[0] = s->next_picture.data[0] = NULL;
  607. break;
  608. default:
  609. //FIXME
  610. break;
  611. }
  612. }
  613. static int raw_encode_init(AVCodecContext *s)
  614. {
  615. return 0;
  616. }
  617. static int raw_decode_frame(AVCodecContext *avctx,
  618. void *data, int *data_size,
  619. UINT8 *buf, int buf_size)
  620. {
  621. return -1;
  622. }
  623. static int raw_encode_frame(AVCodecContext *avctx,
  624. unsigned char *frame, int buf_size, void *data)
  625. {
  626. return -1;
  627. }
  628. AVCodec rawvideo_codec = {
  629. "rawvideo",
  630. CODEC_TYPE_VIDEO,
  631. CODEC_ID_RAWVIDEO,
  632. 0,
  633. raw_encode_init,
  634. raw_encode_frame,
  635. NULL,
  636. raw_decode_frame,
  637. };