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.

123 lines
3.7KB

  1. /*
  2. * Alias PIX image encoder
  3. * Copyright (C) 2014 Vittorio Giovara <vittorio.giovara@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/intreadwrite.h"
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "internal.h"
  25. #define ALIAS_HEADER_SIZE 10
  26. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  27. const AVFrame *frame, int *got_packet)
  28. {
  29. int width, height, bits_pixel, i, j, length, ret;
  30. uint8_t *in_buf, *buf;
  31. #if FF_API_CODED_FRAME
  32. FF_DISABLE_DEPRECATION_WARNINGS
  33. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  34. avctx->coded_frame->key_frame = 1;
  35. FF_ENABLE_DEPRECATION_WARNINGS
  36. #endif
  37. width = avctx->width;
  38. height = avctx->height;
  39. if (width > 65535 || height > 65535 ||
  40. width * height >= INT_MAX / 4 - ALIAS_HEADER_SIZE) {
  41. av_log(avctx, AV_LOG_ERROR, "Invalid image size %dx%d.\n", width, height);
  42. return AVERROR_INVALIDDATA;
  43. }
  44. switch (avctx->pix_fmt) {
  45. case AV_PIX_FMT_GRAY8:
  46. bits_pixel = 8;
  47. break;
  48. case AV_PIX_FMT_BGR24:
  49. bits_pixel = 24;
  50. break;
  51. default:
  52. return AVERROR(EINVAL);
  53. }
  54. length = ALIAS_HEADER_SIZE + 4 * width * height; // max possible
  55. if ((ret = ff_alloc_packet(pkt, length)) < 0) {
  56. av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
  57. return ret;
  58. }
  59. buf = pkt->data;
  60. /* Encode header. */
  61. bytestream_put_be16(&buf, width);
  62. bytestream_put_be16(&buf, height);
  63. bytestream_put_be32(&buf, 0); /* X, Y offset */
  64. bytestream_put_be16(&buf, bits_pixel);
  65. for (j = 0; j < height; j++) {
  66. in_buf = frame->data[0] + frame->linesize[0] * j;
  67. for (i = 0; i < width; ) {
  68. int count = 0;
  69. int pixel;
  70. if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
  71. pixel = *in_buf;
  72. while (count < 255 && count + i < width && pixel == *in_buf) {
  73. count++;
  74. in_buf++;
  75. }
  76. bytestream_put_byte(&buf, count);
  77. bytestream_put_byte(&buf, pixel);
  78. } else { /* AV_PIX_FMT_BGR24 */
  79. pixel = AV_RB24(in_buf);
  80. while (count < 255 && count + i < width &&
  81. pixel == AV_RB24(in_buf)) {
  82. count++;
  83. in_buf += 3;
  84. }
  85. bytestream_put_byte(&buf, count);
  86. bytestream_put_be24(&buf, pixel);
  87. }
  88. i += count;
  89. }
  90. }
  91. /* Total length */
  92. av_shrink_packet(pkt, buf - pkt->data);
  93. pkt->flags |= AV_PKT_FLAG_KEY;
  94. *got_packet = 1;
  95. return 0;
  96. }
  97. AVCodec ff_alias_pix_encoder = {
  98. .name = "alias_pix",
  99. .long_name = NULL_IF_CONFIG_SMALL("Alias/Wavefront PIX image"),
  100. .type = AVMEDIA_TYPE_VIDEO,
  101. .id = AV_CODEC_ID_ALIAS_PIX,
  102. .encode2 = encode_frame,
  103. .pix_fmts = (const enum AVPixelFormat[]) {
  104. AV_PIX_FMT_BGR24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
  105. },
  106. };