td_h264dec.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (C) 2009-2010 Felipe Contreras
  3. * Copyright (C) 2008-2010 Nokia Corporation
  4. *
  5. * Authors:
  6. * Felipe Contreras <felipe.contreras@gmail.com>
  7. * Juha Alanen <juha.m.alanen@nokia.com>
  8. *
  9. * This file may be used under the terms of the GNU Lesser General Public
  10. * License version 2.1, a copy of which is found in LICENSE included in the
  11. * packaging of this file.
  12. */
  13. #include "dsp_bridge.h"
  14. #include "dmm_buffer.h"
  15. #include "td_h264dec_common.h"
  16. #include "gstdspbase.h"
  17. #include "gstdspvdec.h"
  18. struct create_args {
  19. uint32_t size;
  20. uint16_t num_streams;
  21. uint16_t in_id;
  22. uint16_t in_type;
  23. uint16_t in_count;
  24. uint16_t out_id;
  25. uint16_t out_type;
  26. uint16_t out_count;
  27. uint16_t reserved;
  28. uint32_t max_width;
  29. uint32_t max_height;
  30. uint32_t color_format;
  31. uint32_t max_framerate;
  32. uint32_t max_bitrate;
  33. uint32_t endianness;
  34. uint32_t profile;
  35. int32_t max_level;
  36. uint32_t mode;
  37. int32_t preroll;
  38. uint32_t stream_format;
  39. uint32_t display_width;
  40. };
  41. static void create_args(GstDspBase *base, unsigned *profile_id, void **arg_data)
  42. {
  43. GstDspVDec *self = GST_DSP_VDEC(base);
  44. struct create_args args = {
  45. .size = sizeof(args) - 4,
  46. .num_streams = 2,
  47. .in_id = 0,
  48. .in_type = 0,
  49. .in_count = base->ports[0]->num_buffers,
  50. .out_id = 1,
  51. .out_type = 0,
  52. .out_count = base->ports[1]->num_buffers,
  53. .max_width = self->width,
  54. .max_height = self->height,
  55. .color_format = self->color_format == GST_MAKE_FOURCC('U', 'Y', 'V', 'Y') ? 1 : 0,
  56. .max_bitrate = -1,
  57. .endianness = 1,
  58. .max_level = -1,
  59. };
  60. if (self->width * self->height > 352 * 288)
  61. *profile_id = 3;
  62. else if (self->width * self->height > 176 * 144)
  63. *profile_id = 2;
  64. else
  65. *profile_id = 1;
  66. *arg_data = malloc(sizeof(args));
  67. memcpy(*arg_data, &args, sizeof(args));
  68. }
  69. static void transform_nal_encoding(GstDspVDec *self, struct td_buffer *tb)
  70. {
  71. guint8 *data;
  72. gint size;
  73. gint lol;
  74. guint val, nal;
  75. dmm_buffer_t *b = tb->data;
  76. data = b->data;
  77. size = b->len;
  78. lol = self->priv.h264.lol;
  79. nal = 0;
  80. while (size) {
  81. if (size < lol)
  82. goto fail;
  83. /* get NAL size encoded in BE lol bytes */
  84. val = GST_READ_UINT32_BE(data);
  85. val >>= ((4 - lol) << 3);
  86. if (lol == 4)
  87. /* blank size prefix with 00 00 00 01 */
  88. GST_WRITE_UINT32_BE(data, 0x01);
  89. else if (lol == 3)
  90. /* blank size prefix with 00 00 01 */
  91. GST_WRITE_UINT24_BE(data, 0x01);
  92. else
  93. nal++;
  94. data += lol + val;
  95. size -= lol + val;
  96. }
  97. if (lol < 3) {
  98. /* slower, but unlikely path; need to copy stuff to make room for sync */
  99. guint8 *odata, *alloc_data;
  100. gint osize;
  101. /* set up for next run */
  102. data = b->data;
  103. size = b->len;
  104. osize = size + nal * (4 - lol);
  105. /* save this so it is not free'd by subsequent allocate */
  106. alloc_data = b->allocated_data;
  107. b->allocated_data = NULL;
  108. dmm_buffer_allocate(b, osize);
  109. odata = b->data;
  110. while (size) {
  111. if (size < lol)
  112. goto fail;
  113. /* get NAL size encoded in BE lol bytes */
  114. val = GST_READ_UINT32_BE(data);
  115. val >>= ((4 - lol) << 3);
  116. GST_WRITE_UINT32_BE(odata, 0x01);
  117. odata += 4;
  118. data += lol;
  119. memcpy(odata, data, val);
  120. odata += val;
  121. data += val;
  122. size -= lol + val;
  123. }
  124. /* now release original data */
  125. if (tb->user_data) {
  126. gst_buffer_unref(tb->user_data);
  127. tb->user_data = NULL;
  128. }
  129. free(alloc_data);
  130. }
  131. return;
  132. fail:
  133. pr_warning(self, "failed to transform h264 to codec format");
  134. return;
  135. }
  136. struct out_params {
  137. uint32_t display_id;
  138. uint32_t bytes_consumed;
  139. int32_t error_code;
  140. uint32_t frame_type;
  141. uint32_t num_of_nalu;
  142. int32_t mb_err_status_flag;
  143. int8_t mb_err_status_out[1620];
  144. };
  145. static void out_recv_cb(GstDspBase *base, struct td_buffer *tb)
  146. {
  147. GstDspVDec *vdec = GST_DSP_VDEC(base);
  148. dmm_buffer_t *b = tb->data;
  149. struct out_params *param;
  150. param = tb->params->data;
  151. pr_debug(base, "receive %zu/%ld",
  152. b->len, base->output_buffer_size);
  153. pr_debug(base, "error: 0x%x, frame type: %d",
  154. param->error_code, param->frame_type);
  155. if (param->error_code & 0xffff)
  156. pr_err(base, "decode error");
  157. gstdsp_vdec_len_fixup(vdec, b);
  158. }
  159. static void in_send_cb(GstDspBase *base, struct td_buffer *tb)
  160. {
  161. GstDspVDec *vdec = GST_DSP_VDEC(base);
  162. /* transform MP4 format to bytestream format */
  163. if (G_LIKELY(vdec->priv.h264.lol)) {
  164. pr_debug(base, "transforming H264 buffer data");
  165. /* intercept and transform into dsp expected format */
  166. transform_nal_encoding(vdec, tb);
  167. } else {
  168. /* no more need for callback */
  169. tb->port->send_cb = NULL;
  170. }
  171. }
  172. static void setup_params(GstDspBase *base)
  173. {
  174. struct out_params *out_param;
  175. du_port_t *p;
  176. p = base->ports[0];
  177. p->send_cb = in_send_cb;
  178. base->pre_process_buffer = td_h264dec_check_stream_params;
  179. p = base->ports[1];
  180. gstdsp_port_setup_params(base, p, sizeof(*out_param), NULL);
  181. p->recv_cb = out_recv_cb;
  182. }
  183. static bool handle_extra_data(GstDspBase *base, GstBuffer *buf)
  184. {
  185. bool res;
  186. GstDspVDec *self = GST_DSP_VDEC(base);
  187. buf = td_h264dec_transform_extra_data(self, buf);
  188. if (!buf) {
  189. gstdsp_got_error(base, 0, "invalid codec_data");
  190. return false;
  191. }
  192. res = gstdsp_send_codec_data(base, buf);
  193. gst_buffer_unref(buf);
  194. return res;
  195. }
  196. struct td_codec td_h264dec_codec = {
  197. .uuid = &(const struct dsp_uuid) { 0xCB1E9F0F, 0x9D5A, 0x4434, 0x84, 0x49,
  198. { 0x1F, 0xED, 0x2F, 0x99, 0x2D, 0xF7 } },
  199. .filename = "h264vdec_sn.dll64P",
  200. .setup_params = setup_params,
  201. .create_args = create_args,
  202. .handle_extra_data = handle_extra_data,
  203. };